/**
 * 文件职责：自提设置模块 API 与 DTO 定义。
 * 1. 维护基本设置、大时段、精细规则与预览类型。
 * 2. 提供查询、保存与复制自提设置接口。
 */
import { requestClient } from '#/api/request';

/** 自提模式 */
export type PickupMode = 'big' | 'fine';

/** 可选星期（0=周一，6=周日） */
export type PickupWeekDay = 0 | 1 | 2 | 3 | 4 | 5 | 6;

/** 自提基本设置 */
export interface PickupBasicSettingsDto {
  /** 是否允许当天自提 */
  allowSameDayPickup: boolean;
  /** 可预约天数 */
  bookingDays: number;
  /** 单笔最大数量，null 代表不限制 */
  maxItemsPerOrder: null | number;
}

/** 大时段模式单条配置 */
export interface PickupSlotDto {
  capacity: number;
  cutoffMinutes: number;
  dayOfWeeks: PickupWeekDay[];
  enabled: boolean;
  endTime: string;
  id: string;
  name: string;
  /** 当前已预约数量（用于展示进度） */
  reservedCount: number;
  startTime: string;
}

/** 精细时段规则 */
export interface PickupFineRuleDto {
  /** 适用星期 */
  dayOfWeeks: PickupWeekDay[];
  /** 每日结束时间 HH:mm */
  dayEndTime: string;
  /** 每日开始时间 HH:mm */
  dayStartTime: string;
  /** 时间间隔（分钟） */
  intervalMinutes: number;
  /** 最少提前预约小时数 */
  minAdvanceHours: number;
  /** 每个窗口容量 */
  slotCapacity: number;
}

/** 预览时段状态 */
export type PickupPreviewStatus = 'almost' | 'available' | 'expired' | 'full';

/** 预览时段 */
export interface PickupPreviewSlotDto {
  remainingCount: number;
  status: PickupPreviewStatus;
  time: string;
}

/** 预览日 */
export interface PickupPreviewDayDto {
  date: string;
  label: string;
  slots: PickupPreviewSlotDto[];
  subLabel: string;
}

/** 门店自提设置聚合 */
export interface StorePickupSettingsDto {
  basicSettings: null | PickupBasicSettingsDto;
  bigSlots: PickupSlotDto[];
  fineRule: null | PickupFineRuleDto;
  isConfigured: boolean;
  mode: null | PickupMode;
  previewDays: PickupPreviewDayDto[];
  storeId: string;
}

/** 保存基本设置参数 */
export interface SavePickupBasicSettingsParams {
  basicSettings: PickupBasicSettingsDto;
  mode?: PickupMode;
  storeId: string;
}

/** 保存大时段参数 */
export interface SavePickupSlotsParams {
  mode?: PickupMode;
  slots: PickupSlotDto[];
  storeId: string;
}

/** 保存精细规则参数 */
export interface SavePickupFineRuleParams {
  fineRule: PickupFineRuleDto;
  mode?: PickupMode;
  storeId: string;
}

/** 保存自提模式参数 */
export interface SavePickupModeParams {
  mode: PickupMode;
  storeId: string;
}

/** 复制自提设置参数 */
export interface CopyStorePickupSettingsParams {
  sourceStoreId: string;
  targetStoreIds: string[];
}

/** 获取门店自提设置 */
export async function getStorePickupSettingsApi(storeId: string) {
  return requestClient.get<StorePickupSettingsDto>('/store/pickup', {
    params: { storeId },
  });
}

/** 保存基本设置 */
export async function savePickupBasicSettingsApi(
  data: SavePickupBasicSettingsParams,
) {
  return requestClient.post('/store/pickup/basic/save', data);
}

/** 保存大时段配置 */
export async function savePickupSlotsApi(data: SavePickupSlotsParams) {
  return requestClient.post('/store/pickup/slots/save', data);
}

/** 保存精细规则 */
export async function savePickupFineRuleApi(data: SavePickupFineRuleParams) {
  return requestClient.post('/store/pickup/fine-rule/save', data);
}

/** 保存自提模式 */
export async function savePickupModeApi(data: SavePickupModeParams) {
  return requestClient.post('/store/pickup/mode/save', data);
}

/** 复制到其他门店 */
export async function copyStorePickupSettingsApi(
  data: CopyStorePickupSettingsParams,
) {
  return requestClient.post('/store/pickup/copy', data);
}
