/**
 * 邀请有礼功能相关API
 */

import { get, post } from '@/utils/request';

export enum InviteStatus {
  PENDING = 'pending',
  ACTIVATED = 'activated',
  REWARDED = 'rewarded',
  FAILED = 'failed'
}

export enum InviteStatusLabel {
  PENDING = '待激活',
  ACTIVATED = '已激活',
  REWARDED = '奖励已发放',
  FAILED = '发放失败'
}

export interface InviteCodeResponse {
  code: string;
  inviteUrl: string;
  expireTime?: string;
}

export interface BindInviteParams {
  inviteCode: string;
}

export interface BindInviteResponse {
  success: boolean;
  message: string;
  // 双向奖励发放结果
  rewardResult?: {
    inviteeCouponGranted: boolean;     // 是否已向被邀请人发券
    inviteeCouponName?: string;        // 被邀请人获得的优惠券名称
    inviteeCouponAmount?: number;      // 被邀请人获得的优惠券金额
    inviterCouponPending: boolean;     // 邀请人奖励是否待发放（需满足条件）
    triggerCondition?: string;         // 触发条件说明
  };
}

export interface InviteRecord {
  id: string;
  inviteePhone: string;
  inviteeName?: string;
  status: InviteStatus;
  inviteTime: string;
  activateTime?: string;
  rewardTime?: string;
  couponName?: string;
  couponAmount?: number;
  rewardType?: 'inviter' | 'invitee';  // 奖励类型：inviter=邀请人推荐奖励, invitee=被邀请人新人券
}

export interface InviteRecordListResponse {
  list: InviteRecord[];
  total: number;
  page: number;
  pageSize: number;
}

export interface InviteQueryParams {
  page?: number;
  pageSize?: number;
  status?: InviteStatus;
}

export interface InviteStatistics {
  totalInvite: number;
  validInvite: number;
  rewardCount: number;
  pendingCount: number;
  totalRewardAmount: number;
  // 双向奖励统计
  inviterRewardCount?: number;       // 邀请人推荐奖励数量
  inviteeRewardCount?: number;       // 被邀请人新人券发放数量
  inviterRewardAmount?: number;      // 邀请人推荐奖励总金额
  inviteeRewardAmount?: number;      // 被邀请人新人券总金额
}

export interface ActivityInfo {
  activityId: string;
  title: string;
  description: string;
  couponName: string;
  couponAmount: number;
  startTime: string;
  endTime: string;
  rules: string[];
  isActive: boolean;
  // 双向奖励配置
  dualRewardConfig?: {
    inviteeCouponName: string;      // 被邀请人新人券名称
    inviteeCouponAmount: number;    // 被邀请人新人券金额
    inviterCouponName: string;     // 邀请人推荐奖励券名称
    inviterCouponAmount: number;   // 邀请人推荐奖励券金额
    triggerCondition: string;       // 触发条件描述（如"好友完成首单"）
  };
}

export interface PosterResponse {
  posterUrl: string;
  expireTime: string;
}

export async function getInviteCode(): Promise<InviteCodeResponse> {
  return get('/invite/code');
}

export async function bindInviteRelation(params: BindInviteParams): Promise<BindInviteResponse> {
  return post('/invite/bind', params);
}

export async function getMyInviteRecords(params: InviteQueryParams = {}): Promise<InviteRecordListResponse> {
  return get('/invite/my-records', params);
}

export async function getMyInviteStatistics(): Promise<InviteStatistics> {
  return get('/invite/my-statistics');
}

export async function getActivityInfo(): Promise<ActivityInfo> {
  return get('/invite/activity-info');
}

export async function generatePoster(): Promise<PosterResponse> {
  return get('/invite/poster');
}
