// ===== API 응답 타입 =====
export interface ApiResponse<T> {
  success: boolean;
  data?: T;
  message?: string;
}

export interface PaginatedResponse<T> {
  data: T[];
  total: number;
  page?: number;
  size?: number;
}

// ===== API 데이터 타입 (백엔드에서 오는 원본 데이터) =====
export interface ClientApiData {
  id: number;
  client_code: string;
  name: string;
  client_type: 'SALES' | 'PURCHASE' | 'BOTH';
  business_no: string;
  contact_person: string;
  phone: string;
  mobile: string;
  fax: string;
  email: string;
  address: string;
  manager_name: string;
  manager_tel: string;
  system_manager: string;
  purchase_payment_day: string;
  sales_payment_day: string;
  business_type: string;
  business_item: string;
  bad_debt: boolean;
  memo: string;
  is_active: boolean;
  account_id: string;
  outstanding_amount: number;
  bad_debt_total: number;
  has_bad_debt: boolean;
  is_overdue?: boolean;
  created_at: string;
  updated_at: string;
}

// ===== 거래처 구분 =====
export type VendorCategory = 'sales' | 'purchase' | 'both';

// ===== API client_type ↔ VendorCategory 변환 =====
export const CLIENT_TYPE_TO_CATEGORY: Record<string, VendorCategory> = {
  SALES: 'sales',
  PURCHASE: 'purchase',
  BOTH: 'both',
};

export const CATEGORY_TO_CLIENT_TYPE: Record<VendorCategory, string> = {
  sales: 'SALES',
  purchase: 'PURCHASE',
  both: 'BOTH',
};

export const VENDOR_CATEGORY_LABELS: Record<VendorCategory, string> = {
  sales: '매출',
  purchase: '매입',
  both: '매입매출',
};

export const VENDOR_CATEGORY_OPTIONS = [
  { value: 'all', label: '전체' },
  { value: 'sales', label: '매출' },
  { value: 'purchase', label: '매입' },
  { value: 'both', label: '매입매출' },
];

export const VENDOR_CATEGORY_SELECTOR_OPTIONS = [
  { value: 'sales', label: '매출' },
  { value: 'purchase', label: '매입' },
  { value: 'both', label: '매입매출' },
];

// ===== 신용등급 =====
export type CreditRating = 'AAA' | 'AA' | 'A' | 'BBB' | 'BB' | 'B' | 'CCC' | 'CC' | 'C' | 'D';

export const CREDIT_RATING_OPTIONS = [
  { value: 'all', label: '전체' },
  { value: 'AAA', label: 'AAA' },
  { value: 'AA', label: 'AA' },
  { value: 'A', label: 'A' },
  { value: 'BBB', label: 'BBB' },
  { value: 'BB', label: 'BB' },
  { value: 'B', label: 'B' },
  { value: 'CCC', label: 'CCC' },
  { value: 'CC', label: 'CC' },
  { value: 'C', label: 'C' },
  { value: 'D', label: 'D' },
];

export const CREDIT_RATING_SELECTOR_OPTIONS = CREDIT_RATING_OPTIONS.filter(o => o.value !== 'all');

export const CREDIT_RATING_COLORS: Record<CreditRating, string> = {
  AAA: 'bg-green-100 text-green-800',
  AA: 'bg-green-100 text-green-800',
  A: 'bg-green-100 text-green-800',
  BBB: 'bg-yellow-100 text-yellow-800',
  BB: 'bg-yellow-100 text-yellow-800',
  B: 'bg-yellow-100 text-yellow-800',
  CCC: 'bg-red-100 text-red-800',
  CC: 'bg-red-100 text-red-800',
  C: 'bg-red-100 text-red-800',
  D: 'bg-red-100 text-red-800',
};

// ===== 거래등급 =====
export type TransactionGrade = 'A' | 'B' | 'C' | 'D' | 'E';

export const TRANSACTION_GRADE_LABELS: Record<TransactionGrade, string> = {
  A: 'A(우수)',
  B: 'B(양호)',
  C: 'C(보통)',
  D: 'D(주의)',
  E: 'E(위험)',
};

export const TRANSACTION_GRADE_OPTIONS = [
  { value: 'all', label: '전체' },
  { value: 'A', label: 'A(우수)' },
  { value: 'B', label: 'B(양호)' },
  { value: 'C', label: 'C(보통)' },
  { value: 'D', label: 'D(주의)' },
  { value: 'E', label: 'E(위험)' },
];

export const TRANSACTION_GRADE_SELECTOR_OPTIONS = TRANSACTION_GRADE_OPTIONS.filter(o => o.value !== 'all');

export const TRANSACTION_GRADE_COLORS: Record<TransactionGrade, string> = {
  A: 'bg-green-100 text-green-800',
  B: 'bg-blue-100 text-blue-800',
  C: 'bg-yellow-100 text-yellow-800',
  D: 'bg-orange-100 text-orange-800',
  E: 'bg-red-100 text-red-800',
};

// ===== 악성채권 상태 =====
export type BadDebtStatus = 'none' | 'badDebt' | 'normal';

export const BAD_DEBT_STATUS_LABELS: Record<BadDebtStatus, string> = {
  none: '-',
  badDebt: '악성채권',
  normal: '정상',
};

export const BAD_DEBT_STATUS_OPTIONS = [
  { value: 'all', label: '전체' },
  { value: 'badDebt', label: '악성채권' },
  { value: 'normal', label: '정상' },
];

export const BAD_DEBT_STATUS_SELECTOR_OPTIONS = BAD_DEBT_STATUS_OPTIONS.filter(o => o.value !== 'all');

