// ============================================================
// AgentTemplates 页面冒烟测试
// ============================================================

import { describe, it, expect, beforeEach, afterEach } from 'vitest'
import { render, screen, waitFor } from '@testing-library/react'
import { http, HttpResponse } from 'msw'
import { setupServer } from 'msw/node'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'

import AgentTemplates from '@/pages/AgentTemplates'

// ── Mock data ────────────────────────────────────────────────

const mockTemplates = {
  items: [
    {
      id: 'tmpl-001',
      name: 'Medical Assistant',
      description: 'AI health assistant',
      category: 'assistant',
      source: 'builtin' as const,
      model: 'gpt-4o',
      system_prompt: 'You are a medical assistant.',
      tools: ['web_search'],
      capabilities: ['conversation'],
      temperature: 0.7,
      max_tokens: 4096,
      visibility: 'public' as const,
      status: 'active' as const,
      current_version: 2,
      created_at: '2026-01-10T00:00:00Z',
      updated_at: '2026-03-20T00:00:00Z',
      soul_content: null,
      scenarios: ['healthcare'],
      welcome_message: 'Hello!',
      quick_commands: [],
      personality: 'professional',
      communication_style: null,
      emoji: 'hospital',
      version: 2,
      source_id: 'medical-v1',
    },
    {
      id: 'tmpl-002',
      name: 'Code Helper',
      description: 'Programming assistant',
      category: 'tool',
      source: 'custom' as const,
      model: null,
      system_prompt: null,
      tools: [],
      capabilities: [],
      temperature: null,
      max_tokens: null,
      visibility: 'team' as const,
      status: 'active' as const,
      current_version: 1,
      created_at: '2026-02-01T00:00:00Z',
      updated_at: '2026-02-01T00:00:00Z',
      soul_content: null,
      scenarios: [],
      welcome_message: null,
      quick_commands: [],
      personality: null,
      communication_style: null,
      emoji: null,
      version: 1,
      source_id: null,
    },
  ],
  total: 2,
  page: 1,
  page_size: 20,
}

// ── MSW server ───────────────────────────────────────────────

const server = setupServer()

beforeEach(() => {
  server.listen({ onUnhandledRequest: 'bypass' })
})

afterEach(() => {
  server.close()
})

// ── Helper: render with QueryClient ──────────────────────────

function renderWithProviders(ui: React.ReactElement) {
  const queryClient = new QueryClient({
    defaultOptions: {
      queries: { retry: false },
    },
  })
  return render(
    <QueryClientProvider client={queryClient}>
      {ui}
    </QueryClientProvider>,
  )
}

// ── Tests ────────────────────────────────────────────────────

describe('AgentTemplates page', () => {
  it('renders template names in the table', async () => {
    server.use(
      http.get('*/api/v1/agent-templates', () => {
        return HttpResponse.json(mockTemplates)
      }),
    )

    renderWithProviders(<AgentTemplates />)

    // Wait for data to load and template names to appear
    await waitFor(() => {
      expect(screen.getByText('Medical Assistant')).toBeInTheDocument()
    })
    expect(screen.getByText('Code Helper')).toBeInTheDocument()
  })

  it('renders template categories', async () => {
    server.use(
      http.get('*/api/v1/agent-templates', () => {
        return HttpResponse.json(mockTemplates)
      }),
    )

    renderWithProviders(<AgentTemplates />)

    await waitFor(() => {
      expect(screen.getByText('assistant')).toBeInTheDocument()
    })
    expect(screen.getByText('tool')).toBeInTheDocument()
  })
})
