import { describe, it, expect, beforeEach } from 'vitest';
import { testClient } from 'hono/testing';
import {
  IntegrationTestDatabase,
  setupIntegrationDatabaseHooks,
  TestDataFactory
} from '~/utils/server/integration-test-db';
import { IntegrationTestAssertions } from '~/utils/server/integration-test-utils';
import { adminPassengersRoutesExport } from '@d8d/server/api';
import { AuthService } from '@d8d/server/modules/auth/auth.service';
import { UserService } from '@d8d/server/modules/users/user.service';
import { IdType } from '@d8d/server/modules/passengers/passenger.schema';

// 设置集成测试钩子
setupIntegrationDatabaseHooks()

describe('乘客管理API集成测试', () => {
  let client: ReturnType<typeof testClient<typeof adminPassengersRoutesExport>>['api']['v1']['admin'];
  let testToken: string;

  beforeEach(async () => {
    // 创建测试客户端
    client = testClient(adminPassengersRoutesExport).api.v1.admin;

    // 创建测试用户并生成token
    const dataSource = await IntegrationTestDatabase.getDataSource();

    const userService = new UserService(dataSource);
    const authService = new AuthService(userService);

    // 确保admin用户存在
    const user = await authService.ensureAdminExists();

    // 生成admin用户的token
    testToken = authService.generateToken(user);
  });

  describe('乘客创建测试', () => {
    it('应该成功创建乘客', async () => {
      const dataSource = await IntegrationTestDatabase.getDataSource();
      const testUser = await TestDataFactory.createTestUser(dataSource);

      const passengerData = {
        userId: testUser.id,
        name: '测试乘客',
        idType: IdType.ID_CARD,
        idNumber: '110101199001011234',
        phone: '13812345678',
        isDefault: false
      };

      const response = await client.passengers.$post({
        json: passengerData,
      },
      {
        headers: {
          'Authorization': `Bearer ${testToken}`
        }
      });

      // 断言响应
      expect(response.status).toBe(201);
      if (response.status === 201) {
        const responseData = await response.json();
        expect(responseData).toHaveProperty('id');
        expect(responseData.name).toBe(passengerData.name);
        expect(responseData.idType).toBe(passengerData.idType);
        expect(responseData.idNumber).toBe(passengerData.idNumber);
        expect(responseData.phone).toBe(passengerData.phone);
        expect(responseData.isDefault).toBe(passengerData.isDefault);

        // 断言数据库中存在乘客
        await IntegrationTestAssertions.expectPassengerToExist(responseData.id);
      }
    });

    it('应该成功创建默认乘客', async () => {
      const dataSource = await IntegrationTestDatabase.getDataSource();
      const testUser = await TestDataFactory.createTestUser(dataSource);

      const passengerData = {
        userId: testUser.id,
        name: '默认乘客',
        idType: IdType.PASSPORT,
        idNumber: 'E12345678',
        phone: '13987654321',
        isDefault: true
      };

      const response = await client.passengers.$post({
        json: passengerData,
      },
      {
        headers: {
          'Authorization': `Bearer ${testToken}`
        }
      });

      expect(response.status).toBe(201);
      if (response.status === 201) {
        const responseData = await response.json();
        expect(responseData.isDefault).toBe(true);
      }
    });

    it('应该拒绝创建无效证件类型的乘客', async () => {
      const dataSource = await IntegrationTestDatabase.getDataSource();
      const testUser = await TestDataFactory.createTestUser(dataSource);

      const passengerData = {
        userId: testUser.id,
        name: '测试乘客',
        idType: 'invalid_type' as any, // 无效类型
        idNumber: '110101199001011234',
        phone: '13812345678',
        isDefault: false
      };

      const response = await client.passengers.$post({
        json: passengerData,
      },
      {
        headers: {
          'Authorization': `Bearer ${testToken}`
        }
      });

      // 应该返回验证错误
      expect([400, 500]).toContain(response.status);
    });
  });

  describe('乘客读取测试', () => {
    it('应该成功获取乘客列表', async () => {
      const dataSource = await IntegrationTestDatabase.getDataSource();
      if (!dataSource) throw new Error('Database not initialized');

      // 创建几个测试乘客
      await TestDataFactory.createTestPassenger(dataSource, { name: '乘客1' });
      await TestDataFactory.createTestPassenger(dataSource, { name: '乘客2' });

      const response = await client.passengers.$get({
        query: { page: 1, pageSize: 10 }
      },
      {
        headers: {
          'Authorization': `Bearer ${testToken}`
        }
      });

      if (response.status !== 200) {
        const errorData = await response.json() as any;
        console.debug('获取乘客列表失败:', errorData);
        if (errorData.errors) {
          console.debug('详细验证错误:', errorData.errors.map((err: any) => ({
            path: err.path,
            message: err.message,
            expected: err.expected
          })));
        }
      }
      expect(response.status).toBe(200);
      if (response.status === 200) {
        const responseData = await response.json();
        expect(Array.isArray(responseData.data)).toBe(true);
        expect(responseData.data.length).toBeGreaterThanOrEqual(2);
      }
    });

    it('应该成功获取单个乘客详情', async () => {
      const dataSource = await IntegrationTestDatabase.getDataSource();
      if (!dataSource) throw new Error('Database not initialized');

      const testPassenger = await TestDataFactory.createTestPassenger(dataSource, {
        name: '测试乘客详情'
      });

      const response = await client.passengers[':id'].$get({
        param: { id: testPassenger.id }
      },
      {
        headers: {
          'Authorization': `Bearer ${testToken}`
        }
      });

      expect(response.status).toBe(200);
      if (response.status === 200) {
        const responseData = await response.json();
        expect(responseData.id).toBe(testPassenger.id);
        expect(responseData.name).toBe(testPassenger.name);
        expect(responseData.idType).toBe(testPassenger.idType);
        expect(responseData.idNumber).toBe(testPassenger.idNumber);
        expect(responseData.phone).toBe(testPassenger.phone);
      }
    });

    it('应该返回404当乘客不存在时', async () => {
      const response = await client.passengers[':id'].$get({
        param: { id: 999999 }
      },
      {
        headers: {
          'Authorization': `Bearer ${testToken}`
        }
      });

      expect(response.status).toBe(404);
      if (response.status === 404) {
        const responseData = await response.json();
        expect(responseData.message).toContain('资源不存在');
      }
    });
  });

  describe('乘客更新测试', () => {
    it('应该成功更新乘客信息', async () => {
      const dataSource = await IntegrationTestDatabase.getDataSource();
      if (!dataSource) throw new Error('Database not initialized');

      const testPassenger = await TestDataFactory.createTestPassenger(dataSource, {
        name: '测试乘客更新'
      });

      const updateData = {
        name: '更新后的乘客名称',
        phone: '13987654321',
        isDefault: true
      };

      const response = await client.passengers[':id'].$put({
        param: { id: testPassenger.id },
        json: updateData
      },
      {
        headers: {
          'Authorization': `Bearer ${testToken}`
        }
      });

      expect(response.status).toBe(200);
      if (response.status === 200) {
        const responseData = await response.json();
        expect(responseData.name).toBe(updateData.name);
        expect(responseData.phone).toBe(updateData.phone);
        expect(responseData.isDefault).toBe(updateData.isDefault);
      }

      // 验证数据库中的更新
      const getResponse = await client.passengers[':id'].$get({
        param: { id: testPassenger.id }
      },
      {
        headers: {
          'Authorization': `Bearer ${testToken}`
        }
      });
      if (getResponse.status === 200) {
        expect(getResponse.status).toBe(200);
        const getResponseData = await getResponse.json();
        expect(getResponseData.name).toBe(updateData.name);
        expect(getResponseData.phone).toBe(updateData.phone);
      }
    });

    it('应该返回404当更新不存在的乘客时', async () => {
      const updateData = {
        name: '更新后的名称'
      };

      const response = await client.passengers[':id'].$put({
        param: { id: 999999 },
        json: updateData
      },
      {
        headers: {
          'Authorization': `Bearer ${testToken}`
        }
      });

      expect(response.status).toBe(404);
      if (response.status === 404) {
        const responseData = await response.json();
        expect(responseData.message).toContain('资源不存在');
      }
    });
  });

  describe('乘客删除测试', () => {
    it('应该成功删除乘客', async () => {
      const dataSource = await IntegrationTestDatabase.getDataSource();
      if (!dataSource) throw new Error('Database not initialized');

      const testPassenger = await TestDataFactory.createTestPassenger(dataSource, {
        name: '测试乘客删除'
      });

      const response = await client.passengers[':id'].$delete({
        param: { id: testPassenger.id }
      },
      {
        headers: {
          'Authorization': `Bearer ${testToken}`
        }
      });

      IntegrationTestAssertions.expectStatus(response, 204);

      // 验证乘客已从数据库中删除
      await IntegrationTestAssertions.expectPassengerNotToExist(testPassenger.id);

      // 验证再次获取乘客返回404
      const getResponse = await client.passengers[':id'].$get({
        param: { id: testPassenger.id }
      },
      {
        headers: {
          'Authorization': `Bearer ${testToken}`
        }
      });
      IntegrationTestAssertions.expectStatus(getResponse, 404);
    });

    it('应该返回404当删除不存在的乘客时', async () => {
      const response = await client.passengers[':id'].$delete({
        param: { id: 999999 }
      },
      {
        headers: {
          'Authorization': `Bearer ${testToken}`
        }
      });

      IntegrationTestAssertions.expectStatus(response, 404);
      if (response.status === 404) {
        const responseData = await response.json();
        expect(responseData.message).toContain('资源不存在');
      }
    });
  });

  describe('乘客搜索测试', () => {
    it('应该能够按乘客姓名搜索乘客', async () => {
      const dataSource = await IntegrationTestDatabase.getDataSource();
      if (!dataSource) throw new Error('Database not initialized');

      await TestDataFactory.createTestPassenger(dataSource, { name: '搜索乘客1', phone: '13811111111' });
      await TestDataFactory.createTestPassenger(dataSource, { name: '搜索乘客2', phone: '13822222222' });
      await TestDataFactory.createTestPassenger(dataSource, { name: '其他乘客', phone: '13833333333' });

      const response = await client.passengers.$get({
        query: { keyword: '搜索乘客', page: 1, pageSize: 10 }
      },
      {
        headers: {
          'Authorization': `Bearer ${testToken}`
        }
      });

      IntegrationTestAssertions.expectStatus(response, 200);
      if (response.status === 200) {
        const responseData = await response.json();
        expect(Array.isArray(responseData.data)).toBe(true);
        expect(responseData.data.length).toBe(2);

        // 验证搜索结果包含正确的乘客
        const names = responseData.data.map((passenger) => passenger.name);
        expect(names).toContain('搜索乘客1');
        expect(names).toContain('搜索乘客2');
        expect(names).not.toContain('其他乘客');
      }
    });

    it('应该能够按手机号搜索乘客', async () => {
      const dataSource = await IntegrationTestDatabase.getDataSource();
      if (!dataSource) throw new Error('Database not initialized');

      await TestDataFactory.createTestPassenger(dataSource, { name: '乘客1', phone: '13811112222' });
      await TestDataFactory.createTestPassenger(dataSource, { name: '乘客2', phone: '13811113333' });

      const response = await client.passengers.$get({
        query: { keyword: '1381111', page: 1, pageSize: 10 }
      },
      {
        headers: {
          'Authorization': `Bearer ${testToken}`
        }
      });

      IntegrationTestAssertions.expectStatus(response, 200);
      if (response.status === 200) {
        const responseData = await response.json();
        expect(responseData.data.length).toBe(2);

        const phones = responseData.data.map((passenger) => passenger.phone);
        expect(phones).toContain('13811112222');
        expect(phones).toContain('13811113333');
      }
    });

    it('应该能够按证件号码搜索乘客', async () => {
      const dataSource = await IntegrationTestDatabase.getDataSource();
      if (!dataSource) throw new Error('Database not initialized');

      await TestDataFactory.createTestPassenger(dataSource, {
        name: '乘客1',
        idNumber: '110101199001011111'
      });
      await TestDataFactory.createTestPassenger(dataSource, {
        name: '乘客2',
        idNumber: '110101199001012222'
      });

      const response = await client.passengers.$get({
        query: { keyword: '11010119900101', page: 1, pageSize: 10 }
      },
      {
        headers: {
          'Authorization': `Bearer ${testToken}`
        }
      });

      IntegrationTestAssertions.expectStatus(response, 200);
      if (response.status === 200) {
        const responseData = await response.json();
        expect(responseData.data.length).toBe(2);

        const idNumbers = responseData.data.map((passenger) => passenger.idNumber);
        expect(idNumbers).toContain('110101199001011111');
        expect(idNumbers).toContain('110101199001012222');
      }
    });

    it('应该能够按用户ID筛选乘客', async () => {
      const dataSource = await IntegrationTestDatabase.getDataSource();
      if (!dataSource) throw new Error('Database not initialized');

      const testUser1 = await TestDataFactory.createTestUser(dataSource);
      const testUser2 = await TestDataFactory.createTestUser(dataSource);

      await TestDataFactory.createTestPassenger(dataSource, {
        name: '用户1的乘客1',
        userId: testUser1.id
      });
      await TestDataFactory.createTestPassenger(dataSource, {
        name: '用户1的乘客2',
        userId: testUser1.id
      });
      await TestDataFactory.createTestPassenger(dataSource, {
        name: '用户2的乘客',
        userId: testUser2.id
      });

      const response = await client.passengers.$get({
        query: { filters: JSON.stringify({ userId: testUser1.id }), page: 1, pageSize: 10 }
      },
      {
        headers: {
          'Authorization': `Bearer ${testToken}`
        }
      });

      IntegrationTestAssertions.expectStatus(response, 200);
      if (response.status === 200) {
        const responseData = await response.json();
        expect(responseData.data.length).toBe(2);

        const names = responseData.data.map((passenger) => passenger.name);
        expect(names).toContain('用户1的乘客1');
        expect(names).toContain('用户1的乘客2');
        expect(names).not.toContain('用户2的乘客');
      }
    });
  });

  describe('性能测试', () => {
    it('乘客列表查询响应时间应小于200ms', async () => {
      const dataSource = await IntegrationTestDatabase.getDataSource();
      if (!dataSource) throw new Error('Database not initialized');

      // 创建一些测试数据
      for (let i = 0; i < 10; i++) {
        await TestDataFactory.createTestPassenger(dataSource, {
          name: `性能测试乘客_${i}`,
          phone: `138${i.toString().padStart(8, '0')}`
        });
      }

      const startTime = Date.now();
      const response = await client.passengers.$get({
        query: { page: 1, pageSize: 10 }
      },
      {
        headers: {
          'Authorization': `Bearer ${testToken}`
        }
      });
      const endTime = Date.now();
      const responseTime = endTime - startTime;

      IntegrationTestAssertions.expectStatus(response, 200);
      expect(responseTime).toBeLessThan(200); // 响应时间应小于200ms
    });
  });
});