/*
 * M1 홀 배정·자동 견적(SCR /halls) 실배선 API 계약.
 * 공용 클라이언트(../../api/client)만 사용 — client.ts·endpoints.ts·types.ts 무수정.
 * 정본: 백엔드 HallAssignController 시그니처
 *   - GET  /api/halls                                  → ApiResponse<Hall[]>
 *   - GET  /api/halls/assignments?from=&to=&eventId=   → ApiResponse<HallAssignment[]> (홀매니저)
 *   - GET  /api/halls/conflicts?hallId=&from=&to=&excludeEventId= → ApiResponse<Conflict[]> (홀매니저)
 *   - POST /api/halls/assignments                      → ApiResponse<HallAssignment> (겹침 409 CONFLICT)
 *   - DELETE /api/halls/assignments/{id}               → ApiResponse<void> (홀매니저)
 *   - POST /api/halls/quote                            → ApiResponse<QuoteResult> (로그인)
 */
import { api } from '../../api/client';

export interface Hall {
  id: string;
  exhibitionCenter: number;
  label: string;
  areaM2: number;
  floorType: string | null;
  boothCapacity: number | null;
}

export interface HallAssignment {
  id: string;
  eventId: string;
  eventName: string;
  hallId: string;
  hallLabel: string;
  primary: boolean;
  occupyStart: string | null;
  occupyEnd: string | null;
  status: string;
  note: string | null;
}

export interface Conflict {
  assignmentId: string;
  eventId: string;
  eventName: string;
  hallId: string;
  hallLabel: string;
  occupyStart: string;
  occupyEnd: string;
}

export interface AssignmentBody {
  eventId: string;
  hallId: string;
  primary: boolean;
  occupyStart?: string;
  occupyEnd?: string;
  note?: string;
}

export interface QuoteLine {
  code: string;
  label: string;
  basis: string;
  amount: number;
}

export interface QuoteResult {
  rulesetVersion: string;
  currency: string;
  days: number;
  lines: QuoteLine[];
  rentalSubtotal: number;
  total: number;
  depositMin: number;
  depositMax: number;
  capacityNote: string | null;
  disclaimer: string | null;
}

export interface QuoteBody {
  hallIds: string[];
  startDate: string;
  endDate: string;
  boothCount?: number;
}

/** 배정 모달 행사 선택용(카탈로그 /api/events 재사용, id·name·기간만 취함). */
export interface EventOption {
  id: string;
  name: string;
  startDate: string | null;
  endDate: string | null;
}

export const hallAssignApi = {
  halls: () => api.get<Hall[]>('/api/halls'),

  assignments: (from?: string, to?: string, eventId?: string) => {
    const qs = new URLSearchParams();
    if (from) qs.set('from', from);
    if (to) qs.set('to', to);
    if (eventId) qs.set('eventId', eventId);
    const q = qs.toString();
    return api.get<HallAssignment[]>(`/api/halls/assignments${q ? `?${q}` : ''}`);
  },

  conflicts: (hallId: string, from: string, to: string, excludeEventId?: string) => {
    const qs = new URLSearchParams({ hallId, from, to });
    if (excludeEventId) qs.set('excludeEventId', excludeEventId);
    return api.get<Conflict[]>(`/api/halls/conflicts?${qs.toString()}`);
  },

  assign: (body: AssignmentBody) => api.post<HallAssignment>('/api/halls/assignments', body),

  release: (id: string) => api.del<void>(`/api/halls/assignments/${encodeURIComponent(id)}`),

  quote: (body: QuoteBody) => api.post<QuoteResult>('/api/halls/quote', body),

  events: () => api.get<EventOption[]>('/api/events?size=500'),
};
