import { describe, it, expect } from 'vitest'
import { shallowMount } from '@vue/test-utils'
import LiteratureCard from '../LiteratureCard.vue'
import type { LiteratureItem } from '../../../types'

function vm(wrapper: ReturnType<typeof shallowMount>) {
  return wrapper.vm as unknown as { isNew: boolean; isUpdated: boolean }
}

function item(overrides: Partial<LiteratureItem> = {}): LiteratureItem {
  return {
    pmid: 12345,
    title: 'Cancer Immunotherapy Trial',
    first_author: 'Smith J',
    journal: 'New England Journal of Medicine',
    pub_date: '2024-06-01',
    doi: '10.1056/NEJMoa2400001',
    ...overrides,
  }
}

describe('LiteratureCard', () => {
  it('renders title, author, journal', () => {
    const wrapper = shallowMount(LiteratureCard, { props: { item: item() } })
    expect(wrapper.text()).toContain('Cancer Immunotherapy Trial')
    expect(wrapper.text()).toContain('Smith J')
    expect(wrapper.text()).toContain('New England Journal of Medicine')
  })

  it('isNew is true for recently created items', () => {
    const wrapper = shallowMount(LiteratureCard, {
      props: { item: item({ created_at: new Date().toISOString() }) },
    })
    expect(vm(wrapper).isNew).toBe(true)
  })

  it('isNew is false without created_at', () => {
    const wrapper = shallowMount(LiteratureCard, { props: { item: item() } })
    expect(vm(wrapper).isNew).toBe(false)
  })

  it('shows saved state via CSS class', () => {
    const wrapper = shallowMount(LiteratureCard, {
      props: { item: item({ pmid: 999 }), savedPmids: new Set([999]) },
    })
    expect(wrapper.find('.save-btn.saved').exists()).toBe(true)
  })

  it('shows AI summary in non-compact mode', () => {
    const wrapper = shallowMount(LiteratureCard, {
      props: { item: item({ ai_summary: 'Key finding summary.' }) },
    })
    expect(wrapper.text()).toContain('Key finding summary.')
  })

  it('hides AI summary in compact mode', () => {
    const wrapper = shallowMount(LiteratureCard, {
      props: { item: item({ ai_summary: 'Key finding summary.' }), compact: true },
    })
    expect(wrapper.text()).not.toContain('Key finding summary.')
  })

  it('hides action buttons in compact mode', () => {
    const wrapper = shallowMount(LiteratureCard, {
      props: { item: item(), compact: true },
    })
    expect(wrapper.find('.lit-actions').exists()).toBe(false)
  })

  it('emits detail on title click', async () => {
    const testItem = item()
    const wrapper = shallowMount(LiteratureCard, { props: { item: testItem } })
    await wrapper.find('.lit-title-link').trigger('click')
    expect(wrapper.emitted('detail')).toBeTruthy()
    expect(wrapper.emitted('detail')![0]![0]).toStrictEqual(testItem)
  })

  it('emits preview on expand click', async () => {
    const testItem = item()
    const wrapper = shallowMount(LiteratureCard, { props: { item: testItem } })
    await wrapper.find('.expand-btn').trigger('click')
    expect(wrapper.emitted('preview')).toBeTruthy()
    expect(wrapper.emitted('preview')![0]![0]).toStrictEqual(testItem)
  })

  it('emits save on save click', async () => {
    const testItem = item()
    const wrapper = shallowMount(LiteratureCard, { props: { item: testItem } })
    await wrapper.find('.save-btn').trigger('click')
    expect(wrapper.emitted('save')).toBeTruthy()
    expect(wrapper.emitted('save')![0]![0]).toStrictEqual(testItem)
  })

  it('emits dismiss on dismiss click', async () => {
    const testItem = item()
    const wrapper = shallowMount(LiteratureCard, { props: { item: testItem } })
    const btns = wrapper.findAll('.action-btn')
    await btns[btns.length - 1]!.trigger('click')
    expect(wrapper.emitted('dismiss')).toBeTruthy()
    expect(wrapper.emitted('dismiss')![0]![0]).toStrictEqual(testItem)
  })

  it('renders cited by count', () => {
    const wrapper = shallowMount(LiteratureCard, {
      props: { item: item({ cited_by_count: 42 }) },
    })
    expect(wrapper.text()).toContain('42')
  })
})
