import React from 'react'
import { render, fireEvent, waitFor } from '@testing-library/react'
import { ScheduleListPage } from '../../src/pages/schedule-list/ScheduleListPage'

// Mock Taro路由
jest.mock('@tarojs/taro', () => ({
  useRouter: jest.fn(() => ({
    params: {
      startLocationId: '1',
      endLocationId: '2',
      date: '2025-10-18',
      vehicleType: 'bus',
      activityId: '101',
      routeType: 'departure'
    }
  })),
  navigateTo: jest.fn()
}))

// Mock API调用
jest.mock('../../src/api', () => ({
  routeClient: {
    search: {
      $get: jest.fn().mockResolvedValue({
        status: 200,
        json: jest.fn().mockResolvedValue([
          {
            id: 1,
            startLocation: { name: '北京市' },
            endLocation: { name: '上海市' },
            pickupPoint: '北京首都国际机场T3航站楼',
            dropoffPoint: '上海虹桥机场T2航站楼',
            departureTime: '2025-10-18T08:00:00Z',
            vehicleType: 'bus',
            price: 120,
            seatCount: 40,
            availableSeats: 15,
            routeType: 'departure',
            activities: [
              { id: 101, name: '上海音乐节' },
              { id: 102, name: '上海艺术展' }
            ]
          },
          {
            id: 2,
            startLocation: { name: '北京市' },
            endLocation: { name: '上海市' },
            pickupPoint: '北京南站',
            dropoffPoint: '上海南站',
            departureTime: '2025-10-18T14:00:00Z',
            vehicleType: 'business',
            price: 200,
            seatCount: 7,
            availableSeats: 0,
            routeType: 'departure',
            activities: [
              { id: 101, name: '上海音乐节' }
            ]
          },
          {
            id: 3,
            startLocation: { name: '北京市' },
            endLocation: { name: '上海市' },
            pickupPoint: '指定地点接送',
            dropoffPoint: '指定地点接送',
            departureTime: '2025-10-18T10:00:00Z',
            vehicleType: 'charter',
            price: 800,
            seatCount: 15,
            availableSeats: 15,
            routeType: 'departure',
            activities: [
              { id: 101, name: '上海音乐节' }
            ]
          }
        ])
      })
    }
  }
}))

