import { describe, expect, it, vi } from 'vitest'

import { fireEvent, renderWithProviders, screen } from '../../../test/utils'

import CompactDocumentRow from './CompactDocumentRow'

describe('CompactDocumentRow', () => {
  it('resolves title from title, filename, then File', () => {
    const { rerender } = renderWithProviders(
      <CompactDocumentRow variant="dark" title="Custom" filename="name.pdf" />
    )
    expect(screen.getByText('Custom')).toBeInTheDocument()
    rerender(<CompactDocumentRow variant="dark" filename="name.pdf" />)
    expect(screen.getByText('name.pdf')).toBeInTheDocument()
    rerender(<CompactDocumentRow variant="dark" />)
    expect(screen.getByText('File')).toBeInTheDocument()
  })

  it('renders metadata only when it has a value and defaults MIME type', () => {
    const { container, rerender } = renderWithProviders(
      <CompactDocumentRow
        variant="dark"
        filename="report.pdf"
        fileSize={1024}
      />
    )
    expect(screen.getByText('PDF · 1.0 KB')).toBeInTheDocument()
    rerender(
      <CompactDocumentRow
        variant="dark"
        filename="report"
        mimeType="application/octet-stream"
      />
    )
    expect(screen.queryByText(/·/)).toBeNull()
    const genericIconMarkup = container.querySelector('svg')?.innerHTML
    rerender(
      <CompactDocumentRow
        variant="dark"
        filename="report.pdf"
        mimeType="application/pdf"
      />
    )
    expect(container.querySelector('svg')?.innerHTML).not.toBe(
      genericIconMarkup
    )
  })

  it('maps variant classes across metadata, icon, and activation', () => {
    const onActivate = vi.fn()
    const { container, rerender } = renderWithProviders(
      <CompactDocumentRow
        variant="dark"
        filename="report.pdf"
        onActivate={onActivate}
        activateLabel="Open report"
      />
    )
    const darkMetadata = screen.getByText('PDF').className
    const darkIconTile = container.querySelector('div[aria-hidden]')
    const darkTileClasses = darkIconTile?.className
    const darkIconClasses = darkIconTile
      ?.querySelector('svg')
      ?.getAttribute('class')
    expect(screen.getByRole('button', { name: 'Open report' })).toHaveClass(
      'hover:bg-white/[0.04]'
    )

    rerender(
      <CompactDocumentRow
        variant="light"
        filename="report.pdf"
        onActivate={onActivate}
        activateLabel="Open report"
      />
    )
    expect(screen.getByText('PDF').className).not.toBe(darkMetadata)
    const lightIconTile = container.querySelector('div[aria-hidden]')
    expect(lightIconTile?.className).not.toBe(darkTileClasses)
    expect(lightIconTile?.querySelector('svg')?.getAttribute('class')).not.toBe(
      darkIconClasses
    )
    expect(screen.getByRole('button', { name: 'Open report' })).toHaveClass(
      'hover:bg-black/[0.04]'
    )
  })

  it('activates its body without nesting the trailing action', () => {
    const onActivate = vi.fn()
    renderWithProviders(
      <CompactDocumentRow
        variant="dark"
        filename="report.pdf"
        onActivate={onActivate}
        activateLabel="Open report"
        trailingAction={<button type="button">Download</button>}
      />
    )
    const body = screen.getByRole('button', { name: 'Open report' })
    expect(body).toBeInTheDocument()
    expect(body).not.toContainElement(
      screen.getByRole('button', { name: 'Download' })
    )
    fireEvent.click(body)
    expect(onActivate).toHaveBeenCalledOnce()
  })

  it('renders a plain body when activation is omitted', () => {
    renderWithProviders(
      <CompactDocumentRow variant="dark" filename="report.pdf" />
    )
    expect(screen.queryByRole('button', { name: /report/ })).toBeNull()
  })
})