export const BAD_DEBT_STATUS_COLORS: Record<BadDebtStatus, string> = {
  none: 'bg-gray-100 text-gray-500', badDebt: 'bg-red-100 text-red-800',
  normal: 'bg-green-100 text-green-800',
};

// ===== 사용 상태 =====
export type VendorStatus = 'active' | 'inactive';

export const VENDOR_STATUS_LABELS: Record<VendorStatus, string> = {
  active: '사용',
  inactive: '미사용',
};

export const VENDOR_STATUS_OPTIONS = [
  { value: 'all', label: '전체' },
  { value: 'active', label: '사용' },
  { value: 'inactive', label: '미사용' },
];

export const VENDOR_STATUS_SELECTOR_OPTIONS = VENDOR_STATUS_OPTIONS.filter(o => o.value !== 'all');

export const VENDOR_STATUS_COLORS: Record<VendorStatus, string> = {
  active: 'bg-green-100 text-green-800',
  inactive: 'bg-gray-100 text-gray-500',
};

// ===== 정렬 옵션 =====
export type SortOption = 'latest' | 'oldest' | 'nameAsc' | 'nameDesc' | 'outstandingHigh' | 'outstandingLow';

export const SORT_OPTIONS: { value: SortOption; label: string }[] = [
  { value: 'latest', label: '최신순' },
  { value: 'oldest', label: '등록순' },
  { value: 'nameAsc', label: '거래처명 오름차순' },
  { value: 'nameDesc', label: '거래처명 내림차순' },
  { value: 'outstandingHigh', label: '미수금 높은순' },
  { value: 'outstandingLow', label: '미수금 낮은순' },
];

// ===== 결제일 옵션 (1~31일) =====
export const PAYMENT_DAY_OPTIONS = Array.from({ length: 31 }, (_, i) => ({
  value: String(i + 1),
  label: `${i + 1}일`,
}));

// ===== 은행 옵션 =====
export const BANK_OPTIONS = [
  { value: 'none', label: '선택' },
  { value: 'kb', label: 'KB국민은행' },
  { value: 'shinhan', label: '신한은행' },
  { value: 'woori', label: '우리은행' },
  { value: 'hana', label: '하나은행' },
  { value: 'nh', label: 'NH농협은행' },
  { value: 'ibk', label: 'IBK기업은행' },
  { value: 'kakao', label: '카카오뱅크' },
  { value: 'toss', label: '토스뱅크' },
  { value: 'sc', label: 'SC제일은행' },
  { value: 'citi', label: '씨티은행' },
  { value: 'kdb', label: 'KDB산업은행' },
  { value: 'busan', label: '부산은행' },
  { value: 'daegu', label: '대구은행' },
  { value: 'gwangju', label: '광주은행' },
  { value: 'jeonbuk', label: '전북은행' },
  { value: 'jeju', label: '제주은행' },
  { value: 'kbank', label: '케이뱅크' },
  { value: 'suhyup', label: '수협은행' },
  { value: 'post', label: '우체국' },
  { value: 'saemaul', label: '새마을금고' },
  { value: 'shinhyup', label: '신협' },
];

// ===== 메모 인터페이스 =====
export interface VendorMemo {
  id: string;
  content: string;
  createdAt: string;
}

// ===== 거래처 인터페이스 =====
export interface Vendor {
  id: string;
  vendorCode: string;           // 거래처코드
  businessNumber: string;       // 사업자등록번호
  vendorName: string;           // 거래처명
  representativeName: string;   // 대표자명
  category: VendorCategory;     // 구분 (매출/매입/매입매출)
  businessType: string;         // 업태
  businessCategory: string;     // 업종

  // 연락처 정보
  zipCode: string;              // 우편번호
  address1: string;             // 기본주소
  address2: string;             // 상세주소
  phone: string;                // 전화번호
  mobile: string;               // 모바일
  fax: string;                  // 팩스
  email: string;                // 이메일

  // 담당자 정보
  managerName: string;          // 담당자명
  managerPhone: string;         // 담당자 전화
  systemManager: string;        // 시스템 관리자

  // 회사 정보
  logoUrl: string;              // 회사 로고 URL
  purchasePaymentDay: number;   // 매입 결제일 (1~31)
  salesPaymentDay: number;      // 매출 결제일 (1~31)

  // 신용/거래 정보
  creditRating: CreditRating;   // 신용등급
  transactionGrade: TransactionGrade; // 거래등급
  taxInvoiceEmail: string;      // 세금계산서 이메일
  bankName: string;             // 입금계좌 은행
  accountNumber: string;        // 계좌번호
  accountHolder: string;        // 예금주

  // 사용 상태
  isActive: boolean;            // 사용/미사용

  // 추가 정보
  outstandingAmount: number;    // 미수금
  overdueAmount: number;        // 연체금액
  overdueDays: number;          // 연체일수
  unpaidAmount: number;         // 미지급
  badDebtAmount: number;        // 악성채권 금액
  badDebtStatus: BadDebtStatus; // 악성채권 상태
  overdueToggle: boolean;       // 연체 토글
  badDebtToggle: boolean;       // 악성채권 토글

  // 메모
  memos: VendorMemo[];

  // 시스템 필드
  createdAt: string;
  updatedAt: string;
}

// ===== 구분별 Badge 색상 =====
export const VENDOR_CATEGORY_COLORS: Record<VendorCategory, string> = {
  sales: 'bg-green-100 text-green-800',
  purchase: 'bg-orange-100 text-orange-800',
  both: 'bg-blue-100 text-blue-800',
};