import type { Pagination } from '@/types/global';
import type { CommonPageResult, CommonResult } from './common';
import qs from 'query-string';
import axios from 'axios';

export interface TMallOrderRecord {
  accountNumber: string;
  status: string;
  createdAt: string;
  accountId: string;
  accountInfo: {
    accountNumber: string;
    id: string;
  };
  notifyStatus: number;
  amount: number;
  callbackType: string;
  callbackUrl: string;
  merchantOrder: string;
  orderNo: string;
  remark: string;
  shopId: number;
  updatedAt: string;
}

export interface RechargeTMallShopRecord {
  id: number;
  orderNo: string;
  tMallOrderNo: number;
  created?: string; // 使用了 gtime.Time 的假定类型，且允许为 null
  buyerNick: string;
  buyerRate: number;
  status: string;
  payment: string; // 如果 payment 字段存储的是数字，那么应该用 number 类型
  totalFee: string; // 同上，如果是数字存储则使用 number 类型
  payTime?: string | null; // 同上
  tradeStatus: string;
  endTime?: string | null; // 同上
  buyerMemo: string;
  alipayNo: string;
  buyerMessage: string;
}

export interface RechargeTMallOrderHistory {
  id: number;
  orderNo: string;
  originalData: string;
  remark: string;
  status: string;
  sourceType: string;
  createdAt?: Date | null; // 使用 Date 类型
}

export interface TMallOrderParams
  extends Pagination,
    Partial<TMallOrderRecord> {
  dateRange?: Date[];
}

export function queryTMallOrderList(params: TMallOrderParams) {
  delete params.dateRange;
  return axios.get<CommonPageResult<TMallOrderRecord>>(
    '/api/recharge/tMallGame/order/list',
    {
      params,
      paramsSerializer: obj => {
        return qs.stringify(obj, { arrayFormat: 'bracket' });
      }
    }
  );
}

export function queryTMallShopOrder(params: { orderNo: string }) {
  return axios.get<RechargeTMallShopRecord>(
    '/api/recharge/tMallGame/shopOrder/getOne',
    {
      params,
      paramsSerializer: obj => {
        return qs.stringify(obj, { arrayFormat: 'bracket' });
      }
    }
  );
}

export function queryTMallOrderHistory(params: { orderNo: string }) {
  return axios.get<CommonResult<RechargeTMallOrderHistory>>(
    '/api/recharge/tMallGame/shopOrder/getHistoryList',
    {
      params,
      paramsSerializer: obj => {
        return qs.stringify(obj);
      }
    }
  );
}

// 手动回调订单
export function callBackOrderManualAPI(data: {
  orderNo?: string;
  rechargeId?: string;
}) {
  return axios.post('/api/recharge/tMallGame/shopOrder/callbackByManual', data);
}

/**
 * 订单统计摘要
 */
export interface OrderSummary {
  /**
   * 订单数量
   */
  count: number;

  /**
   * 订单成功数量
   */
  succeedCount: number;

  /**
   * 订单成功金额
   */
  succeedAmount: number;
  callbackType: string;

  /**
   * 总支付金额
   */
  amount: number;

  /**
   * 今日订单成功率
   */
  rate: number;

  /**
   * 日期
   */
  createdAt: string;
}
export interface OrderSummaryParams extends Pagination, Partial<OrderSummary> {}

export function getOrderSummary(params: OrderSummaryParams) {
  return axios.get<CommonPageResult<OrderSummary>>(
    '/api/recharge/tMallGame/workspace/orderSummary',
    {
      params,
      paramsSerializer: obj => {
        return qs.stringify(obj);
      }
    }
  );
}

/**
 * 表示商城游戏统计数据的响应对象
 */
export interface TMallGameStatsRes {
  /**
   * 总订单数量
   */
  totalCount: number;

  /**
   * 总订单成功数量
   */
  totalSucceedCount: number;

  /**
   * 总支付金额
   */
  totalAmount: number;

  /**
   * 总成功订单总额
   */
  totalSucceedAmount: number;

  /**
   * 今日订单数量
   */
  todayCount: number;

  /**
   * 今日订单成功数量
   */
  todaySucceedCount: number;

  /**
   * 今日支付金额
   */
  todayAmount: number;

  /**
   * 今日成功订单总额
   */
  TodaySucceedAmount: number;

  /**
   * 总订单成功率
   */
  totalRate: number;

  /**
   * 今日订单成功率
   */
  todayRate: number;
}

export function getOrderStats(params: { channelName?: string }) {
  return axios.get<TMallGameStatsRes>(
    '/api/recharge/tMallGame/workspace/stats',
    {
      params,
      paramsSerializer: obj => {
        return qs.stringify(obj);
      }
    }
  );
}

export interface OrderFakeRes {
  id: string; // 充值ID
  sourceOfShop: string; // 来源
  orderNo: string; // 订单号
  category: string; // 游戏品类
  account: string; // 游戏账号
  amount: number; // 充值金额
  topUpTime: Date | null; // 充值时间
  status: number; // 状态
}

export function queryFakeOrder(params: { account: string }) {
  return axios.get<OrderFakeRes>('/api/recharge/tMallGame/order/queryOne', {
    params,
    paramsSerializer: obj => {
      return qs.stringify(obj);
    }
  });
}

export interface GameCategory {
  id: number;
  name: string;
}

// 获取游戏分类
export function queryGameCategory() {
  return axios.get<CommonResult<GameCategory>>(
    '/api/recharge/tMallGame/getCategoryList'
  );
}
