/*
 * F005/F006 부스 판매 인벤토리·온라인 셀프 판매 실 API 클라이언트.
 * 공용 client(../../api/client)의 api 만 사용 — client.ts·endpoints.ts·types.ts 무수정.
 * 정본: 백엔드 com.zioinfo.kintex.boothsales (BoothSalesController) 시그니처.
 *   - GET  /api/booth-sales/inventory?eventId=&hallId=  → ApiResponse<InventoryResponse>
 *   - POST /api/booth-sales/hold      { eventId, boothId }            → BoothSaleResult (이중 hold 409)
 *   - POST /api/booth-sales/purchase  { eventId, boothId, company? }  → BoothSaleResult (만료 409)
 *   - POST /api/booth-sales/release   { eventId, boothId }            → BoothSaleResult
 *   - POST /api/booth-sales/block     { eventId, boothId, blocked, reason? } → BoothSaleResult (매니저)
 *   - POST /api/booth-sales/price     { eventId, boothId, unitPrice } → BoothSaleResult (매니저)
 *   - POST /api/booth-sales/settle?eventId=  → GenerateResult (매니저, M9 정산 연계)
 *
 * ★ 금액은 서버 계산이 권위 — 프론트는 서버가 준 원 단위(number)를 표시만 한다(재계산 없음).
 */
import { api } from '../../api/client';

export type BoothSaleStatus = 'available' | 'held' | 'sold' | 'blocked';

export interface BoothSaleUnit {
  boothId: string;
  boothNo: string | null;
  boothType: string | null;
  hallId: string;
  hallLabel: string;
  areaM2: number;
  status: BoothSaleStatus;
  unitPrice: number;
  heldByMe: boolean;
  heldUntil: string | null;
  soldToCompany: string | null;
}

export interface InventoryResponse {
  eventId: string;
  hallId: string | null;
  rulesetVersion: string;
  ratePerM2: number;
  available: number;
  held: number;
  sold: number;
  blocked: number;
  canManage: boolean;
  holdTtlMinutes: number;
  units: BoothSaleUnit[];
}

export interface BoothSaleResult {
  action: 'held' | 'purchased' | 'released' | 'blocked' | 'unblocked' | 'priced';
  unit: BoothSaleUnit;
}

export interface SettleResult {
  eventId: string;
  created: number;
  skipped: number;
}

export const boothSalesApi = {
  inventory: (eventId: string, hallId?: string | null) => {
    const qs = new URLSearchParams({ eventId });
    if (hallId) qs.set('hallId', hallId);
    return api.get<InventoryResponse>(`/api/booth-sales/inventory?${qs.toString()}`);
  },

  hold: (eventId: string, boothId: string) =>
    api.post<BoothSaleResult>('/api/booth-sales/hold', { eventId, boothId }),

  purchase: (eventId: string, boothId: string, company?: string) =>
    api.post<BoothSaleResult>('/api/booth-sales/purchase', { eventId, boothId, company }),

  release: (eventId: string, boothId: string) =>
    api.post<BoothSaleResult>('/api/booth-sales/release', { eventId, boothId }),

  block: (eventId: string, boothId: string, blocked: boolean, reason?: string) =>
    api.post<BoothSaleResult>('/api/booth-sales/block', { eventId, boothId, blocked, reason }),

  price: (eventId: string, boothId: string, unitPrice: number | null) =>
    api.post<BoothSaleResult>('/api/booth-sales/price', { eventId, boothId, unitPrice }),

  settle: (eventId: string) =>
    api.post<SettleResult>(`/api/booth-sales/settle?eventId=${encodeURIComponent(eventId)}`),
};
