/*
 * 관람객 대면 라이브 정보 API — 주차 현황(F-C2)·혼잡 안내(F-C3)·주차권.
 * 백엔드 계약: _workspace/parking_congestion_contract.md
 *   GET  /api/public/parking/lots?eventId    (공개)
 *   GET  /api/public/congestion?eventId       (공개)
 *   POST /api/parking/passes                  (인증)
 *   GET  /api/parking/passes/me               (인증)
 * 보안: 차량번호·PII 원문 없음(서버 마스킹 필드만).
 */
import { api } from '../../api/client';

const pub = { anonymous: true } as const;

export interface ParkingLotStatus {
  lotId: string;
  code: string;
  name: string;
  exhibitionCenter: number | null;
  totalCapacity: number;
  occupied: number;
  available: number;
  occupancyPercent: number;
  congestionLevel: 'FREE' | 'NORMAL' | 'BUSY';
  congestionLabel: string;
  hourlyRate: number;
  dailyMax: number | null;
  passPrice: number;
  note: string | null;
  updatedAt: string;
}

export interface CongestionArea {
  id: string;
  label: string;
  level: 'FREE' | 'NORMAL' | 'BUSY';
  levelLabel: string;
  occupancyPercent: number | null;
}
export interface CongestionOverview {
  eventId: string;
  overallLevel: 'FREE' | 'NORMAL' | 'BUSY';
  overallLabel: string;
  onSiteCount: number;
  entryGates: CongestionArea[];
  parking: CongestionArea[];
  popularSessions: CongestionArea[];
  updatedAt: string;
}

export interface ParkingPass {
  passNo: string;
  status: string; // PAID/CANCELLED/USED
  lotId: string;
  lotName: string;
  eventId: string | null;
  useDate: string;
  vehiclePlateMasked: string | null;
  amount: number;
  payMethod: string;
  payApprovalNo: string;
  issuedAt: string;
}
export interface MyPasses {
  passes: ParkingPass[];
}
export interface PurchasePassRequest {
  lotId: string;
  useDate: string;
  eventId?: string;
  vehiclePlate?: string;
  payMethod?: string;
}

export const liveApi = {
  parkingLots: (eventId?: string) =>
    api.get<ParkingLotStatus[]>(
      `/api/public/parking/lots${eventId ? `?eventId=${encodeURIComponent(eventId)}` : ''}`,
      pub,
    ),
  congestion: (eventId: string) =>
    api.get<CongestionOverview>(`/api/public/congestion?eventId=${encodeURIComponent(eventId)}`, pub),

  // 인증(로그인 관람객)
  myPasses: () => api.get<MyPasses>('/api/parking/passes/me'),
  purchasePass: (payload: PurchasePassRequest) =>
    api.post<ParkingPass>('/api/parking/passes', payload),
};
