/**
 * 文件职责：堂食管理模块 API 与 DTO 定义。
 * 1. 维护区域、桌位、堂食设置类型。
 * 2. 提供查询、保存、删除、批量生成与复制接口。
 */
import { requestClient } from '#/api/request';

/** 桌位状态 */
export type DineInTableStatus = 'dining' | 'disabled' | 'free' | 'reserved';

/** 可编辑桌位状态（业务态由系统驱动，不在管理端直接设置） */
export type DineInEditableStatus = 'disabled' | 'free';

/** 堂食基础设置 */
export interface DineInBasicSettingsDto {
  /** 是否开启堂食 */
  enabled: boolean;
  /** 默认用餐时长（分钟） */
  defaultDiningMinutes: number;
  /** 超时提醒阈值（分钟） */
  overtimeReminderMinutes: number;
}

/** 堂食区域 */
export interface DineInAreaDto {
  /** 区域描述 */
  description: string;
  id: string;
  name: string;
  /** 数字越小越靠前 */
  sort: number;
}

/** 堂食桌位 */
export interface DineInTableDto {
  areaId: string;
  code: string;
  id: string;
  seats: number;
  status: DineInTableStatus;
  tags: string[];
}

/** 门店堂食设置聚合 */
export interface StoreDineInSettingsDto {
  areas: DineInAreaDto[];
  basicSettings: DineInBasicSettingsDto | null;
  isConfigured: boolean;
  storeId: string;
  tables: DineInTableDto[];
}

/** 保存堂食基础设置参数 */
export interface SaveStoreDineInBasicSettingsParams {
  basicSettings: DineInBasicSettingsDto;
  storeId: string;
}

/** 保存堂食区域参数 */
export interface SaveDineInAreaParams {
  area: Omit<DineInAreaDto, 'id'> & { id?: string };
  storeId: string;
}

/** 删除堂食区域参数 */
export interface DeleteDineInAreaParams {
  areaId: string;
  storeId: string;
}

/** 保存堂食桌位参数 */
export interface SaveDineInTableParams {
  storeId: string;
  table: Omit<DineInTableDto, 'id'> & { id?: string };
}

/** 删除堂食桌位参数 */
export interface DeleteDineInTableParams {
  storeId: string;
  tableId: string;
}

/** 批量生成桌位参数 */
export interface BatchCreateDineInTablesParams {
  areaId: string;
  codePrefix: string;
  count: number;
  seats: number;
  startNumber: number;
  storeId: string;
}

/** 批量生成桌位结果 */
export interface BatchCreateDineInTablesResultDto {
  createdTables: DineInTableDto[];
}

/** 复制堂食设置参数 */
export interface CopyStoreDineInSettingsParams {
  sourceStoreId: string;
  targetStoreIds: string[];
}

/** 获取门店堂食设置 */
export async function getStoreDineInSettingsApi(storeId: string) {
  return requestClient.get<StoreDineInSettingsDto>('/store/dinein', {
    params: { storeId },
  });
}

/** 保存门店堂食基础设置 */
export async function saveStoreDineInBasicSettingsApi(
  data: SaveStoreDineInBasicSettingsParams,
) {
  return requestClient.post('/store/dinein/basic/save', data);
}

/** 新增或编辑堂食区域 */
export async function saveDineInAreaApi(data: SaveDineInAreaParams) {
  return requestClient.post<DineInAreaDto>('/store/dinein/area/save', data);
}

/** 删除堂食区域 */
export async function deleteDineInAreaApi(data: DeleteDineInAreaParams) {
  return requestClient.post('/store/dinein/area/delete', data);
}

/** 新增或编辑堂食桌位 */
export async function saveDineInTableApi(data: SaveDineInTableParams) {
  return requestClient.post<DineInTableDto>('/store/dinein/table/save', data);
}

/** 删除堂食桌位 */
export async function deleteDineInTableApi(data: DeleteDineInTableParams) {
  return requestClient.post('/store/dinein/table/delete', data);
}

/** 批量生成堂食桌位 */
export async function batchCreateDineInTablesApi(
  data: BatchCreateDineInTablesParams,
) {
  return requestClient.post<BatchCreateDineInTablesResultDto>(
    '/store/dinein/table/batch-create',
    data,
  );
}

/** 复制堂食设置到其他门店 */
export async function copyStoreDineInSettingsApi(
  data: CopyStoreDineInSettingsParams,
) {
  return requestClient.post('/store/dinein/copy', data);
}
