import type { Dayjs } from 'dayjs';

import type {
  MemberPointMallExchangeType,
  MemberPointMallExpiryMode,
  MemberPointMallNotifyChannel,
  MemberPointMallPickupMethod,
  MemberPointMallProductDto,
  MemberPointMallProductStatus,
  MemberPointMallRecordDetailDto,
  MemberPointMallRecordDto,
  MemberPointMallRecordStatsDto,
  MemberPointMallRecordStatus,
  MemberPointMallRedeemType,
  MemberPointMallRuleStatsDto,
  MemberPointMallVerifyMethod,
} from '#/api/member/points-mall';

/** 页面主 Tab。 */
export type PointMallTabKey = 'products' | 'records' | 'rules';

/** 规则编辑表单。 */
export interface PointMallRuleForm {
  consumeAmountPerStep: null | number;
  consumeRewardPointsPerStep: null | number;
  expiryMode: MemberPointMallExpiryMode;
  isConsumeRewardEnabled: boolean;
  isRegisterRewardEnabled: boolean;
  isReviewRewardEnabled: boolean;
  isSigninRewardEnabled: boolean;
  registerRewardPoints: null | number;
  reviewRewardPoints: null | number;
  signinRewardPoints: null | number;
}

/** 商品筛选表单。 */
export interface PointMallProductFilterForm {
  keyword: string;
  status: '' | MemberPointMallProductStatus;
}

/** 商品编辑表单。 */
export interface PointMallProductEditorForm {
  cashAmount: null | number;
  couponTemplateId: string;
  couponTemplateName: string;
  description: string;
  exchangeType: MemberPointMallExchangeType;
  imageUrl: string;
  name: string;
  notifyChannels: MemberPointMallNotifyChannel[];
  perMemberLimit: null | number;
  physicalName: string;
  pickupMethod: MemberPointMallPickupMethod;
  pointMallProductId: string;
  productId: string;
  productName: string;
  redeemType: MemberPointMallRedeemType;
  requiredPoints: null | number;
  status: MemberPointMallProductStatus;
  stockTotal: null | number;
}

/** 记录筛选表单。 */
export interface PointMallRecordFilterForm {
  dateRange: [Dayjs, Dayjs] | null;
  keyword: string;
  redeemType: '' | MemberPointMallRedeemType;
  status: '' | MemberPointMallRecordStatus;
}

/** 记录分页状态。 */
export interface PointMallRecordPager {
  items: MemberPointMallRecordDto[];
  page: number;
  pageSize: number;
  totalCount: number;
}

/** 核销表单。 */
export interface PointMallVerifyForm {
  verifyMethod: MemberPointMallVerifyMethod;
  verifyRemark: string;
}

/** 商品卡片模型。 */
export type PointMallProductCardViewModel = MemberPointMallProductDto;

/** 规则统计模型。 */
export type PointMallRuleStatsViewModel = MemberPointMallRuleStatsDto;

/** 记录统计模型。 */
export type PointMallRecordStatsViewModel = MemberPointMallRecordStatsDto;

/** 记录详情模型。 */
export type PointMallRecordDetailViewModel = MemberPointMallRecordDetailDto;

/** 创建默认规则表单。 */
export function createDefaultPointMallRuleForm(): PointMallRuleForm {
  return {
    isConsumeRewardEnabled: true,
    consumeAmountPerStep: 1,
    consumeRewardPointsPerStep: 1,
    isReviewRewardEnabled: true,
    reviewRewardPoints: 10,
    isRegisterRewardEnabled: true,
    registerRewardPoints: 100,
    isSigninRewardEnabled: false,
    signinRewardPoints: 5,
    expiryMode: 'yearly_clear',
  };
}

/** 创建默认商品筛选表单。 */
export function createDefaultPointMallProductFilterForm(): PointMallProductFilterForm {
  return {
    status: '',
    keyword: '',
  };
}

/** 创建默认商品编辑表单。 */
export function createDefaultPointMallProductEditorForm(): PointMallProductEditorForm {
  return {
    pointMallProductId: '',
    redeemType: 'product',
    productId: '',
    productName: '',
    couponTemplateId: '',
    couponTemplateName: '',
    physicalName: '',
    pickupMethod: 'store_pickup',
    name: '',
    imageUrl: '',
    exchangeType: 'points',
    requiredPoints: null,
    cashAmount: null,
    stockTotal: null,
    perMemberLimit: null,
    description: '',
    notifyChannels: ['in_app'],
    status: 'enabled',
  };
}

/** 创建默认记录筛选表单。 */
export function createDefaultPointMallRecordFilterForm(): PointMallRecordFilterForm {
  return {
    redeemType: '',
    status: '',
    keyword: '',
    dateRange: null,
  };
}

/** 创建默认记录分页。 */
export function createDefaultPointMallRecordPager(): PointMallRecordPager {
  return {
    items: [],
    page: 1,
    pageSize: 10,
    totalCount: 0,
  };
}

/** 创建默认规则统计。 */
export function createDefaultPointMallRuleStats(): PointMallRuleStatsViewModel {
  return {
    totalIssuedPoints: 0,
    redeemedPoints: 0,
    pointMembers: 0,
    redeemRate: 0,
  };
}

/** 创建默认记录统计。 */
export function createDefaultPointMallRecordStats(): PointMallRecordStatsViewModel {
  return {
    todayRedeemCount: 0,
    pendingPhysicalCount: 0,
    currentMonthUsedPoints: 0,
  };
}

/** 创建默认核销表单。 */
export function createDefaultPointMallVerifyForm(): PointMallVerifyForm {
  return {
    verifyMethod: 'manual',
    verifyRemark: '',
  };
}
