/*
 * 공개 입장권(티켓) API 클라이언트 — 전 경로 비인증 공개(/api/public/tickets/**).
 * 백엔드: com.zioinfo.kintex.ticket (V50). 응답은 ApiResponse.data 로 언랩됨.
 * 보안: 응답의 구매자 정보는 서버 마스킹 필드만 소비(원문 없음).
 */
import { api } from '../../api/client';

/** 판매 상품 — remaining/onSale/soldOut 은 서버 권위. */
export interface TicketProduct {
  id: string;
  eventId: string;
  code: string;
  kind: string; // GENERAL/STUDENT/GROUP/VIP/BUYER
  name: string;
  description: string | null;
  price: number;
  currency: string;
  saleStart: string | null;
  saleEnd: string | null;
  totalQty: number;
  remaining: number;
  maxPerOrder: number;
  onSale: boolean;
  soldOut: boolean;
  sortOrder: number;
}

export interface IssuedTicket {
  ticketCode: string;
  seq: number;
  used: boolean;
}

export interface TicketOrderRequest {
  productId: string;
  qty: number;
  buyerName: string;
  buyerContact: string;
  buyerEmail?: string;
  agreePrivacy: boolean;
  payMethod?: string; // card/easy/bank (무료권은 서버가 free 처리)
}

export interface TicketOrderResult {
  orderNo: string;
  status: string; // PAID
  eventId: string;
  productName: string;
  qty: number;
  unitPrice: number;
  totalAmount: number;
  payMethod: string | null;
  approvalNo: string | null;
  buyerNameMasked: string | null;
  buyerContactMasked: string | null;
  buyerEmailMasked: string | null;
  orderedAt: string | null;
  tickets: IssuedTicket[];
}

export interface TicketLookupResult {
  orderNo: string;
  status: string;
  eventId: string;
  productName: string;
  qty: number;
  unitPrice: number;
  totalAmount: number;
  payMethod: string | null;
  buyerNameMasked: string | null;
  buyerContactMasked: string | null;
  buyerEmailMasked: string | null;
  orderedAt: string | null;
  paidAt: string | null;
  tickets: IssuedTicket[];
}

const opt = { anonymous: true } as const;

export const ticketApi = {
  /** 행사 판매중 상품 목록(잔여수량·판매기간 서버 권위). */
  listProducts: (eventId: string) =>
    api.get<TicketProduct[]>(`/api/public/tickets/${encodeURIComponent(eventId)}`, opt),

  /** 예매(재고차감→PG승인→발권). 실패 시 봉투 error(code)로 강등. */
  createOrder: (eventId: string, payload: TicketOrderRequest) =>
    api.post<TicketOrderResult>(
      `/api/public/tickets/${encodeURIComponent(eventId)}/orders`,
      payload,
      opt,
    ),

  /** 예매 조회 — 주문번호 + 연락처(하이픈 무관). 불일치·부재는 NOT_FOUND. */
  lookup: (orderNo: string, contact: string) =>
    api.get<TicketLookupResult>(
      `/api/public/tickets/lookup?orderNo=${encodeURIComponent(orderNo)}&contact=${encodeURIComponent(contact)}`,
      opt,
    ),
};
