/**
 * 支付流程集成测试
 * 测试下单→支付回调→会员激活完整流程
 */

import { describe, it, expect, beforeAll } from 'vitest';
import { apiPost, apiGet, setAuthToken, initHttpClient, createTestUser } from '../../integration/http-client';

describe('支付流程集成测试', () => {
  let authToken: string;
  let userId: string;

  beforeAll(async () => {
    try {
      const user = await createTestUser();
      authToken = user.token;
      userId = user.userId;
      setAuthToken(authToken);
    } catch (e) {
      console.log('创建测试用户失败，跳过部分测试');
    }
  });

  describe('创建支付订单', () => {
    it('创建支付宝订单成功', async () => {
      if (!authToken) return;

      const res = await apiPost('/api/payment/create-order', {
        planId: 2, // 专业版
        paymentMethod: 'mock', // 使用 mock 支付
        period: 'monthly'
      });

      expect(res.code).toBe(0);
      expect(res.data?.orderNo).toBeDefined();
      expect(res.data?.amount).toBeGreaterThan(0);
    });

    it('创建年付订单成功', async () => {
      if (!authToken) return;

      const res = await apiPost('/api/payment/create-order', {
        planId: 2,
        paymentMethod: 'mock',
        period: 'yearly'
      });

      expect(res.code).toBe(0);
      // 年付价格应该是月付的 10 倍左右
      expect(res.data?.amount).toBeGreaterThan(500);
    });

    it('无效套餐返回错误', async () => {
      if (!authToken) return;

      const res = await apiPost('/api/payment/create-order', {
        planId: 9999,
        paymentMethod: 'mock'
      });

      expect(res.code).not.toBe(0);
    });
  });

  describe('模拟支付回调', () => {
    it('支付成功后会员等级更新', async () => {
      if (!authToken) return;

      // 获取当前会员等级
      const userBeforeRes = await apiGet('/api/auth/user-info');
      const levelBefore = userBeforeRes.data?.memberLevel || 0;

      // 创建订单
      const orderRes = await apiPost('/api/payment/create-order', {
        planId: 2,
        paymentMethod: 'mock',
        period: 'monthly'
      });

      const orderNo = orderRes.data?.orderNo;
      if (!orderNo) return;

      // 模拟支付成功
      const callbackRes = await apiPost('/api/payment/callback/mock', {
        orderNo,
        paymentId: 'MOCK_' + Date.now(),
        status: 'success'
      });

      expect(callbackRes.code).toBe(0);

      // 验证会员等级已更新
      const userAfterRes = await apiGet('/api/auth/user-info');
      const levelAfter = userAfterRes.data?.memberLevel;

      expect(levelAfter).toBeGreaterThanOrEqual(levelBefore);
    });

    it('支付失败时订单状态更新', async () => {
      if (!authToken) return;

      // 创建订单
      const orderRes = await apiPost('/api/payment/create-order', {
        planId: 1,
        paymentMethod: 'mock'
      });

      const orderNo = orderRes.data?.orderNo;
      if (!orderNo) return;

      // 模拟支付失败
      const callbackRes = await apiPost('/api/payment/callback/mock', {
        orderNo,
        paymentId: 'MOCK_FAIL_' + Date.now(),
        status: 'failed'
      });

      expect(callbackRes.code).toBe(0);
    });
  });

  describe('订单查询', () => {
    it('查询订单列表', async () => {
      if (!authToken) return;

      const res = await apiGet('/api/payment/orders');
      expect(res.code).toBe(0);
      expect(Array.isArray(res.data?.list)).toBe(true);
    });

    it('查询订单详情', async () => {
      if (!authToken) return;

      // 先创建订单
      const orderRes = await apiPost('/api/payment/create-order', {
        planId: 1,
        paymentMethod: 'mock'
      });

      const orderNo = orderRes.data?.orderNo;
      if (!orderNo) return;

      const res = await apiGet(`/api/payment/order/${orderNo}`);
      expect(res.code).toBe(0);
      expect(res.data?.orderNo).toBe(orderNo);
    });
  });

  describe('套餐查询', () => {
    it('获取可用套餐列表', async () => {
      if (!authToken) return;

      const res = await apiGet('/api/payment/plans');
      expect(res.code).toBe(0);
      expect(Array.isArray(res.data?.plans)).toBe(true);
      expect(res.data?.plans.length).toBeGreaterThan(0);
    });

    it('获取单个套餐详情', async () => {
      if (!authToken) return;

      const res = await apiGet('/api/payment/plan/2');
      expect(res.code).toBe(0);
      expect(res.data?.name).toBeDefined();
    });
  });
});