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

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

export namespace ComplaintApi {
  export interface Complaint {
    id: string;
    type: 'alipay' | 'manual';
    order_id: string;
    name: string;
    contact: string;
    content: string;
    status: 'pending' | 'processing' | 'resolved' | 'closed';
    status_description: string;
    remark: string;
    handler_id: string;
    handle_time: string;
    create_time: string;
    update_time: string;
    // 支付宝投诉特有字段
    task_id?: string;
    trade_no?: string;
    complain_amount?: string;
    gmt_complain?: string;
    // 主动投诉特有字段
    subject?: string;
    priority?: string;
    source?: string;
  }

  export interface ComplaintList {
    total: number;
    items: Complaint[];
  }

  export interface ComplaintDetail {
    id: string;
    type: 'alipay' | 'manual';
    order_id: string;
    name: string;
    contact: string;
    content: string;
    status: string;
    status_description: string;
    remark: string;
    handler_id: string;
    handle_time: string;
    create_time: string;
    update_time: string;
    alipay_complaint?: AlipayComplaintDetail;
    manual_complaint?: ManualComplaintDetail;
  }

  export interface AlipayComplaintDetail {
    id: string;
    alipay_id: number;
    task_id: string;
    opposite_pid: string;
    opposite_name: string;
    complain_amount: string;
    gmt_complain: string;
    gmt_process: string;
    complain_content: string;
    trade_no: string;
    status: string;
    status_description: string;
    process_code: string;
    process_message: string;
    process_remark: string;
    process_img_url_list: string[];
    gmt_risk_finish_time: string;
    complain_url: string;
    certify_info: string[];
    trade_info_list: AlipayComplaintTradeInfo[];
  }

  export interface AlipayComplaintTradeInfo {
    id: string;
    alipay_trade_id: string;
    alipay_complaint_record_id: string;
    trade_no: string;
    out_no: string;
    gmt_trade: string;
    gmt_refund: string;
    status: string;
    status_description: string;
    amount: string;
  }

  export interface ManualComplaintDetail {
    id: string;
    user_id: string;
    subject: string;
    priority: string;
    source: string;
    attachment_urls: string[];
  }

  export interface UpdateStatusRequest {
    status: 'pending' | 'processing' | 'resolved' | 'closed';
    status_description?: string;
    handler_id?: string;
  }

  export interface UpdateRemarkRequest {
    remark: string;
  }
}

/**
 * 获取投诉列表
 */
async function getComplaintList(params: Recordable<any>) {
  return requestClient.get<ComplaintApi.ComplaintList>('/complaint/list', {
    params,
  });
}

/**
 * 获取投诉详情
 * @param id 投诉 ID
 */
async function getComplaintDetail(id: string) {
  return requestClient.get<ComplaintApi.ComplaintDetail>(
    `/complaint/detail/${id}`,
  );
}

/**
 * 更新投诉状态
 * @param id 投诉 ID
 * @param data 更新状态请求数据
 */
async function updateComplaintStatus(
  id: string,
  data: ComplaintApi.UpdateStatusRequest,
) {
  return requestClient.put<{ success: boolean }>(
    `/complaint/update-status/${id}`,
    data,
  );
}

/**
 * 更新投诉备注
 * @param id 投诉 ID
 * @param data 更新备注请求数据
 */
async function updateComplaintRemark(
  id: string,
  data: ComplaintApi.UpdateRemarkRequest,
) {
  return requestClient.put<{ success: boolean }>(
    `/complaint/update-remark/${id}`,
    data,
  );
}

export { getComplaintList, getComplaintDetail, updateComplaintStatus, updateComplaintRemark };

