/**
 * 文件职责：会员中心储值卡 API 与 DTO 定义。
 */
import { requestClient } from '#/api/request';

/** 储值卡方案状态。 */
export type MemberStoredCardPlanStatus = 'disabled' | 'enabled';

/** 储值卡充值支付方式。 */
export type MemberStoredCardPaymentMethod =
  | 'alipay'
  | 'balance'
  | 'card'
  | 'cash'
  | 'unknown'
  | 'wechat';

/** 储值卡方案列表查询。 */
export interface MemberStoredCardPlanListQuery {
  storeId: string;
}

/** 储值卡方案统计。 */
export interface MemberStoredCardPlanStatsDto {
  currentMonthRechargeAmount: number;
  rechargeMemberCount: number;
  totalGiftAmount: number;
  totalRechargeAmount: number;
}

/** 储值卡方案项。 */
export interface MemberStoredCardPlanDto {
  arrivedAmount: number;
  giftAmount: number;
  planId: string;
  rechargeAmount: number;
  rechargeCount: number;
  sortOrder: number;
  status: MemberStoredCardPlanStatus;
  totalRechargeAmount: number;
}

/** 储值卡方案列表结果。 */
export interface MemberStoredCardPlanListResultDto {
  items: MemberStoredCardPlanDto[];
  stats: MemberStoredCardPlanStatsDto;
}

/** 保存储值卡方案请求。 */
export interface SaveMemberStoredCardPlanPayload {
  giftAmount: number;
  planId?: string;
  rechargeAmount: number;
  sortOrder: number;
  status: MemberStoredCardPlanStatus;
  storeId: string;
}

/** 修改储值卡方案状态请求。 */
export interface ChangeMemberStoredCardPlanStatusPayload {
  planId: string;
  status: MemberStoredCardPlanStatus;
  storeId: string;
}

/** 删除储值卡方案请求。 */
export interface DeleteMemberStoredCardPlanPayload {
  planId: string;
  storeId: string;
}

/** 充值记录列表查询。 */
export interface MemberStoredCardRechargeRecordListQuery {
  endDate?: string;
  keyword?: string;
  page: number;
  pageSize: number;
  startDate?: string;
  storeId: string;
}

/** 储值卡充值记录项。 */
export interface MemberStoredCardRechargeRecordDto {
  arrivedAmount: number;
  giftAmount: number;
  memberId: string;
  memberMobileMasked: string;
  memberName: string;
  paymentMethod: MemberStoredCardPaymentMethod;
  paymentMethodText: string;
  planId?: string;
  rechargedAt: string;
  rechargeAmount: number;
  recordId: string;
  recordNo: string;
  remark?: string;
}

/** 储值卡充值记录分页结果。 */
export interface MemberStoredCardRechargeRecordListResultDto {
  items: MemberStoredCardRechargeRecordDto[];
  page: number;
  pageSize: number;
  totalCount: number;
}

/** 储值卡充值记录导出查询。 */
export interface ExportMemberStoredCardRechargeRecordQuery {
  endDate?: string;
  keyword?: string;
  startDate?: string;
  storeId: string;
}

/** 储值卡充值记录导出结果。 */
export interface MemberStoredCardRechargeRecordExportDto {
  fileContentBase64: string;
  fileName: string;
  totalCount: number;
}

/** 写入储值卡充值记录请求。 */
export interface WriteMemberStoredCardRechargeRecordPayload {
  giftAmount: number;
  memberId: string;
  paymentMethod: Exclude<MemberStoredCardPaymentMethod, 'unknown'>;
  planId?: string;
  rechargedAt?: string;
  rechargeAmount: number;
  remark?: string;
  storeId: string;
}

/** 查询储值卡方案列表。 */
export async function getMemberStoredCardPlanListApi(
  params: MemberStoredCardPlanListQuery,
) {
  return requestClient.get<MemberStoredCardPlanListResultDto>(
    '/member/stored-card/plan/list',
    {
      params,
    },
  );
}

/** 保存储值卡方案。 */
export async function saveMemberStoredCardPlanApi(
  payload: SaveMemberStoredCardPlanPayload,
) {
  return requestClient.post<MemberStoredCardPlanDto>(
    '/member/stored-card/plan/save',
    payload,
  );
}

/** 修改储值卡方案状态。 */
export async function changeMemberStoredCardPlanStatusApi(
  payload: ChangeMemberStoredCardPlanStatusPayload,
) {
  return requestClient.post<MemberStoredCardPlanDto>(
    '/member/stored-card/plan/status',
    payload,
  );
}

/** 删除储值卡方案。 */
export async function deleteMemberStoredCardPlanApi(
  payload: DeleteMemberStoredCardPlanPayload,
) {
  return requestClient.post('/member/stored-card/plan/delete', payload);
}

/** 查询储值卡充值记录。 */
export async function getMemberStoredCardRechargeRecordListApi(
  params: MemberStoredCardRechargeRecordListQuery,
) {
  return requestClient.get<MemberStoredCardRechargeRecordListResultDto>(
    '/member/stored-card/record/list',
    {
      params,
    },
  );
}

/** 导出储值卡充值记录。 */
export async function exportMemberStoredCardRechargeRecordApi(
  params: ExportMemberStoredCardRechargeRecordQuery,
) {
  return requestClient.get<MemberStoredCardRechargeRecordExportDto>(
    '/member/stored-card/record/export',
    {
      params,
    },
  );
}

/** 写入储值卡充值记录。 */
export async function writeMemberStoredCardRechargeRecordApi(
  payload: WriteMemberStoredCardRechargeRecordPayload,
) {
  return requestClient.post<MemberStoredCardRechargeRecordDto>(
    '/member/stored-card/record/write',
    payload,
  );
}
