import type { Recordable } from '@vben/types';

import { requestClient } from '#/api/request';

export namespace OrderApi {
  export interface Order {
    id: number;
    order_no: string;
    platform_order_id: string;
    product_name: string;
    payment_platform: 'alipay' | 'appleiap' | 'wechat';
    payment_scene: 'app' | 'h5' | 'mini_program' | 'public_account';
    amount: number;
    sales_cost: number;
    status: 'closed' | 'failed' | 'paid' | 'pending' | 'refunded';
    query_state: 'cleaned' | 'failed' | 'pending' | 'processing' | 'success';
    create_time: string;
    pay_time: null | string;
    refund_time: null | string;
    is_promotion: 0 | 1;
  }

  export interface OrderList {
    total: number;
    items: Order[];
  }

  export interface RefundOrderRequest {
    refund_amount: number;
    refund_reason: string;
  }

  export interface RefundOrderResponse {
    status: string;
    refund_no: string;
    amount: number;
  }

  // 退款统计数据
  export interface RefundStatistics {
    total_refund_amount: number;
    today_refund_amount: number;
  }

  // 收入统计数据
  export interface IncomeStatistics {
    total_revenue_amount: number;
    today_revenue_amount: number;
    total_profit_amount: number;
    today_profit_amount: number;
  }

  // 订单来源统计数据
  export interface OrderSourceStatistics {
    product_name: string;
    order_count: number;
  }
}

/**
 * 获取订单列表数据
 */
async function getOrderList(params: Recordable<any>) {
  return requestClient.get<OrderApi.OrderList>('/order/list', {
    params,
  });
}

/**
 * 订单退款
 * @param id 订单 ID
 * @param data 退款请求数据
 */
async function refundOrder(id: number, data: OrderApi.RefundOrderRequest) {
  return requestClient.post<OrderApi.RefundOrderResponse>(
    `/order/refund/${id}`,
    data,
  );
}

/**
 * 获取退款统计数据
 */
async function getRefundStatistics() {
  return requestClient.get<OrderApi.RefundStatistics>('/order/refund-statistics');
}

/**
 * 获取收入统计数据
 */
async function getIncomeStatistics() {
  return requestClient.get<OrderApi.IncomeStatistics>('/order/revenue-statistics');
}

/**
 * 获取订单来源统计数据
 */
async function getOrderSourceStatistics() {
  return requestClient.get<{ items: OrderApi.OrderSourceStatistics[] }>('/order/source-statistics');
}

export { getOrderList, refundOrder, getRefundStatistics, getIncomeStatistics, getOrderSourceStatistics };
