/**
 * 文件职责：会员中心积分商城 API 契约定义。
 */
import { requestClient } from '#/api/request';

/** 兑换类型。 */
export type MemberPointMallRedeemType = 'coupon' | 'physical' | 'product';

/** 兑换方式。 */
export type MemberPointMallExchangeType = 'mixed' | 'points';

/** 商品状态。 */
export type MemberPointMallProductStatus = 'disabled' | 'enabled';

/** 积分有效期模式。 */
export type MemberPointMallExpiryMode = 'permanent' | 'yearly_clear';

/** 通知渠道。 */
export type MemberPointMallNotifyChannel = 'in_app' | 'sms';

/** 实物领取方式。 */
export type MemberPointMallPickupMethod = 'delivery' | 'store_pickup';

/** 兑换记录状态。 */
export type MemberPointMallRecordStatus =
  | 'canceled'
  | 'completed'
  | 'issued'
  | 'pending_pickup';

/** 核销方式。 */
export type MemberPointMallVerifyMethod = 'manual' | 'scan';

/** 规则详情查询。 */
export interface MemberPointMallRuleDetailQuery {
  storeId: string;
}

/** 规则模型。 */
export interface MemberPointMallRuleDto {
  consumeAmountPerStep: number;
  consumeRewardPointsPerStep: number;
  expiryMode: MemberPointMallExpiryMode;
  isConsumeRewardEnabled: boolean;
  isRegisterRewardEnabled: boolean;
  isReviewRewardEnabled: boolean;
  isSigninRewardEnabled: boolean;
  registerRewardPoints: number;
  reviewRewardPoints: number;
  signinRewardPoints: number;
  storeId: string;
}

/** 规则统计。 */
export interface MemberPointMallRuleStatsDto {
  pointMembers: number;
  redeemRate: number;
  redeemedPoints: number;
  totalIssuedPoints: number;
}

/** 规则详情结果。 */
export interface MemberPointMallRuleDetailResultDto {
  rule: MemberPointMallRuleDto;
  stats: MemberPointMallRuleStatsDto;
}

/** 保存规则。 */
export type SaveMemberPointMallRulePayload = MemberPointMallRuleDto;

/** 商品列表查询。 */
export interface MemberPointMallProductListQuery {
  keyword?: string;
  status?: MemberPointMallProductStatus;
  storeId: string;
}

/** 商品详情查询。 */
export interface MemberPointMallProductDetailQuery {
  pointMallProductId: string;
  storeId: string;
}

/** 商品模型。 */
export interface MemberPointMallProductDto {
  cashAmount: number;
  couponTemplateId?: string;
  description?: string;
  exchangeType: MemberPointMallExchangeType;
  imageUrl?: string;
  name: string;
  notifyChannels: MemberPointMallNotifyChannel[];
  perMemberLimit?: number;
  physicalName?: string;
  pickupMethod?: MemberPointMallPickupMethod;
  pointMallProductId: string;
  productId?: string;
  redeemType: MemberPointMallRedeemType;
  redeemTypeText: string;
  redeemedCount: number;
  requiredPoints: number;
  status: MemberPointMallProductStatus;
  statusText: string;
  stockAvailable: number;
  stockTotal: number;
  storeId: string;
  updatedAt: string;
}

/** 商品列表结果。 */
export interface MemberPointMallProductListResultDto {
  items: MemberPointMallProductDto[];
}

/** 保存商品。 */
export interface SaveMemberPointMallProductPayload {
  cashAmount: number;
  couponTemplateId?: string;
  description?: string;
  exchangeType: MemberPointMallExchangeType;
  imageUrl?: string;
  name: string;
  notifyChannels: MemberPointMallNotifyChannel[];
  perMemberLimit?: number;
  physicalName?: string;
  pickupMethod?: MemberPointMallPickupMethod;
  pointMallProductId?: string;
  productId?: string;
  redeemType: MemberPointMallRedeemType;
  requiredPoints: number;
  status: MemberPointMallProductStatus;
  stockTotal: number;
  storeId: string;
}

/** 修改商品状态。 */
export interface ChangeMemberPointMallProductStatusPayload {
  pointMallProductId: string;
  status: MemberPointMallProductStatus;
  storeId: string;
}

/** 删除商品。 */
export interface DeleteMemberPointMallProductPayload {
  pointMallProductId: string;
  storeId: string;
}

/** 记录列表查询。 */
export interface MemberPointMallRecordListQuery {
  endDate?: string;
  keyword?: string;
  page: number;
  pageSize: number;
  redeemType?: MemberPointMallRedeemType;
  startDate?: string;
  status?: MemberPointMallRecordStatus;
  storeId: string;
}

/** 记录详情查询。 */
export interface MemberPointMallRecordDetailQuery {
  recordId: string;
  storeId: string;
}

/** 记录模型。 */
export interface MemberPointMallRecordDto {
  cashAmount: number;
  exchangeType: MemberPointMallExchangeType;
  issuedAt?: string;
  memberId: string;
  memberMobileMasked: string;
  memberName: string;
  pointMallProductId: string;
  productName: string;
  recordId: string;
  recordNo: string;
  redeemedAt: string;
  redeemType: MemberPointMallRedeemType;
  redeemTypeText: string;
  status: MemberPointMallRecordStatus;
  statusText: string;
  usedPoints: number;
  verifiedAt?: string;
}

