import { render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { ModelTable } from '@/pages/Providers/ModelTable'
import type { Model } from '@/types'

const mockModels: Model[] = [
  {
    id: 'model-1',
    providerId: 'openai',
    modelName: 'gpt-4o',
    enabled: true,
    createdAt: '2024-01-01T00:00:00Z',
    unifiedId: 'openai/gpt-4o',
  },
  {
    id: 'model-2',
    providerId: 'openai',
    modelName: 'gpt-3.5-turbo',
    enabled: false,
    createdAt: '2024-01-02T00:00:00Z',
    unifiedId: 'openai/gpt-3.5-turbo',
  },
]

const mockMutate = vi.fn()

vi.mock('@/hooks/useModels', () => ({
  useModels: vi.fn((providerId: string) => {
    if (providerId === 'openai') {
      return { data: mockModels, isLoading: false }
    }
    return { data: [], isLoading: false }
  }),
  useDeleteModel: vi.fn(() => ({ mutate: mockMutate })),
}))

const defaultProps = {
  providerId: 'openai',
  onAdd: vi.fn(),
  onEdit: vi.fn(),
}

describe('ModelTable', () => {
  beforeEach(() => {
    mockMutate.mockClear()
  })

  it('renders model list with unified ID and model name', () => {
    render(<ModelTable {...defaultProps} />)

    expect(screen.getByText(/关联模型/)).toBeInTheDocument()
    expect(screen.getByText('openai/gpt-4o')).toBeInTheDocument()
    expect(screen.getByText('openai/gpt-3.5-turbo')).toBeInTheDocument()
    expect(screen.getByText('gpt-4o')).toBeInTheDocument()
    expect(screen.getByText('gpt-3.5-turbo')).toBeInTheDocument()
  })

  it('renders status tags correctly', () => {
    render(<ModelTable {...defaultProps} />)

    const enabledTags = screen.getAllByText('启用')
    const disabledTags = screen.getAllByText('禁用')
    expect(enabledTags.length).toBeGreaterThanOrEqual(1)
    expect(disabledTags.length).toBeGreaterThanOrEqual(1)
  })

  it('calls onAdd when clicking "添加模型" button', async () => {
    const user = userEvent.setup()
    const onAdd = vi.fn()
    render(<ModelTable {...defaultProps} onAdd={onAdd} />)

    await user.click(screen.getByRole('button', { name: '添加模型' }))
    expect(onAdd).toHaveBeenCalledTimes(1)
  })

  it('calls onEdit with correct model when clicking "编辑"', async () => {
    const user = userEvent.setup()
    const onEdit = vi.fn()
    render(<ModelTable {...defaultProps} onEdit={onEdit} />)

    const editButtons = screen.getAllByRole('button', { name: /编 ?辑/ })
    await user.click(editButtons[0])

    expect(onEdit).toHaveBeenCalledTimes(1)
    expect(onEdit).toHaveBeenCalledWith(mockModels[0])
  })

  it('calls deleteModel.mutate with correct model ID when delete is confirmed', async () => {
    const user = userEvent.setup()

    render(<ModelTable {...defaultProps} />)

    // Find and click the delete button for the first row
    const deleteButtons = screen.getAllByRole('button', { name: '删除' })
    await user.click(deleteButtons[0])

    // TDesign Popconfirm renders confirmation popup with "确定" button
    const confirmButton = await screen.findByRole('button', { name: '确定' })
    await user.click(confirmButton)

    // Assert that deleteModel.mutate was called with the correct model ID
    expect(mockMutate).toHaveBeenCalledTimes(1)
    expect(mockMutate).toHaveBeenCalledWith('model-1')
  }, 10000)

  it('shows custom empty text when models list is empty', () => {
    render(<ModelTable providerId='anthropic' />)
    expect(screen.getByText('暂无模型，点击上方按钮添加')).toBeInTheDocument()
  })

  it('does not render add button when onAdd is not provided', () => {
    render(<ModelTable providerId='openai' />)

    expect(screen.queryByRole('button', { name: '添加模型' })).not.toBeInTheDocument()
  })

  it('does not render edit button when onEdit is not provided', () => {
    render(<ModelTable providerId='openai' onAdd={vi.fn()} />)

    expect(screen.queryByRole('button', { name: /编 ?辑/ })).not.toBeInTheDocument()
  })
})
