/**
 * 급여관리 타입 정의 (payrolls API 기준)
 */

// ===== 상태 =====
export type PayrollStatus = 'draft' | 'confirmed' | 'paid';

// ===== 동적 항목 (수당/공제) =====
export interface NameAmountItem {
  name: string;
  amount: number;
}

// ===== 급여 목록 레코드 =====
export interface PayrollRecord {
  id: number;
  userId: number;
  userName: string;
  department: string;
  baseSalary: number;
  overtimePay: number;
  bonus: number;
  allowancesTotal: number;
  grossSalary: number;
  totalDeductions: number;
  netSalary: number;
  status: PayrollStatus;
  payYear: number;
  payMonth: number;
  note: string | null;
  createdAt: string;
}

// ===== 급여 상세 =====
export interface PayrollDetail {
  id: number;
  userId: number;
  userName: string;
  userEmail: string;
  department: string;
  position: string;
  rank: string;
  // 지급 항목
  baseSalary: number;
  overtimePay: number;
  bonus: number;
  allowances: NameAmountItem[];
  grossSalary: number;
  // 법정 공제
  pension: number;
  healthInsurance: number;
  longTermCare: number;
  employmentInsurance: number;
  incomeTax: number;
  residentTax: number;
  // 추가 공제
  deductions: NameAmountItem[];
  totalDeductions: number;
  netSalary: number;
  // 상태/메타
  status: PayrollStatus;
  payYear: number;
  payMonth: number;
  note: string | null;
  confirmedAt: string | null;
  paidAt: string | null;
}

// ===== 월간 요약 =====
export interface PayrollSummary {
  year: number;
  month: number;
  totalCount: number;
  draftCount: number;
  confirmedCount: number;
  paidCount: number;
  totalGross: number;
  totalDeductions: number;
  totalNet: number;
}

// ===== 급여 설정 =====
export interface PayrollSettings {
  id?: number;
  healthInsuranceRate: number;
  longTermCareRate: number;
  pensionRate: number;
  employmentInsuranceRate: number;
  incomeTaxRate: number;
  residentTaxRate: number;
  pensionMaxSalary: number;
  pensionMinSalary: number;
  payDay: number;
  autoCalculate: boolean;
  allowanceTypes: { code: string; name: string; is_taxable?: boolean }[];
  deductionTypes: { code: string; name: string }[];
}

// ===== 급여명세서 =====
export interface Payslip {
  payroll: PayrollDetail;
  period: string;
  employee: { id: number; name: string; email: string };
  earnings: {
    baseSalary: number;
    overtimePay: number;
    bonus: number;
    allowances: NameAmountItem[];
    allowancesTotal: number;
    grossTotal: number;
  };
  deductions: {
    incomeTax: number;
    residentTax: number;
    healthInsurance: number;
    pension: number;
    employmentInsurance: number;
    otherDeductions: NameAmountItem[];
    otherTotal: number;
    total: number;
  };
  netSalary: number;
  status: PayrollStatus;
  statusLabel: string;
  paidAt: string | null;
}

// ===== 상수 =====
export const PAYROLL_STATUS_LABELS: Record<PayrollStatus, string> = {
  draft: '작성중',
  confirmed: '확정',
  paid: '지급완료',
};

export const PAYROLL_STATUS_COLORS: Record<PayrollStatus, string> = {
  draft: 'bg-gray-100 text-gray-800',
  confirmed: 'bg-blue-100 text-blue-800',
  paid: 'bg-green-100 text-green-800',
};

// ===== 유틸리티 =====
export const formatCurrency = (amount: number): string => {
  return new Intl.NumberFormat('ko-KR').format(Math.round(amount));
};
