import { describe, it, expect, beforeEach } from 'vitest';
import { testClient } from 'hono/testing';
import {
  IntegrationTestDatabase,
  setupIntegrationDatabaseHooks,
  TestDataFactory
} from '../utils/integration-test-db';
import { IntegrationTestAssertions } from '../utils/integration-test-utils';
import { userRoutes } from '../../src/api';
import { AuthService } from '@d8d/auth-module';
import { UserService } from '@d8d/user-module';


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

describe('用户API集成测试 (使用hono/testing)', () => {
  let client: ReturnType<typeof testClient<typeof userRoutes>>['api']['v1'];
  let testToken: string;

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

    // 创建测试用户并生成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 userData = {
        username: 'testuser_create',
        email: 'testcreate@example.com',
        password: 'TestPassword123!',
        name: 'Test User',
        phone: '13800138000'
      };

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

      // 断言响应
      expect(response.status).toBe(201);
      if (response.status === 201) {
        const responseData = await response.json();
        expect(responseData).toHaveProperty('id');
        expect(responseData.username).toBe(userData.username);
        expect(responseData.email).toBe(userData.email);
        expect(responseData.name).toBe(userData.name);

        // 断言数据库中存在用户
        await IntegrationTestAssertions.expectUserToExist(userData.username);
      }
    });

    it('应该拒绝创建重复用户名的用户', async () => {
      const dataSource = await IntegrationTestDatabase.getDataSource();
      if (!dataSource) throw new Error('Database not initialized');

      // 先创建一个用户
      await TestDataFactory.createTestUser(dataSource, {
        username: 'duplicate_user'
      });

      // 尝试创建相同用户名的用户
      const userData = {
        username: 'duplicate_user',
        email: 'different@example.com',
        password: 'TestPassword123!',
        name: 'Test User'
      };

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

      // 应该返回错误
      expect(response.status).toBe(500);
      if (response.status === 500) {
        const responseData = await response.json();
        expect(responseData.message).toContain('duplicate key');
      }
    });

    it('应该拒绝创建无效邮箱的用户', async () => {
      const userData = {
        username: 'testuser_invalid_email',
        email: 'invalid-email',
        password: 'TestPassword123!',
        name: 'Test User'
      };

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

      // 应该返回验证错误或服务器错误
      // 根据实际实现，可能是400验证错误或500服务器错误
      expect([400, 500]).toContain(response.status);
      if (response.status === 400) {
        const responseData = await response.json();
        // 检查是否有code属性
        if (responseData.code !== undefined) {
          expect(responseData.code).toBe(400);
        }
        // 检查是否有message属性
        if (responseData.message !== undefined) {
          expect(typeof responseData.message).toBe('string');
        }
      } else if (response.status === 500) {
        const responseData = await response.json();
        expect(responseData.message).toBeDefined();
      }
    });
  });

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

      // 创建几个测试用户
      await TestDataFactory.createTestUser(dataSource, { username: 'user1' });
      await TestDataFactory.createTestUser(dataSource, { username: 'user2' });

      const response = await client.users.$get({
        query: {}
      },
      {
        headers: {
          'Authorization': `Bearer ${testToken}`
        }
      });

      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 testUser = await TestDataFactory.createTestUser(dataSource, {
        username: 'testuser_detail'
      });

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

      expect(response.status).toBe(200);
      if (response.status === 200) {
        const responseData = await response.json();
        expect(responseData.id).toBe(testUser.id);
        expect(responseData.username).toBe(testUser.username);
        expect(responseData.email).toBe(testUser.email);
      }
    });

    it('应该返回404当用户不存在时', async () => {
      const response = await client.users[':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 testUser = await TestDataFactory.createTestUser(dataSource, {
        username: 'testuser_update'
      });

      const updateData = {
        name: 'Updated Name',
        email: 'updated@example.com'
      };

      const response = await client.users[':id'].$put({
        param: { id: testUser.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.email).toBe(updateData.email);
      }

      // 验证数据库中的更新
      const getResponse = await client.users[':id'].$get({
        param: { id: testUser.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);
      }else{
        const getResponseData = await getResponse.json();
        process.stderr.write('message:'+ getResponseData.message +"\n");
      }
    });

    it('应该返回404当更新不存在的用户时', async () => {
      const updateData = {
        name: 'Updated Name',
        email: 'updated@example.com'
      };

      const response = await client.users[':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 testUser = await TestDataFactory.createTestUser(dataSource, {
        username: 'testuser_delete'
      });

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

      IntegrationTestAssertions.expectStatus(response, 204);

      // 验证用户已从数据库中删除
      await IntegrationTestAssertions.expectUserNotToExist('testuser_delete');

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

    it('应该返回404当删除不存在的用户时', async () => {
      const response = await client.users[':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.createTestUser(dataSource, { username: 'search_user_1', email: 'search1@example.com' });
      await TestDataFactory.createTestUser(dataSource, { username: 'search_user_2', email: 'search2@example.com' });
      await TestDataFactory.createTestUser(dataSource, { username: 'other_user', email: 'other@example.com' });

      const response = await client.users.$get({
        query: { keyword: 'search_user' }
      },
      {
        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 usernames = responseData.data.map((user: any) => user.username);
        expect(usernames).toContain('search_user_1');
        expect(usernames).toContain('search_user_2');
        expect(usernames).not.toContain('other_user');
      }
    });

    it('应该能够按邮箱搜索用户', async () => {
      const dataSource = await IntegrationTestDatabase.getDataSource();
      if (!dataSource) throw new Error('Database not initialized');

      await TestDataFactory.createTestUser(dataSource, { username: 'user_email_1', email: 'test.email1@example.com' });
      await TestDataFactory.createTestUser(dataSource, { username: 'user_email_2', email: 'test.email2@example.com' });

      const response = await client.users.$get({
        query: { keyword: 'test.email' }
      },
      {
        headers: {
          'Authorization': `Bearer ${testToken}`
        }
      });

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

        const emails = responseData.data.map((user: any) => user.email);
        expect(emails).toContain('test.email1@example.com');
        expect(emails).toContain('test.email2@example.com');
      }
    });
  });

  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.createTestUser(dataSource, {
          username: `perf_user_${i}`,
          email: `perf${i}@example.com`
        });
      }

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

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