import { describe, it, expect, beforeEach } from 'vitest';
import { testClient } from 'hono/testing';
import { IntegrationTestDatabase, setupIntegrationDatabaseHooksWithEntities } from '@d8d/shared-test-util';
import { JWTUtil } from '@d8d/shared-utils';
import { UserEntity, Role } from '@d8d/user-module';
import { File } from '@d8d/file-module';
import { AreaEntity } from '@d8d/geo-areas';
import salaryRoutes from '../../src/routes/salary.routes';
import { SalaryLevel } from '../../src/entities/salary-level.entity';

// 设置集成测试钩子
setupIntegrationDatabaseHooksWithEntities([UserEntity, File, Role, AreaEntity, SalaryLevel])

describe('薪资管理API集成测试（简化版）', () => {
  let client: ReturnType<typeof testClient<typeof salaryRoutes>>;
  let testToken: string;
  let testUser: UserEntity;
  let testProvince: AreaEntity;
  let testCity: AreaEntity;

  beforeEach(async () => {
    // 创建测试客户端
    client = testClient(salaryRoutes);

    // 获取数据源
    const dataSource = await IntegrationTestDatabase.getDataSource();

    // 创建测试用户
    const userRepository = dataSource.getRepository(UserEntity);
    testUser = userRepository.create({
      username: `test_user_${Date.now()}`,
      password: 'test_password',
      nickname: '测试用户',
      registrationSource: 'web'
    });
    await userRepository.save(testUser);

    // 生成测试用户的token
    testToken = JWTUtil.generateToken({
      id: testUser.id,
      username: testUser.username,
      roles: [{name:'user'}]
    });

    // 创建测试区域数据
    const areaRepository = dataSource.getRepository(AreaEntity);

    // 创建省份
    testProvince = areaRepository.create({
      code: '110000',
      name: '北京市',
      level: 1,
      parentId: null
    });
    await areaRepository.save(testProvince);

    // 创建城市
    testCity = areaRepository.create({
      code: '110100',
      name: '北京市',
      level: 2,
      parentId: testProvince.id
    });
    await areaRepository.save(testCity);
  });

  describe('基本功能测试', () => {
    it('应该成功创建薪资水平', async () => {
      const createData = {
        provinceId: testProvince.id,
        cityId: testCity.id,
        basicSalary: 5000.00
      };

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

      expect(response.status).toBe(200);
      const result = await response.json();

      // 检查返回结果是否有错误码
      if ('code' in result) {
        // 如果有错误码，测试失败
        expect(result.code).not.toBe(400);
        expect(result.code).not.toBe(401);
      } else {
        // 成功情况
        expect(result).toHaveProperty('id');
        expect(result.provinceId).toBe(testProvince.id);
        expect(result.cityId).toBe(testCity.id);
        expect(result.basicSalary).toBe(5000.00);
      }
    });

    it('应该要求认证', async () => {
      const createData = {
        provinceId: testProvince.id,
        cityId: testCity.id,
        basicSalary: 5000.00
      };

      const response = await client.create.$post({
        json: createData
        // 不提供Authorization header
      });

      expect(response.status).toBe(401);
    });
  });
});