import type { Meta, StoryObj } from '@storybook/react-vite'
import { expect } from 'storybook/test'

import type { components } from '../api/schema'
import { SearchResultRow } from './search-result-row'

type SearchHitView = components['schemas']['SearchHitView']

const hit: SearchHitView = {
  id: '11111111-1111-1111-1111-111111111111',
  object_number: '2019.4.12',
  object_name: 'Bronze figurine',
  brief_description: 'A small cast figure.',
  visibility: 'public',
  snippet: 'cast \x02bronze\x03 with green patina',
}

const meta = {
  component: SearchResultRow,
  render: (args) => (
    <ul>
      <SearchResultRow {...args} />
    </ul>
  ),
  tags: ['ai-generated'],
} satisfies Meta<typeof SearchResultRow>

export default meta
type Story = StoryObj<typeof meta>

export const Default: Story = {
  args: { hit },
  play: async ({ canvas }) => {
    const link = canvas.getByRole('link', { name: /bronze figurine/i })
    await expect(link).toHaveAttribute('href', `/search/${hit.id}`)
    // The snippet's marked term renders through Highlight.
    await expect(canvas.getByText('bronze').tagName).toBe('MARK')
  },
}

export const NoSnippet: Story = {
  args: { hit: { ...hit, snippet: null } },
}

export const Internal: Story = {
  args: { hit: { ...hit, visibility: 'internal' } },
}