describe('ScheduleListPage', () => {
  beforeEach(() => {
    jest.clearAllMocks()
  })

  it('应该正确渲染页面头部信息', async () => {
    const { getByText } = render(<ScheduleListPage />)

    await waitFor(() => {
      expect(getByText('上海音乐节')).toBeTruthy()
      expect(getByText('北京市 → 上海市')).toBeTruthy()
      expect(getByText('去程')).toBeTruthy()
    })
  })

  it('应该生成日期选项', async () => {
    const { getByText } = render(<ScheduleListPage />)

    await waitFor(() => {
      expect(getByText('选择出发日期')).toBeTruthy()
      // 验证生成了7天的日期选项
      const today = new Date().toISOString().split('T')[0]
      expect(getByText(today)).toBeTruthy()
    })
  })

  it('应该加载并显示班次列表', async () => {
    const { getByText } = render(<ScheduleListPage />)

    await waitFor(() => {
      expect(getByText('可选班次')).toBeTruthy()
      expect(getByText('(3个班次)')).toBeTruthy()
      expect(getByText('08:00')).toBeTruthy()
      expect(getByText('14:00')).toBeTruthy()
      expect(getByText('10:00')).toBeTruthy()
    })
  })

  it('应该正确显示班次信息', async () => {
    const { getByText } = render(<ScheduleListPage />)

    await waitFor(() => {
      // 验证大巴拼车班次
      expect(getByText('¥120/人')).toBeTruthy()
      expect(getByText('大巴拼车')).toBeTruthy()
      expect(getByText('北京首都国际机场T3航站楼')).toBeTruthy()
      expect(getByText('上海虹桥机场T2航站楼')).toBeTruthy()
      expect(getByText('剩余15/40座')).toBeTruthy()

      // 验证商务拼车班次
      expect(getByText('¥200/人')).toBeTruthy()
      expect(getByText('商务拼车')).toBeTruthy()

      // 验证包车班次
      expect(getByText('¥800/车')).toBeTruthy()
      expect(getByText('包车')).toBeTruthy()
      expect(getByText('可载15人')).toBeTruthy()
    })
  })

  it('应该处理日期选择', async () => {
    const { getByText } = render(<ScheduleListPage />)

    await waitFor(() => {
      const newDate = '2025-10-19'
      const dateOption = getByText(newDate)
      fireEvent.click(dateOption)

      // 验证日期状态更新
      expect(dateOption.className).toContain('border-blue-500')
    })
  })

  it('应该处理已售罄班次', async () => {
    const { getByText } = render(<ScheduleListPage />)

    await waitFor(() => {
      expect(getByText('已售罄')).toBeTruthy()
      // 验证已售罄按钮被禁用
      const soldOutButton = getByText('已售罄')
      expect(soldOutButton.getAttribute('disabled')).toBe('')
    })
  })

  it('应该处理预订操作', async () => {
    const { getByText } = render(<ScheduleListPage />)

    await waitFor(() => {
      const bookButton = getByText('立即购票')
      fireEvent.click(bookButton)

      // 验证预订逻辑被触发
      // 这里可以验证控制台输出或其他副作用
      expect(bookButton).toBeTruthy()
    })
  })

  it('应该正确格式化时间和价格', async () => {
    const { getByText } = render(<ScheduleListPage />)

    await waitFor(() => {
      // 验证时间格式化
      expect(getByText('08:00')).toBeTruthy()
      expect(getByText('14:00')).toBeTruthy()
      expect(getByText('10:00')).toBeTruthy()

      // 验证价格格式化
      expect(getByText('¥120/人')).toBeTruthy()
      expect(getByText('¥200/人')).toBeTruthy()
      expect(getByText('¥800/车')).toBeTruthy()
    })
  })

  it('应该显示车辆类型标签', async () => {
    const { getByText } = render(<ScheduleListPage />)

    await waitFor(() => {
      expect(getByText('拼车')).toBeTruthy()
      expect(getByText('包车')).toBeTruthy()
    })
  })

  it('应该显示车辆特色', async () => {
    const { getAllByText } = render(<ScheduleListPage />)

    await waitFor(() => {
      // 验证所有班次都显示特色标签
      const acTags = getAllByText('空调')
      const wifiTags = getAllByText('免费WiFi')
      expect(acTags.length).toBeGreaterThan(0)
      expect(wifiTags.length).toBeGreaterThan(0)
    })
  })

  it('应该处理空班次列表', async () => {
    // Mock 空数据
    const routeClient = require('../../src/api').routeClient
    routeClient.search.$get.mockResolvedValueOnce({
      status: 200,
      json: jest.fn().mockResolvedValue([])
    })

    const { getByText } = render(<ScheduleListPage />)

    await waitFor(() => {
      expect(getByText('暂无班次')).toBeTruthy()
      expect(getByText('请选择其他日期查看')).toBeTruthy()
    })
  })

  it('应该正确过滤包含指定活动的路线', async () => {
    const { getByText } = render(<ScheduleListPage />)

    await waitFor(() => {
      // 验证只显示包含活动ID 101的路线
      expect(getByText('上海音乐节')).toBeTruthy()
      // 不应该显示包含其他活动的路线
      // 这里假设测试数据中所有路线都包含活动101
    })
  })

  it('应该处理加载状态', () => {
    // Mock 延迟加载
    const routeClient = require('../../src/api').routeClient
    routeClient.search.$get.mockImplementationOnce(
      () => new Promise(resolve => setTimeout(resolve, 100))
    )

    const { getByText } = render(<ScheduleListPage />)

    expect(getByText('加载中...')).toBeTruthy()
  })

  it('应该处理路由参数缺失的情况', () => {
    // Mock 缺失参数
    const useRouter = require('@tarojs/taro').useRouter
    useRouter.mockReturnValueOnce({
      params: {}
    })

    const { getByText } = render(<ScheduleListPage />)

    // 验证页面仍然渲染
    expect(getByText('选择出发日期')).toBeTruthy()
  })
})