/*
 * M8 반입/반출 도크 예약(SCR-25) 실배선 API 계약.
 * 공용 클라이언트(../../api/client)만 사용 — client.ts·endpoints.ts·types.ts 무수정.
 * 정본: 백엔드 LogisticsController 시그니처
 *   - GET  /api/events/{eventId}/docks?direction=in|out&date=YYYY-MM-DD → ApiResponse<DockBoard>
 *   - POST /api/events/{eventId}/dock-reservations                      → ApiResponse<GridReservation> (중복 409)
 *   - GET  /api/events/{eventId}/dock-forecast?date=YYYY-MM-DD          → ApiResponse<Forecast>(fallback 폴백)
 */
import { api } from '../../api/client';

export interface Dock {
  id: string;
  dockNo: number;
  label: string;
  heavyPriority: boolean;
}
export interface GridReservation {
  id: string;
  dockIndex: number;
  start: number;
  span: number;
  label: string;
  tone: 'booked' | 'priority';
}
export interface DockBoard {
  direction: 'in' | 'out';
  date: string;
  gridStartHour: number;
  hours: number;
  docks: Dock[];
  reservations: GridReservation[];
}

export interface ReservationBody {
  direction: 'in' | 'out';
  date: string;
  dockIndex: number;
  startHour: number;
  span?: number;
  vehicleNo?: string;
  weight?: string;
  item?: string;
  forklift?: boolean;
  companyName?: string;
}

export interface Forecast {
  hourly: number[];
  fallback: boolean;
  peakLabel: string;
}

export const logisticsApi = {
  board: (eventId: string, direction: 'in' | 'out', date?: string) => {
    const qs = new URLSearchParams({ direction });
    if (date) qs.set('date', date);
    return api.get<DockBoard>(`/api/events/${encodeURIComponent(eventId)}/docks?${qs.toString()}`);
  },
  reserve: (eventId: string, body: ReservationBody) =>
    api.post<GridReservation>(`/api/events/${encodeURIComponent(eventId)}/dock-reservations`, body),
  forecast: (eventId: string, date: string) =>
    api.get<Forecast>(
      `/api/events/${encodeURIComponent(eventId)}/dock-forecast?date=${encodeURIComponent(date)}`,
    ),
};
