/**
 * 文件职责：营销中心新客有礼 API 与 DTO 定义。
 * 1. 维护新客有礼详情、配置保存、邀请记录分页与写入契约。
 */
import { requestClient } from '#/api/request';

/** 礼包类型。 */
export type MarketingNewCustomerGiftType = 'coupon' | 'direct';

/** 券场景。 */
export type MarketingNewCustomerCouponScene = 'invitee' | 'inviter' | 'welcome';

/** 券类型。 */
export type MarketingNewCustomerCouponType =
  | 'amount_off'
  | 'discount'
  | 'free_shipping';

/** 分享渠道。 */
export type MarketingNewCustomerShareChannel =
  | 'moments'
  | 'sms'
  | 'wechat_friend';

/** 邀请订单状态。 */
export type MarketingNewCustomerInviteOrderStatus = 'ordered' | 'pending_order';

/** 邀请奖励状态。 */
export type MarketingNewCustomerInviteRewardStatus = 'issued' | 'pending';

/** 优惠券规则。 */
export interface MarketingNewCustomerCouponRuleDto {
  couponType: MarketingNewCustomerCouponType;
  id: string;
  minimumSpend: null | number;
  scene: MarketingNewCustomerCouponScene;
  sortOrder: number;
  validDays: number;
  value: null | number;
}

/** 新客有礼设置。 */
export interface MarketingNewCustomerSettingsDto {
  directMinimumSpend: null | number;
  directReduceAmount: null | number;
  giftEnabled: boolean;
  giftType: MarketingNewCustomerGiftType;
  inviteEnabled: boolean;
  inviteeCoupons: MarketingNewCustomerCouponRuleDto[];
  inviterCoupons: MarketingNewCustomerCouponRuleDto[];
  shareChannels: MarketingNewCustomerShareChannel[];
  storeId: string;
  updatedAt: string;
  welcomeCoupons: MarketingNewCustomerCouponRuleDto[];
}

/** 统计数据。 */
export interface MarketingNewCustomerStatsDto {
  firstOrderConversionRate: number;
  firstOrderedCount: number;
  giftClaimRate: number;
  giftClaimedCount: number;
  monthlyGrowthCount: number;
  monthlyGrowthRatePercent: number;
  monthlyNewCustomers: number;
}

/** 邀请记录。 */
export interface MarketingNewCustomerInviteRecordDto {
  id: string;
  inviteeName: string;
  inviteTime: string;
  inviterName: string;
  orderStatus: MarketingNewCustomerInviteOrderStatus;
  rewardIssuedAt: null | string;
  rewardStatus: MarketingNewCustomerInviteRewardStatus;
  sourceChannel?: string;
}

/** 邀请记录分页。 */
export interface MarketingNewCustomerInviteRecordListResultDto {
  items: MarketingNewCustomerInviteRecordDto[];
  page: number;
  pageSize: number;
  totalCount: number;
}

/** 新客有礼详情。 */
export interface MarketingNewCustomerDetailDto {
  inviteRecords: MarketingNewCustomerInviteRecordListResultDto;
  settings: MarketingNewCustomerSettingsDto;
  stats: MarketingNewCustomerStatsDto;
}

/** 新客有礼详情查询。 */
export interface MarketingNewCustomerDetailQuery {
  recordPage?: number;
  recordPageSize?: number;
  storeId: string;
}

/** 邀请记录查询。 */
export interface MarketingNewCustomerInviteRecordListQuery {
  page: number;
  pageSize: number;
  storeId: string;
}

/** 保存券规则项。 */
export interface SaveMarketingNewCustomerCouponRuleDto {
  couponType: MarketingNewCustomerCouponType;
  minimumSpend: null | number;
  validDays: number;
  value: null | number;
}

/** 保存新客有礼配置请求。 */
export interface SaveMarketingNewCustomerSettingsDto {
  directMinimumSpend: null | number;
  directReduceAmount: null | number;
  giftEnabled: boolean;
  giftType: MarketingNewCustomerGiftType;
  inviteEnabled: boolean;
  inviteeCoupons: SaveMarketingNewCustomerCouponRuleDto[];
  inviterCoupons: SaveMarketingNewCustomerCouponRuleDto[];
  shareChannels: MarketingNewCustomerShareChannel[];
  storeId: string;
  welcomeCoupons: SaveMarketingNewCustomerCouponRuleDto[];
}

/** 写入邀请记录请求。 */
export interface WriteMarketingNewCustomerInviteRecordDto {
  inviteeName: string;
  inviteTime: string;
  inviterName: string;
  orderStatus: MarketingNewCustomerInviteOrderStatus;
  rewardIssuedAt: null | string;
  rewardStatus: MarketingNewCustomerInviteRewardStatus;
  sourceChannel?: string;
  storeId: string;
}

/** 新客成长记录。 */
export interface MarketingNewCustomerGrowthRecordDto {
  customerKey: string;
  customerName?: string;
  firstOrderAt: null | string;
  giftClaimedAt: null | string;
  id: string;
  registeredAt: string;
  sourceChannel?: string;
}

/** 写入成长记录请求。 */
export interface WriteMarketingNewCustomerGrowthRecordDto {
  customerKey: string;
  customerName?: string;
  firstOrderAt: null | string;
  giftClaimedAt: null | string;
  registeredAt: string;
  sourceChannel?: string;
  storeId: string;
}

/** 获取新客有礼详情。 */
export async function getMarketingNewCustomerDetailApi(
  params: MarketingNewCustomerDetailQuery,
) {
  return requestClient.get<MarketingNewCustomerDetailDto>(
    '/marketing/new-customer/detail',
    { params },
  );
}

/** 保存新客有礼配置。 */
export async function saveMarketingNewCustomerSettingsApi(
  data: SaveMarketingNewCustomerSettingsDto,
) {
  return requestClient.post<MarketingNewCustomerSettingsDto>(
    '/marketing/new-customer/save',
    data,
  );
}

/** 获取邀请记录分页。 */
export async function getMarketingNewCustomerInviteRecordListApi(
  params: MarketingNewCustomerInviteRecordListQuery,
) {
  return requestClient.get<MarketingNewCustomerInviteRecordListResultDto>(
    '/marketing/new-customer/invite-record/list',
    { params },
  );
}

/** 写入邀请记录。 */
export async function writeMarketingNewCustomerInviteRecordApi(
  data: WriteMarketingNewCustomerInviteRecordDto,
) {
  return requestClient.post<MarketingNewCustomerInviteRecordDto>(
    '/marketing/new-customer/invite-record/write',
    data,
  );
}

/** 写入成长记录。 */
export async function writeMarketingNewCustomerGrowthRecordApi(
  data: WriteMarketingNewCustomerGrowthRecordDto,
) {
  return requestClient.post<MarketingNewCustomerGrowthRecordDto>(
    '/marketing/new-customer/growth-record/write',
    data,
  );
}
