// 전자세금계산서 발행 추상화 — 어댑터 인터페이스
// 국세청 직접 연동(어댑터 C) / Popbill(B) / 자체 거래명세서(A) 모두 같은 인터페이스로 호출.

export type InvoiceKind = "TAX" | "TAXFREE" | "RECEIPT";
export type InvoiceType = "NORMAL" | "MODIFIED" | "CANCELED";
export type IssueMethod = "NTS" | "POPBILL" | "MANUAL";
export type EinvoiceStatus = "DRAFT" | "QUEUED" | "SENT" | "ACK" | "FAIL" | "CANCELED";

export interface InvoiceParty {
  bizNo: string;          // 사업자등록번호 (000-00-00000 또는 0000000000)
  name: string;
  ceoName?: string;
  address?: string;
  business?: string;      // 업태
  item?: string;          // 종목
  email?: string;
  phone?: string;
}

export interface InvoiceLine {
  seq: number;
  itemDate?: string;      // YYYY-MM-DD
  itemName: string;
  spec?: string;
  qty?: number;
  unitPrice?: number;
  supplyAmount: number;   // 공급가
  vatAmount: number;      // 세액 (면세는 0)
  remark?: string;
}

export interface InvoiceRequest {
  kind: InvoiceKind;              // TAX / TAXFREE / RECEIPT
  type: InvoiceType;              // NORMAL / MODIFIED / CANCELED
  issueDate: string;              // YYYY-MM-DD
  supplier: InvoiceParty;
  buyer: InvoiceParty;
  lines: InvoiceLine[];
  totalSupply: number;
  totalVat: number;
  totalAmount: number;
  memo?: string;
  // 수정/취소 시 원본 승인번호
  originNtsInvoiceNo?: string;
}

export interface InvoiceResponse {
  success: boolean;
  status: EinvoiceStatus;
  ntsInvoiceNo?: string;     // 국세청 승인번호 (24자리)
  responseCode?: string;
  responseMessage?: string;
  rawRequest?: string;       // 디버그용 (XML)
  rawResponse?: string;
}

export interface InvoiceProvider {
  readonly method: IssueMethod;
  /** 발행 (TAX/TAXFREE) */
  issue(req: InvoiceRequest): Promise<InvoiceResponse>;
  /** 발행 상태 조회 */
  status?(ntsInvoiceNo: string): Promise<InvoiceResponse>;
  /** 발행 취소 */
  cancel?(ntsInvoiceNo: string, reason?: string): Promise<InvoiceResponse>;
}
