// ===== 악성채권 추심관리 타입 정의 =====

// 추심 상태
export type CollectionStatus = 'collecting' | 'legalAction' | 'collectionEnd';

// 추심종료 사유
export type CollectionEndReason = 'recovered' | 'badDebt';

export const COLLECTION_END_REASON_OPTIONS: { value: CollectionEndReason; label: string }[] = [
  { value: 'recovered', label: '회수완료' },
  { value: 'badDebt', label: '대손처리' },
];

// 정렬 옵션
export type SortOption = 'latest' | 'oldest';

// 담당자 정보
export interface Manager {
  id: string;
  departmentName: string;
  name: string;
  position: string;
  phone: string;
}

// 메모
export interface BadDebtMemo {
  id: string;
  content: string;
  createdAt: string;
  createdBy: string;
}

// 첨부 파일
export interface AttachedFile {
  id: string;
  name: string;
  url: string;
  type: 'businessRegistration' | 'taxInvoice' | 'additional';
}

// 개별 악성채권 항목 (거래처별 하위 목록)
export interface BadDebtItem {
  id: string;
  debtAmount: number;
  status: CollectionStatus;
  overdueDays: number;
  isActive: boolean;
  occurredAt: string | null;
  assignedManager: { id: string; name: string } | null;
}

// 악성채권 레코드 (거래처 기준)
export interface BadDebtRecord {
  id: string;
  // 거래처 기본 정보
  vendorId: string;
  vendorCode: string;
  vendorName: string;
  businessNumber: string;
  representativeName: string;
  vendorType: 'sales' | 'purchase' | 'both';
  businessType: string;
  businessCategory: string;
  // 연락처 정보
  zipCode: string;
  address1: string;
  address2: string;
  phone: string;
  mobile: string;
  fax: string;
  email: string;
  // 담당자 정보
  contactName: string;
  contactPhone: string;
  systemManager: string;
  // 악성채권 집계 정보 (거래처 기준)
  debtAmount: number;           // 총 미수금액
  badDebtCount: number;         // 악성채권 건수
  status: CollectionStatus;     // 대표 상태 (가장 최근)
  collectionEndReason?: CollectionEndReason; // 추심종료 사유 (status === 'collectionEnd'일 때)
  overdueDays: number;          // 최대 연체일수
  overdueToggle: boolean;
  occurrenceDate: string;
  endDate: string | null;
  assignedManagerId: string | null;
  assignedManager: Manager | null;
  settingToggle: boolean;
  // 개별 악성채권 목록
  badDebts: BadDebtItem[];
  // 첨부 파일
  files: AttachedFile[];
  // 메모
  memos: BadDebtMemo[];
  // 타임스탬프
  createdAt: string;
  updatedAt: string;
}

// ===== 상태 설정 (status-config 기반) =====
import { BAD_DEBT_COLLECTION_STATUS_CONFIG } from '@/lib/utils/status-config';

export const COLLECTION_STATUS_LABELS = BAD_DEBT_COLLECTION_STATUS_CONFIG.STATUS_LABELS;
export const STATUS_FILTER_OPTIONS = BAD_DEBT_COLLECTION_STATUS_CONFIG.STATUS_OPTIONS;
export const STATUS_SELECT_OPTIONS = BAD_DEBT_COLLECTION_STATUS_CONFIG.STATUS_OPTIONS.filter(
  (o): o is { value: CollectionStatus; label: string } => o.value !== 'all'
);
export const STATUS_BADGE_STYLES = BAD_DEBT_COLLECTION_STATUS_CONFIG.STATUS_STYLES;

// ===== 정렬 옵션 =====
export const SORT_OPTIONS = [
  { value: 'latest', label: '최신순' },
  { value: 'oldest', label: '등록순' },
] as const;

// ===== 거래처 유형 라벨 =====
export const VENDOR_TYPE_LABELS: Record<string, string> = {
  sales: '매출',
  purchase: '매입',
  both: '매출매입',
};

// ===== 요약 통계 =====
export interface BadDebtSummary {
  totalCount: number;
  totalAmount: number;
  collectedAmount: number;
  pendingAmount: number;
  collectionRate: number;
}