/**
 * 상품권 관리 - 타입 및 상수 정의
 */

// ===== 상품권 상태 =====
export type GiftCertificateStatus = 'holding' | 'used' | 'disposed';

export const STATUS_MAP: Record<GiftCertificateStatus, { label: string; color: string }> = {
  holding: { label: '보유', color: 'bg-blue-100 text-blue-700' },
  used: { label: '사용', color: 'bg-green-100 text-green-700' },
  disposed: { label: '폐기', color: 'bg-red-100 text-red-700' },
};

// ===== 구입목적 =====
export type PurchasePurpose = 'promotion' | 'gift' | 'entertainment' | 'other';

// ===== 접대비 해당 여부 =====
export type EntertainmentExpense = 'applicable' | 'not_applicable';

// ===== 목록용 레코드 =====
export interface GiftCertificateRecord {
  id: string;
  serialNumber: string;
  name: string;
  faceValue: number;
  purchaseDate: string;
  usedDate: string | null;
  status: GiftCertificateStatus;
  entertainmentExpense: EntertainmentExpense;
}

// ===== 상세/폼 데이터 =====
export interface GiftCertificateFormData {
  serialNumber: string;
  name: string;
  faceValue: number;
  vendorId: string;
  vendorName: string;
  purchaseDate: string;
  purchasePurpose: PurchasePurpose;
  entertainmentExpense: EntertainmentExpense;
  status: GiftCertificateStatus;
  // 상품권 정보 (액면가 50만원 이상 시 필수)
  usedDate: string;
  recipientName: string;
  recipientOrganization: string;
  usageDescription: string;
  memo: string;
}

// ===== 필터 상태 =====
export interface FilterState {
  startDate: string;
  endDate: string;
  status: string;
  entertainmentExpense: string;
}

// ===== Select 옵션 상수 =====
export const STATUS_OPTIONS = [
  { value: 'all', label: '전체' },
  { value: 'holding', label: '보유' },
  { value: 'used', label: '사용' },
  { value: 'disposed', label: '폐기' },
];

export const PURCHASE_PURPOSE_OPTIONS = [
  { value: 'promotion', label: '판촉' },
  { value: 'gift', label: '선물' },
  { value: 'entertainment', label: '접대' },
  { value: 'other', label: '기타' },
];

export const ENTERTAINMENT_EXPENSE_OPTIONS = [
  { value: 'applicable', label: '해당' },
  { value: 'not_applicable', label: '비해당' },
];

export const ENTERTAINMENT_FILTER_OPTIONS = [
  { value: 'all', label: '접대비 전체' },
  { value: 'applicable', label: '해당' },
  { value: 'not_applicable', label: '비해당' },
];

// ===== 초기 폼 데이터 =====
export function createEmptyFormData(): GiftCertificateFormData {
  return {
    serialNumber: '',
    name: '',
    faceValue: 0,
    vendorId: '',
    vendorName: '',
    purchaseDate: '',
    purchasePurpose: 'promotion',
    entertainmentExpense: 'not_applicable',
    status: 'holding',
    usedDate: '',
    recipientName: '',
    recipientOrganization: '',
    usageDescription: '',
    memo: '',
  };
}

// ===== 액면가 50만원 기준 =====
export const FACE_VALUE_THRESHOLD = 500000;

// ===== Loan API 응답 타입 =====
export interface LoanApiData {
  id: number;
  tenant_id: number;
  user_id: number | null;
  loan_date: string;
  amount: string;
  purpose: string | null;
  settlement_date: string | null;
  settlement_amount: string | null;
  status: string;
  category: string | null;
  metadata: {
    serial_number?: string;
    cert_name?: string;
    vendor_id?: string;
    vendor_name?: string;
    purchase_purpose?: string;
    entertainment_expense?: string;
    recipient_name?: string;
    recipient_organization?: string;
    usage_description?: string;
    memo?: string;
  } | null;
  withdrawal_id: number | null;
  created_by: number | null;
  updated_by: number | null;
  user?: { id: number; name: string; email: string } | null;
  creator?: { id: number; name: string } | null;
}

// ===== API → 프론트 변환 (목록용) =====
export function transformApiToRecord(api: LoanApiData): GiftCertificateRecord {
  const meta = api.metadata ?? {};
  return {
    id: String(api.id),
    serialNumber: meta.serial_number ?? '',
    name: meta.cert_name ?? '',
    faceValue: parseFloat(api.amount) || 0,
    purchaseDate: api.loan_date ?? '',
    usedDate: api.settlement_date ?? null,
    status: (api.status as GiftCertificateStatus) ?? 'holding',
    entertainmentExpense: (meta.entertainment_expense as EntertainmentExpense) ?? 'not_applicable',
  };
}

// ===== API → 프론트 변환 (상세/폼용) =====
export function transformApiToFormData(api: LoanApiData): GiftCertificateFormData {
  const meta = api.metadata ?? {};
  return {
    serialNumber: meta.serial_number ?? '',
    name: meta.cert_name ?? '',
    faceValue: parseFloat(api.amount) || 0,
    vendorId: meta.vendor_id ?? '',
    vendorName: meta.vendor_name ?? '',
    purchaseDate: api.loan_date ?? '',
    purchasePurpose: (meta.purchase_purpose as PurchasePurpose) ?? 'promotion',
    entertainmentExpense: (meta.entertainment_expense as EntertainmentExpense) ?? 'not_applicable',
    status: (api.status as GiftCertificateStatus) ?? 'holding',
    usedDate: api.settlement_date ?? '',
    recipientName: meta.recipient_name ?? '',
    recipientOrganization: meta.recipient_organization ?? '',
    usageDescription: meta.usage_description ?? '',
    memo: meta.memo ?? '',
  };
}

// ===== 프론트 → API 변환 =====
export function transformFormToApi(data: GiftCertificateFormData): Record<string, unknown> {
  return {
    loan_date: data.purchaseDate,
    amount: data.faceValue,
    purpose: data.usageDescription || null,
    category: 'gift_certificate',
    status: data.status,
    settlement_date: data.usedDate || null,
    metadata: {
      serial_number: data.serialNumber || null,
      cert_name: data.name || null,
      vendor_id: data.vendorId || null,
      vendor_name: data.vendorName || null,
      purchase_purpose: data.purchasePurpose || null,
      entertainment_expense: data.entertainmentExpense || null,
      recipient_name: data.recipientName || null,
      recipient_organization: data.recipientOrganization || null,
      usage_description: data.usageDescription || null,
      memo: data.memo || null,
    },
  };
}
