/**
 * 세금계산서 발행 - 타입 및 상수 정의
 */

// ===== 세금계산서 상태 =====
export type TaxInvoiceStatus = 'draft' | 'issued' | 'nts_sent' | 'error';

export const TAX_INVOICE_STATUS_MAP: Record<TaxInvoiceStatus, { label: string; color: string }> = {
  draft: { label: '작성중', color: 'bg-gray-100 text-gray-700' },
  issued: { label: '발행완료', color: 'bg-blue-100 text-blue-700' },
  nts_sent: { label: '국세청전송', color: 'bg-green-100 text-green-700' },
  error: { label: '오류', color: 'bg-red-100 text-red-700' },
};

// ===== 사업자 정보 (공급자/공급받는자 공통) =====
export interface BusinessEntity {
  businessNumber: string;
  companyName: string;
  representativeName: string;
  address: string;
  businessType: string;
  businessItem: string;
  contactName: string;
  contactPhone: string;
  contactEmail: string;
}

// ===== 품목 행 =====
export interface TaxInvoiceItem {
  id: string;
  month: string;
  day: string;
  itemName: string;
  specification: string;
  quantity: number;
  unitPrice: number;
  supplyAmount: number;
  taxAmount: number;
  totalAmount: number;
  taxType: 'taxable' | 'taxFree';
}

// ===== 목록용 레코드 =====
export interface TaxInvoiceRecord {
  id: string;
  invoiceNumber: string;
  vendorName: string;
  vendorBusinessNumber: string;
  writeDate: string;
  sendDate: string | null;
  supplyAmount: number;
  taxAmount: number;
  totalAmount: number;
  status: TaxInvoiceStatus;
}

// ===== 발행 폼 데이터 =====
export interface TaxInvoiceFormData {
  supplier: BusinessEntity;
  receiver: BusinessEntity;
  writeDate: string;
  items: TaxInvoiceItem[];
  memo: string;
}

// ===== 공급자 설정 =====
export type SupplierSettings = BusinessEntity;

// ===== 거래처 검색 결과 =====
export interface VendorSearchItem {
  id: string;
  companyName: string;
  businessNumber: string;
  representativeName: string;
  address: string;
  businessType: string;
  businessItem: string;
  contactName: string;
  contactPhone: string;
  contactEmail: string;
}

// ===== 필터 상태 =====
export interface FilterState {
  dateType: string;
  startDate: string;
  endDate: string;
  vendorSearch: string;
  status: string;
  sortBy: string;
  sortOrder: string;
}

// ===== Select 옵션 상수 =====
export const DATE_TYPE_OPTIONS = [
  { value: 'write', label: '작성일자' },
  { value: 'send', label: '전송일자' },
];

export const STATUS_OPTIONS = [
  { value: 'all', label: '전체' },
  { value: 'draft', label: '작성중' },
  { value: 'issued', label: '발행완료' },
  { value: 'nts_sent', label: '국세청전송' },
  { value: 'error', label: '오류' },
];

export const SORT_BY_OPTIONS = [
  { value: 'writeDate', label: '작성일자' },
  { value: 'sendDate', label: '전송일자' },
  { value: 'totalAmount', label: '합계금액' },
  { value: 'vendorName', label: '거래처명' },
];

export const SORT_ORDER_OPTIONS = [
  { value: 'desc', label: '내림차순' },
  { value: 'asc', label: '오름차순' },
];

export const TAX_TYPE_OPTIONS = [
  { value: 'taxable', label: '과세' },
  { value: 'taxFree', label: '면세' },
];

// ===== 빈 품목 행 생성 =====
export function createEmptyItem(): TaxInvoiceItem {
  return {
    id: crypto.randomUUID(),
    month: '',
    day: '',
    itemName: '',
    specification: '',
    quantity: 0,
    unitPrice: 0,
    supplyAmount: 0,
    taxAmount: 0,
    totalAmount: 0,
    taxType: 'taxable',
  };
}

// ===== 빈 사업자 정보 =====
export function createEmptyBusinessEntity(): BusinessEntity {
  return {
    businessNumber: '',
    companyName: '',
    representativeName: '',
    address: '',
    businessType: '',
    businessItem: '',
    contactName: '',
    contactPhone: '',
    contactEmail: '',
  };
}
