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 { DeliveryAddress } from '@d8d/delivery-address-module';
import { AreaEntity } from '@d8d/geo-areas';
import { Merchant } from '@d8d/merchant-module';
import { Supplier } from '@d8d/supplier-module';
import { File } from '@d8d/file-module';
import adminOrderRoutes from '../../src/routes/admin/orders';
import { Order } from '../../src/entities';

// 设置集成测试钩子
setupIntegrationDatabaseHooksWithEntities([
  UserEntity, Role, Order, DeliveryAddress, Merchant, Supplier, File, AreaEntity
])

describe('管理员订单管理API集成测试', () => {
  let client: ReturnType<typeof testClient<typeof adminOrderRoutes>>;
  let adminToken: string;
  let testUser: UserEntity;
  let testAdmin: UserEntity;
  let testDeliveryAddress: DeliveryAddress;
  let testMerchant: Merchant;
  let testSupplier: Supplier;
  let testProvince: AreaEntity;
  let testCity: AreaEntity;
  let testDistrict: AreaEntity;
  let testTown: AreaEntity;

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

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

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

    // 创建测试管理员用户
    testAdmin = userRepository.create({
      username: `test_admin_${Math.floor(Math.random() * 100000)}`,
      password: 'admin_password',
      nickname: '测试管理员',
      registrationSource: 'web'
    });
    await userRepository.save(testAdmin);

    // 生成测试管理员的token
    adminToken = JWTUtil.generateToken({
      id: testAdmin.id,
      username: testAdmin.username,
      roles: [{name:'admin'}]
    });

    // 创建测试地区数据
    const areaRepository = dataSource.getRepository(AreaEntity);
    testProvince = areaRepository.create({
      name: '广东省',
      level: 1,
      code: '44',
      state: 1
    });
    await areaRepository.save(testProvince);

    testCity = areaRepository.create({
      name: '深圳市',
      level: 2,
      code: '4403',
      state: 1
    });
    await areaRepository.save(testCity);

    testDistrict = areaRepository.create({
      name: '南山区',
      level: 3,
      code: '440305',
      state: 1
    });
    await areaRepository.save(testDistrict);

    testTown = areaRepository.create({
      name: '粤海街道',
      level: 4,
      code: '440305001',
      state: 1
    });
    await areaRepository.save(testTown);

    // 创建测试配送地址
    const deliveryAddressRepository = dataSource.getRepository(DeliveryAddress);
    testDeliveryAddress = deliveryAddressRepository.create({
      userId: testUser.id,
      name: '收货人姓名',
      phone: '13800138000',
      receiverProvince: testProvince.id,
      receiverCity: testCity.id,
      receiverDistrict: testDistrict.id,
      receiverTown: testTown.id,
      address: '测试地址',
      isDefault: 1,
      state: 1,
      createdBy: testUser.id
    });
    await deliveryAddressRepository.save(testDeliveryAddress);

    // 创建测试商户
    const merchantRepository = dataSource.getRepository(Merchant);
    testMerchant = merchantRepository.create({
      name: '测试商户',
      username: `test_merchant_${Math.floor(Math.random() * 100000)}`,
      password: 'password123',
      phone: '13800138001',
      realname: '测试商户',
      state: 1,
      createdBy: testUser.id
    });
    await merchantRepository.save(testMerchant);

    // 创建测试供应商
    const supplierRepository = dataSource.getRepository(Supplier);
    testSupplier = supplierRepository.create({
      name: '测试供应商',
      username: `test_supplier_${Math.floor(Math.random() * 100000)}`,
      password: 'password123',
      phone: '13800138002',
      realname: '测试供应商',
      state: 1,
      createdBy: testUser.id
    });
    await supplierRepository.save(testSupplier);
  });

  describe('GET /orders', () => {
    it('应该返回所有订单列表', async () => {
      // 创建多个用户的订单
      const dataSource = await IntegrationTestDatabase.getDataSource();
      const orderRepository = dataSource.getRepository(Order);

      const userOrder1 = orderRepository.create({
        orderNo: `ORDER_${Math.floor(Math.random() * 100000)}`,
        userId: testUser.id,
        amount: 100.00,
        costAmount: 80.00,
        payAmount: 100.00,
        orderType: 1,
        payType: 1,
        payState: 2,
        state: 0,
        addressId: testDeliveryAddress.id,
        merchantId: testMerchant.id,
        supplierId: testSupplier.id,
        recevierProvince: testProvince.id,
        recevierCity: testCity.id,
        recevierDistrict: testDistrict.id,
        recevierTown: testTown.id,
        createdBy: testUser.id
      });
      await orderRepository.save(userOrder1);

      const userOrder2 = orderRepository.create({
        orderNo: `ORDER_${Math.floor(Math.random() * 100000)}`,
        userId: testAdmin.id,
        amount: 200.00,
        costAmount: 160.00,
        payAmount: 200.00,
        orderType: 1,
        payType: 1,
        payState: 2,
        state: 1,
        addressId: testDeliveryAddress.id,
        merchantId: testMerchant.id,
        supplierId: testSupplier.id,
        recevierProvince: testProvince.id,
        recevierCity: testCity.id,
        recevierDistrict: testDistrict.id,
        recevierTown: testTown.id,
        createdBy: testAdmin.id
      });
      await orderRepository.save(userOrder2);

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

      console.debug('管理员订单列表响应状态:', response.status);
      expect(response.status).toBe(200);

      if (response.status === 200) {
        const data = await response.json();
        expect(data).toHaveProperty('data');
        expect(Array.isArray(data.data)).toBe(true);

        // 验证返回所有用户的订单（管理员可以访问所有数据）
        const userOrderCount = data.data.filter((order: any) => order.userId === testUser.id).length;
        const adminOrderCount = data.data.filter((order: any) => order.userId === testAdmin.id).length;

        expect(userOrderCount).toBeGreaterThan(0);
        expect(adminOrderCount).toBeGreaterThan(0);
      }
    });

    it('应该拒绝未认证用户的访问', async () => {
      const response = await client.index.$get({
        query: {}
      });
      expect(response.status).toBe(401);
    });
  });

  describe('POST /orders', () => {
    it('应该成功创建订单并可以指定权限', async () => {
      const createData = {
        orderNo: `ORDER_${Math.floor(Math.random() * 100000)}`,
        userId: testUser.id,
        amount: 150.00,
        costAmount: 120.00,
        payAmount: 150.00,
        orderType: 1,
        payType: 1,
        payState: 0,
        state: 0,
        addressId: testDeliveryAddress.id,
        merchantId: testMerchant.id,
        supplierId: testSupplier.id
        // createdBy 由认证中间件自动设置
      };

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

      console.debug('管理员创建订单响应状态:', response.status);
      if (response.status !== 201) {
        const errorData = await response.json();
        console.debug('管理员创建订单错误响应:', errorData);
      }
      expect(response.status).toBe(201);

      if (response.status === 201) {
        const data = await response.json();
        expect(data).toHaveProperty('id');
        expect(data.orderNo).toBe(createData.orderNo);
        expect(parseFloat(data.amount)).toBe(createData.amount);
        expect(data.userId).toBe(createData.userId); // 验证可以指定用户
        expect(data.createdBy).toBe(testAdmin.id); // 验证创建人自动设置为当前管理员用户
      }
    });

    it('应该验证创建订单的必填字段', async () => {
      const invalidData = {
        // 缺少必填字段
        amount: -1,
        orderType: -1
      };

      const response = await client.index.$post({
        json: invalidData
      }, {
        headers: {
          'Authorization': `Bearer ${adminToken}`
        }
      });

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

  describe('GET /orders/:id', () => {
    it('应该返回指定订单的详情', async () => {
      // 先创建一个订单
      const dataSource = await IntegrationTestDatabase.getDataSource();
      const orderRepository = dataSource.getRepository(Order);
      const testOrder = orderRepository.create({
        orderNo: `ORDER_${Math.floor(Math.random() * 100000)}`,
        userId: testUser.id,
        amount: 100.00,
        costAmount: 80.00,
        payAmount: 100.00,
        orderType: 1,
        payType: 1,
        payState: 2,
        state: 0,
        addressId: testDeliveryAddress.id,
        merchantId: testMerchant.id,
        supplierId: testSupplier.id,
        recevierProvince: testProvince.id,
        recevierCity: testCity.id,
        recevierDistrict: testDistrict.id,
        recevierTown: testTown.id,
        createdBy: testUser.id
      });
      await orderRepository.save(testOrder);

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

      console.debug('管理员订单详情响应状态:', response.status);
      expect(response.status).toBe(200);

      if (response.status === 200) {
        const data = await response.json();
        expect(data.id).toBe(testOrder.id);
        expect(data.orderNo).toBe(testOrder.orderNo);
        expect(data.userId).toBe(testUser.id);
      }
    });

    it('应该处理不存在的订单', async () => {
      const response = await client[':id'].$get({
        param: { id: 999999 }
      }, {
        headers: {
          'Authorization': `Bearer ${adminToken}`
        }
      });

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

  describe('PUT /orders/:id', () => {
    it('应该成功更新任何订单', async () => {
      // 先创建一个订单
      const dataSource = await IntegrationTestDatabase.getDataSource();
      const orderRepository = dataSource.getRepository(Order);
      const testOrder = orderRepository.create({
        orderNo: `ORDER_${Math.floor(Math.random() * 100000)}`,
        userId: testUser.id,
        amount: 100.00,
        costAmount: 80.00,
        payAmount: 100.00,
        orderType: 1,
        payType: 1,
        payState: 2,
        state: 0,
        addressId: testDeliveryAddress.id,
        merchantId: testMerchant.id,
        supplierId: testSupplier.id,
        recevierProvince: testProvince.id,
        recevierCity: testCity.id,
        recevierDistrict: testDistrict.id,
        recevierTown: testTown.id,
        createdBy: testUser.id
      });
      await orderRepository.save(testOrder);

      const updateData = {
        state: 1,
        payState: 1,
        updatedBy: testAdmin.id // 管理员可以指定更新人
      };

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

      console.debug('管理员更新订单响应状态:', response.status);
      expect(response.status).toBe(200);

      if (response.status === 200) {
        const data = await response.json();
        expect(data.state).toBe(updateData.state);
        expect(data.payState).toBe(updateData.payState);
        expect(data.updatedBy).toBe(testAdmin.id); // 验证可以指定更新人
      }
    });
  });

  describe('DELETE /orders/:id', () => {
    it('应该成功删除任何订单', async () => {
      // 先创建一个订单
      const dataSource = await IntegrationTestDatabase.getDataSource();
      const orderRepository = dataSource.getRepository(Order);
      const testOrder = orderRepository.create({
        orderNo: `ORDER_${Math.floor(Math.random() * 100000)}`,
        userId: testUser.id,
        amount: 100.00,
        costAmount: 80.00,
        payAmount: 100.00,
        orderType: 1,
        payType: 1,
        payState: 2,
        state: 0,
        addressId: testDeliveryAddress.id,
        merchantId: testMerchant.id,
        supplierId: testSupplier.id,
        recevierProvince: testProvince.id,
        recevierCity: testCity.id,
        recevierDistrict: testDistrict.id,
        recevierTown: testTown.id,
        createdBy: testUser.id
      });
      await orderRepository.save(testOrder);

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

      console.debug('管理员删除订单响应状态:', response.status);
      expect(response.status).toBe(204);
    });
  });

  describe('订单状态管理测试', () => {
    it('应该正确处理订单状态变更', async () => {
      const dataSource = await IntegrationTestDatabase.getDataSource();
      const orderRepository = dataSource.getRepository(Order);

      // 创建不同状态的订单
      const pendingOrder = orderRepository.create({
        orderNo: `ORDER_${Math.floor(Math.random() * 100000)}`,
        userId: testUser.id,
        amount: 100.00,
        costAmount: 80.00,
        payAmount: 100.00,
        orderType: 1,
        payType: 1,
        payState: 2,
        state: 0, // 未发货
        addressId: testDeliveryAddress.id,
        merchantId: testMerchant.id,
        supplierId: testSupplier.id,
        recevierProvince: testProvince.id,
        recevierCity: testCity.id,
        recevierDistrict: testDistrict.id,
        recevierTown: testTown.id,
        createdBy: testUser.id
      });
      await orderRepository.save(pendingOrder);

      const shippedOrder = orderRepository.create({
        orderNo: `ORDER_${Math.floor(Math.random() * 100000)}`,
        userId: testUser.id,
        amount: 200.00,
        costAmount: 160.00,
        payAmount: 200.00,
        orderType: 1,
        payType: 1,
        payState: 2,
        state: 1, // 已发货
        addressId: testDeliveryAddress.id,
        merchantId: testMerchant.id,
        supplierId: testSupplier.id,
        recevierProvince: testProvince.id,
        recevierCity: testCity.id,
        recevierDistrict: testDistrict.id,
        recevierTown: testTown.id,
        createdBy: testUser.id
      });
      await orderRepository.save(shippedOrder);

      const completedOrder = orderRepository.create({
        orderNo: `ORDER_${Math.floor(Math.random() * 100000)}`,
        userId: testUser.id,
        amount: 300.00,
        costAmount: 240.00,
        payAmount: 300.00,
        orderType: 1,
        payType: 1,
        payState: 2,
        state: 2, // 收货成功
        addressId: testDeliveryAddress.id,
        merchantId: testMerchant.id,
        supplierId: testSupplier.id,
        recevierProvince: testProvince.id,
        recevierCity: testCity.id,
        recevierDistrict: testDistrict.id,
        recevierTown: testTown.id,
        createdBy: testUser.id
      });
      await orderRepository.save(completedOrder);

      // 验证状态过滤
      const response = await client.index.$get({
        query: { filters: JSON.stringify({ state: 1 }) }
      }, {
        headers: {
          'Authorization': `Bearer ${adminToken}`
        }
      });

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

      // 类型检查确保data属性存在
      if ('data' in data && Array.isArray(data.data)) {
        // 应该只返回已发货状态的订单
        const pendingOrdersInResponse = data.data.filter((order: any) => order.state === 0);
        const shippedOrdersInResponse = data.data.filter((order: any) => order.state === 1);
        const completedOrdersInResponse = data.data.filter((order: any) => order.state === 2);

        expect(pendingOrdersInResponse.length).toBe(0);
        expect(shippedOrdersInResponse.length).toBeGreaterThan(0);
        expect(completedOrdersInResponse.length).toBe(0);
      } else {
        // 如果响应是错误格式，应该失败
        expect(data).toHaveProperty('data');
      }
    });
  });

  describe('支付状态管理测试', () => {
    it('应该正确处理支付状态变更', async () => {
      const dataSource = await IntegrationTestDatabase.getDataSource();
      const orderRepository = dataSource.getRepository(Order);

      // 创建不同支付状态的订单
      const unpaidOrder = orderRepository.create({
        orderNo: `ORDER_${Math.floor(Math.random() * 100000)}`,
        userId: testUser.id,
        amount: 100.00,
        costAmount: 80.00,
        payAmount: 100.00,
        orderType: 1,
        payType: 1,
        payState: 0, // 未支付
        state: 0,
        addressId: testDeliveryAddress.id,
        merchantId: testMerchant.id,
        supplierId: testSupplier.id,
        recevierProvince: testProvince.id,
        recevierCity: testCity.id,
        recevierDistrict: testDistrict.id,
        recevierTown: testTown.id,
        createdBy: testUser.id
      });
      await orderRepository.save(unpaidOrder);

      const payingOrder = orderRepository.create({
        orderNo: `ORDER_${Math.floor(Math.random() * 100000)}`,
        userId: testUser.id,
        amount: 200.00,
        costAmount: 160.00,
        payAmount: 200.00,
        orderType: 1,
        payType: 1,
        payState: 1, // 支付中
        state: 0,
        addressId: testDeliveryAddress.id,
        merchantId: testMerchant.id,
        supplierId: testSupplier.id,
        recevierProvince: testProvince.id,
        recevierCity: testCity.id,
        recevierDistrict: testDistrict.id,
        recevierTown: testTown.id,
        createdBy: testUser.id
      });
      await orderRepository.save(payingOrder);

      const paidOrder = orderRepository.create({
        orderNo: `ORDER_${Math.floor(Math.random() * 100000)}`,
        userId: testUser.id,
        amount: 300.00,
        costAmount: 240.00,
        payAmount: 300.00,
        orderType: 1,
        payType: 1,
        payState: 2, // 支付成功
        state: 0,
        addressId: testDeliveryAddress.id,
        merchantId: testMerchant.id,
        supplierId: testSupplier.id,
        recevierProvince: testProvince.id,
        recevierCity: testCity.id,
        recevierDistrict: testDistrict.id,
        recevierTown: testTown.id,
        createdBy: testUser.id
      });
      await orderRepository.save(paidOrder);

      // 验证支付状态过滤
      const response = await client.index.$get({
        query: { filters: JSON.stringify({ payState: 2 }) }
      }, {
        headers: {
          'Authorization': `Bearer ${adminToken}`
        }
      });

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

      // 类型检查确保data属性存在
      if ('data' in data && Array.isArray(data.data)) {
        // 应该只返回支付成功状态的订单
        const unpaidOrdersInResponse = data.data.filter((order: any) => order.payState === 0);
        const payingOrdersInResponse = data.data.filter((order: any) => order.payState === 1);
        const paidOrdersInResponse = data.data.filter((order: any) => order.payState === 2);

        expect(unpaidOrdersInResponse.length).toBe(0);
        expect(payingOrdersInResponse.length).toBe(0);
        expect(paidOrdersInResponse.length).toBeGreaterThan(0);
      } else {
        // 如果响应是错误格式，应该失败
        expect(data).toHaveProperty('data');
      }
    });
  });

  describe('管理员权限验证测试', () => {
    it('应该验证管理员可以访问所有数据', async () => {
      const dataSource = await IntegrationTestDatabase.getDataSource();
      const orderRepository = dataSource.getRepository(Order);

      // 创建多个用户的订单
      const userOrder = orderRepository.create({
        orderNo: `ORDER_${Math.floor(Math.random() * 100000)}`,
        userId: testUser.id,
        amount: 100.00,
        costAmount: 80.00,
        payAmount: 100.00,
        orderType: 1,
        payType: 1,
        payState: 2,
        state: 0,
        addressId: testDeliveryAddress.id,
        merchantId: testMerchant.id,
        supplierId: testSupplier.id,
        recevierProvince: testProvince.id,
        recevierCity: testCity.id,
        recevierDistrict: testDistrict.id,
        recevierTown: testTown.id,
        createdBy: testUser.id
      });
      await orderRepository.save(userOrder);

      const adminOrder = orderRepository.create({
        orderNo: `ORDER_${Math.floor(Math.random() * 100000)}`,
        userId: testAdmin.id,
        amount: 200.00,
        costAmount: 160.00,
        payAmount: 200.00,
        orderType: 1,
        payType: 1,
        payState: 2,
        state: 0,
        addressId: testDeliveryAddress.id,
        merchantId: testMerchant.id,
        supplierId: testSupplier.id,
        recevierProvince: testProvince.id,
        recevierCity: testCity.id,
        recevierDistrict: testDistrict.id,
        recevierTown: testTown.id,
        createdBy: testAdmin.id
      });
      await orderRepository.save(adminOrder);

      // 使用管理员token获取列表
      const response = await client.index.$get({
        query: {}
      }, {
        headers: {
          'Authorization': `Bearer ${adminToken}`
        }
      });

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

      // 类型检查确保data属性存在
      if ('data' in data && Array.isArray(data.data)) {
        // 验证返回所有用户的订单
        const userOrdersInResponse = data.data.filter((order: any) => order.userId === testUser.id);
        const adminOrdersInResponse = data.data.filter((order: any) => order.userId === testAdmin.id);

        expect(userOrdersInResponse.length).toBeGreaterThan(0);
        expect(adminOrdersInResponse.length).toBeGreaterThan(0);
      } else {
        // 如果响应是错误格式，应该失败
        expect(data).toHaveProperty('data');
      }
    });
  });
});