/*
 * M8 물류 확장(F041 장비 신청 · F042 렌탈 주문 · F043 선입고 트래킹) 실배선 API 계약.
 * 공용 클라이언트(../../api/client)만 사용 — client.ts·endpoints.ts·types.ts 무수정.
 * 정본: 백엔드 LogisticsExtController 시그니처 (/api/events/{eventId}/logistics/*)
 *   - GET  /ext                              → ApiResponse<LogisticsExtBoard>
 *   - POST /equipment-requests               → 장비 신청(멤버, 금액 서버 계산)
 *   - POST /equipment-requests/{id}/status   → 승인/반려/처리완료(매니저)
 *   - POST /rental-orders                    → 렌탈 주문(멤버, 기간·금액 서버 산출)
 *   - POST /rental-orders/{id}/status        → 확정/취소/납품(매니저)
 *   - POST /inbound                          → 운송장 등록(멤버)
 *   - POST /inbound/{id}/status              → 입고예정/창고도착/부스전달 전이(매니저)
 */
import { api } from '../../api/client';

export interface EquipmentItem {
  id: string;
  code: string;
  name: string;
  category: string;
  unit: string;
  unitPrice: number;
  spec: string | null;
}
export interface EquipmentRequestView {
  id: string;
  equipmentId: string;
  equipmentName: string;
  category: string | null;
  unitPrice: number;
  boothNo: string | null;
  quantity: number;
  useDate: string | null;
  days: number;
  amount: number;
  status: 'requested' | 'approved' | 'rejected' | 'done';
  note: string | null;
  rejectReason: string | null;
  createdAt: string;
}
export interface RentalItem {
  id: string;
  code: string;
  name: string;
  category: string;
  unit: string;
  dailyPrice: number;
  spec: string | null;
}
export interface RentalOrderView {
  id: string;
  itemId: string;
  itemName: string;
  category: string | null;
  dailyPrice: number;
  boothNo: string | null;
  quantity: number;
  startDate: string | null;
  endDate: string | null;
  days: number;
  amount: number;
  status: 'ordered' | 'confirmed' | 'cancelled' | 'delivered';
  note: string | null;
  createdAt: string;
}
export interface InboundView {
  id: string;
  trackingNo: string;
  carrier: string | null;
  boothNo: string | null;
  itemDesc: string | null;
  quantity: number;
  status: 'registered' | 'inbound_scheduled' | 'arrived_warehouse' | 'delivered_booth';
  etaDate: string | null;
  warehouseAt: string | null;
  deliveredAt: string | null;
  note: string | null;
  createdAt: string;
}
export interface LogisticsExtBoard {
  canManage: boolean;
  equipmentCatalog: EquipmentItem[];
  equipmentRequests: EquipmentRequestView[];
  rentalCatalog: RentalItem[];
  rentalOrders: RentalOrderView[];
  inbound: InboundView[];
}

export interface EquipmentRequestBody {
  equipmentId: string;
  boothNo?: string;
  quantity: number;
  useDate?: string;
  days: number;
  note?: string;
}
export interface RentalOrderBody {
  itemId: string;
  boothNo?: string;
  quantity: number;
  startDate?: string;
  endDate?: string;
  days: number;
  note?: string;
}
export interface InboundBody {
  trackingNo: string;
  carrier?: string;
  boothNo?: string;
  itemDesc?: string;
  quantity: number;
  etaDate?: string;
  note?: string;
}
export interface StatusDecisionBody {
  status: string;
  reason?: string;
}
export interface InboundStatusBody {
  status: string;
  note?: string;
}

const base = (eventId: string) => `/api/events/${encodeURIComponent(eventId)}/logistics`;

export const logisticsExtApi = {
  board: (eventId: string) => api.get<LogisticsExtBoard>(`${base(eventId)}/ext`),

  requestEquipment: (eventId: string, body: EquipmentRequestBody) =>
    api.post<EquipmentRequestView>(`${base(eventId)}/equipment-requests`, body),
  decideEquipment: (eventId: string, id: string, body: StatusDecisionBody) =>
    api.post<EquipmentRequestView>(
      `${base(eventId)}/equipment-requests/${encodeURIComponent(id)}/status`,
      body,
    ),

  orderRental: (eventId: string, body: RentalOrderBody) =>
    api.post<RentalOrderView>(`${base(eventId)}/rental-orders`, body),
  decideRental: (eventId: string, id: string, body: StatusDecisionBody) =>
    api.post<RentalOrderView>(
      `${base(eventId)}/rental-orders/${encodeURIComponent(id)}/status`,
      body,
    ),

  registerInbound: (eventId: string, body: InboundBody) =>
    api.post<InboundView>(`${base(eventId)}/inbound`, body),
  advanceInbound: (eventId: string, id: string, body: InboundStatusBody) =>
    api.post<InboundView>(`${base(eventId)}/inbound/${encodeURIComponent(id)}/status`, body),
};