/** 记录详情。 */
export interface MemberPointMallRecordDetailDto extends MemberPointMallRecordDto {
  verifyMethod?: MemberPointMallVerifyMethod;
  verifyMethodText?: string;
  verifyRemark?: string;
  verifiedBy?: string;
}

/** 记录统计。 */
export interface MemberPointMallRecordStatsDto {
  currentMonthUsedPoints: number;
  pendingPhysicalCount: number;
  todayRedeemCount: number;
}

/** 记录分页结果。 */
export interface MemberPointMallRecordListResultDto {
  items: MemberPointMallRecordDto[];
  page: number;
  pageSize: number;
  stats: MemberPointMallRecordStatsDto;
  totalCount: number;
}

/** 导出记录查询。 */
export interface ExportMemberPointMallRecordQuery {
  endDate?: string;
  keyword?: string;
  redeemType?: MemberPointMallRedeemType;
  startDate?: string;
  status?: MemberPointMallRecordStatus;
  storeId: string;
}

/** 导出结果。 */
export interface MemberPointMallRecordExportDto {
  fileContentBase64: string;
  fileName: string;
  totalCount: number;
}

/** 写入记录请求。 */
export interface WriteMemberPointMallRecordPayload {
  memberId: string;
  pointMallProductId: string;
  redeemedAt?: string;
  storeId: string;
}

/** 核销记录请求。 */
export interface VerifyMemberPointMallRecordPayload {
  recordId: string;
  storeId: string;
  verifyMethod: MemberPointMallVerifyMethod;
  verifyRemark?: string;
}

/** 查询规则详情。 */
export async function getMemberPointMallRuleDetailApi(
  params: MemberPointMallRuleDetailQuery,
) {
  return requestClient.get<MemberPointMallRuleDetailResultDto>(
    '/member/points-mall/rule/detail',
    {
      params,
    },
  );
}

/** 保存规则。 */
export async function saveMemberPointMallRuleApi(
  payload: SaveMemberPointMallRulePayload,
) {
  return requestClient.post<MemberPointMallRuleDto>(
    '/member/points-mall/rule/save',
    payload,
  );
}

/** 查询商品列表。 */
export async function getMemberPointMallProductListApi(
  params: MemberPointMallProductListQuery,
) {
  return requestClient.get<MemberPointMallProductListResultDto>(
    '/member/points-mall/product/list',
    {
      params,
    },
  );
}

/** 查询商品详情。 */
export async function getMemberPointMallProductDetailApi(
  params: MemberPointMallProductDetailQuery,
) {
  return requestClient.get<MemberPointMallProductDto>(
    '/member/points-mall/product/detail',
    {
      params,
    },
  );
}

/** 保存商品。 */
export async function saveMemberPointMallProductApi(
  payload: SaveMemberPointMallProductPayload,
) {
  return requestClient.post<MemberPointMallProductDto>(
    '/member/points-mall/product/save',
    payload,
  );
}

/** 修改商品状态。 */
export async function changeMemberPointMallProductStatusApi(
  payload: ChangeMemberPointMallProductStatusPayload,
) {
  return requestClient.post<MemberPointMallProductDto>(
    '/member/points-mall/product/status',
    payload,
  );
}

/** 删除商品。 */
export async function deleteMemberPointMallProductApi(
  payload: DeleteMemberPointMallProductPayload,
) {
  return requestClient.post('/member/points-mall/product/delete', payload);
}

/** 查询兑换记录。 */
export async function getMemberPointMallRecordListApi(
  params: MemberPointMallRecordListQuery,
) {
  return requestClient.get<MemberPointMallRecordListResultDto>(
    '/member/points-mall/record/list',
    {
      params,
    },
  );
}

/** 查询兑换记录详情。 */
export async function getMemberPointMallRecordDetailApi(
  params: MemberPointMallRecordDetailQuery,
) {
  return requestClient.get<MemberPointMallRecordDetailDto>(
    '/member/points-mall/record/detail',
    {
      params,
    },
  );
}

/** 导出兑换记录。 */
export async function exportMemberPointMallRecordApi(
  params: ExportMemberPointMallRecordQuery,
) {
  return requestClient.get<MemberPointMallRecordExportDto>(
    '/member/points-mall/record/export',
    {
      params,
    },
  );
}

/** 写入兑换记录。 */
export async function writeMemberPointMallRecordApi(
  payload: WriteMemberPointMallRecordPayload,
) {
  return requestClient.post<MemberPointMallRecordDto>(
    '/member/points-mall/record/write',
    payload,
  );
}

/** 核销兑换记录。 */
export async function verifyMemberPointMallRecordApi(
  payload: VerifyMemberPointMallRecordPayload,
) {
  return requestClient.post<MemberPointMallRecordDetailDto>(
    '/member/points-mall/record/verify',
    payload,
  );
}
