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

/** 会员列表筛选参数。 */
export interface MemberListFilterQuery {
  keyword?: string;
  storeId: string;
  tierId?: string;
}

/** 会员列表分页参数。 */
export interface MemberListQuery extends MemberListFilterQuery {
  page: number;
  pageSize: number;
}

/** 会员列表行。 */
export interface MemberListItemDto {
  avatarColor: string;
  avatarText: string;
  isDormant: boolean;
  lastOrderAt: string;
  memberId: string;
  mobileMasked: string;
  name: string;
  orderCount: number;
  pointsBalance: number;
  storedBalance: number;
  tierColorHex: string;
  tierId?: string;
  tierName: string;
  totalAmount: number;
}

/** 会员列表结果。 */
export interface MemberListResultDto {
  items: MemberListItemDto[];
  page: number;
  pageSize: number;
  total: number;
}

/** 会员列表统计。 */
export interface MemberListStatsDto {
  activeMembers: number;
  dormantMembers: number;
  monthlyNewMembers: number;
  totalMembers: number;
}

/** 最近订单。 */
export interface MemberRecentOrderDto {
  amount: number;
  orderNo: string;
  orderedAt: string;
  statusText: string;
}

/** 会员详情。 */
export interface MemberDetailDto {
  avatarColor: string;
  avatarText: string;
  averageAmount: number;
  joinedAt: string;
  memberId: string;
  mobileMasked: string;
  name: string;
  orderCount: number;
  pointsBalance: number;
  recentOrders: MemberRecentOrderDto[];
  storedBalance: number;
  storedGiftBalance: number;
  storedRechargeBalance: number;
  tags: string[];
  tierColorHex: string;
  tierId?: string;
  tierName: string;
  totalAmount: number;
}

/** 会员导出回执。 */
export interface MemberExportDto {
  fileContentBase64: string;
  fileName: string;
  totalCount: number;
}

/** 会员等级列表项。 */
export interface MemberTierListItemDto {
  canDelete: boolean;
  colorHex: string;
  conditionText: string;
  iconKey: string;
  isDefault: boolean;
  memberCount: number;
  name: string;
  perks: string[];
  sortOrder: number;
  tierId: string;
}

/** 升降级规则。 */
export interface MemberTierRuleDto {
  downgradeWindowDays: number;
  upgradeAmountThreshold?: number;
  upgradeOrderCountThreshold?: number;
  upgradeRuleType: 'amount' | 'both' | 'count' | 'none';
}

/** 折扣权益。 */
export interface MemberTierDiscountBenefitDto {
  discountRate?: number;
  enabled: boolean;
}

/** 积分倍率权益。 */
export interface MemberTierPointMultiplierBenefitDto {
  enabled: boolean;
  multiplier?: number;
}

/** 生日特权。 */
export interface MemberTierBirthdayBenefitDto {
  couponTemplateIds: string[];
  doublePointsEnabled: boolean;
  enabled: boolean;
}

/** 每月赠券。 */
export interface MemberTierMonthlyCouponBenefitDto {
  couponTemplateIds: string[];
  enabled: boolean;
  grantDay: number;
}

/** 免配送费权益。 */
export interface MemberTierFreeDeliveryBenefitDto {
  enabled: boolean;
  monthlyFreeTimes: number;
}

/** 等级权益。 */
export interface MemberTierBenefitsDto {
  birthday: MemberTierBirthdayBenefitDto;
  discount: MemberTierDiscountBenefitDto;
  exclusiveServiceEnabled: boolean;
  freeDelivery: MemberTierFreeDeliveryBenefitDto;
  monthlyCoupon: MemberTierMonthlyCouponBenefitDto;
  pointMultiplier: MemberTierPointMultiplierBenefitDto;
  priorityDeliveryEnabled: boolean;
}

/** 等级详情。 */
export interface MemberTierDetailDto {
  benefits: MemberTierBenefitsDto;
  canDelete: boolean;
  colorHex: string;
  iconKey: string;
  isDefault: boolean;
  name: string;
  rule: MemberTierRuleDto;
  sortOrder: number;
  tierId?: string;
}

/** 保存等级请求。 */
export interface SaveMemberTierPayload {
  benefits: MemberTierBenefitsDto;
  colorHex: string;
  iconKey: string;
  isDefault: boolean;
  name: string;
  rule: MemberTierRuleDto;
  sortOrder: number;
  tierId?: string;
}

/** 会员日配置。 */
export interface MemberDaySettingDto {
  extraDiscountRate: number;
  isEnabled: boolean;
  weekday: number;
}

/** 优惠券选择项。 */
export interface MemberCouponPickerItemDto {
  couponTemplateId: string;
  couponType: string;
  displayText: string;
  minimumSpend?: number;
  name: string;
  value: number;
}

/** 查询会员列表。 */
export async function getMemberListApi(params: MemberListQuery) {
  return requestClient.get<MemberListResultDto>('/member/list/list', {
    params,
  });
}

/** 查询会员列表统计。 */
export async function getMemberListStatsApi(params: MemberListFilterQuery) {
  return requestClient.get<MemberListStatsDto>('/member/list/stats', {
    params,
  });
}

/** 查询会员详情。 */
export async function getMemberDetailApi(params: {
  memberId: string;
  storeId: string;
}) {
  return requestClient.get<MemberDetailDto>('/member/list/detail', {
    params,
  });
}

/** 保存会员标签。 */
export async function saveMemberTagsApi(payload: {
  memberId: string;
  storeId?: string;
  tags: string[];
}) {
  return requestClient.post('/member/list/tags', payload);
}

/** 导出会员 CSV。 */
export async function exportMemberCsvApi(params: MemberListFilterQuery) {
  return requestClient.get<MemberExportDto>('/member/list/export', {
    params,
  });
}

/** 查询会员等级列表。 */
export async function getMemberTierListApi() {
  return requestClient.get<MemberTierListItemDto[]>('/member/tier/list');
}

/** 查询会员等级详情。 */
export async function getMemberTierDetailApi(params: { tierId?: string }) {
  return requestClient.get<MemberTierDetailDto>('/member/tier/detail', {
    params,
  });
}

/** 保存会员等级。 */
export async function saveMemberTierApi(payload: SaveMemberTierPayload) {
  return requestClient.post<MemberTierDetailDto>('/member/tier/save', payload);
}

/** 删除会员等级。 */
export async function deleteMemberTierApi(payload: { tierId: string }) {
  return requestClient.post('/member/tier/delete', payload);
}

/** 查询会员日配置。 */
export async function getMemberDaySettingApi() {
  return requestClient.get<MemberDaySettingDto>('/member/tier/day-setting');
}

/** 保存会员日配置。 */
export async function saveMemberDaySettingApi(payload: MemberDaySettingDto) {
  return requestClient.post<MemberDaySettingDto>(
    '/member/tier/day-setting',
    payload,
  );
}

/** 查询优惠券选择器列表。 */
export async function getMemberCouponPickerApi(params: {
  keyword?: string;
  storeId?: string;
}) {
  return requestClient.get<MemberCouponPickerItemDto[]>(
    '/member/tier/coupon-picker',
    {
      params,
    },
  );
}

export * from './message-reach';
export * from './points-mall';
export * from './stored-card';
