// ===== 계좌 상태 =====
export type AccountStatus = 'active' | 'inactive';

export const ACCOUNT_STATUS_LABELS: Record<AccountStatus, string> = {
  active: '사용',
  inactive: '정지',
};

export const ACCOUNT_STATUS_OPTIONS = [
  { value: 'active', label: '사용' },
  { value: 'inactive', label: '정지' },
];

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

// ===== 계좌 구분 =====
export type AccountCategory = 'bank_account' | 'loan_account' | 'securities_account' | 'insurance_account';

export const ACCOUNT_CATEGORY_LABELS: Record<AccountCategory, string> = {
  bank_account: '은행계좌',
  loan_account: '대출계좌',
  securities_account: '증권계좌',
  insurance_account: '보험계좌',
};

export const ACCOUNT_CATEGORY_OPTIONS = [
  { value: 'bank_account', label: '은행계좌' },
  { value: 'loan_account', label: '대출계좌' },
  { value: 'securities_account', label: '증권계좌' },
  { value: 'insurance_account', label: '보험계좌' },
];

export const ACCOUNT_CATEGORY_FILTER_OPTIONS = [
  { value: 'all', label: '전체' },
  ...ACCOUNT_CATEGORY_OPTIONS,
];

// ===== 계좌 유형 (구분별) =====
export type BankAccountType = 'savings' | 'fixed_deposit' | 'installment_savings' | 'foreign_currency' | 'other';
export type LoanAccountType = 'facility_fund' | 'operating_fund' | 'other';
export type SecuritiesAccountType = 'direct_investment' | 'fund' | 'trust' | 'other';
export type InsuranceAccountType = 'group_insurance' | 'fire_insurance' | 'ceo_insurance';

export const ACCOUNT_TYPE_OPTIONS_BY_CATEGORY: Record<AccountCategory, { value: string; label: string }[]> = {
  bank_account: [
    { value: 'savings', label: '보통예금' },
    { value: 'fixed_deposit', label: '정기예금' },
    { value: 'installment_savings', label: '적금' },
    { value: 'foreign_currency', label: '외화예금' },
    { value: 'other', label: '기타' },
  ],
  loan_account: [
    { value: 'facility_fund', label: '시설자금' },
    { value: 'operating_fund', label: '운전자금' },
    { value: 'other', label: '기타' },
  ],
  securities_account: [
    { value: 'direct_investment', label: '직접투자' },
    { value: 'fund', label: '펀드' },
    { value: 'trust', label: '신탁' },
    { value: 'other', label: '기타' },
  ],
  insurance_account: [
    { value: 'group_insurance', label: '단체보험' },
    { value: 'fire_insurance', label: '화재보험' },
    { value: 'ceo_insurance', label: 'CEO보험' },
  ],
};

export const ACCOUNT_TYPE_LABELS: Record<string, string> = Object.values(ACCOUNT_TYPE_OPTIONS_BY_CATEGORY)
  .flat()
  .reduce((acc, opt) => ({ ...acc, [opt.value]: opt.label }), {} as Record<string, string>);

// ===== 금융기관 목록 (은행 + 증권 + 보험) =====
export const BANK_OPTIONS = [
  { value: 'shinhan', label: '신한은행' },
  { value: 'kb', label: 'KB국민은행' },
  { 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 const SECURITIES_OPTIONS = [
  { value: 'mirae', label: '미래에셋증권' },
  { value: 'samsung', label: '삼성증권' },
  { value: 'kb_securities', label: 'KB증권' },
  { value: 'nh_securities', label: 'NH투자증권' },
  { value: 'hana_securities', label: '하나증권' },
  { value: 'shinhan_securities', label: '신한투자증권' },
  { value: 'korea_invest', label: '한국투자증권' },
  { value: 'kiwoom', label: '키움증권' },
  { value: 'daishin', label: '대신증권' },
];

export const INSURANCE_OPTIONS = [
  { value: 'samsung_life', label: '삼성생명' },
  { value: 'samsung_fire', label: '삼성화재' },
  { value: 'hanwha_life', label: '한화생명' },
  { value: 'kyobo', label: '교보생명' },
  { value: 'db_insurance', label: 'DB손해보험' },
  { value: 'hyundai_marine', label: '현대해상' },
  { value: 'kb_insurance', label: 'KB손해보험' },
  { value: 'meritz_fire', label: '메리츠화재' },
  { value: 'nh_life', label: 'NH농협생명' },
];

export const FINANCIAL_INSTITUTION_OPTIONS_BY_CATEGORY: Record<AccountCategory, { value: string; label: string }[]> = {
  bank_account: BANK_OPTIONS,
  loan_account: BANK_OPTIONS,
  securities_account: SECURITIES_OPTIONS,
  insurance_account: INSURANCE_OPTIONS,
};

export const ALL_FINANCIAL_INSTITUTION_OPTIONS = [
  ...BANK_OPTIONS,
  ...SECURITIES_OPTIONS,
  ...INSURANCE_OPTIONS,
];

export const BANK_LABELS: Record<string, string> = ALL_FINANCIAL_INSTITUTION_OPTIONS.reduce(
  (acc, opt) => ({ ...acc, [opt.value]: opt.label }),
  {}
);

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

export const SORT_OPTIONS: { value: SortOption; label: string }[] = [
  { value: 'latest', label: '최신순' },
  { value: 'oldest', label: '등록순' },
  { value: 'bankAsc', label: '은행명 오름차순' },
  { value: 'bankDesc', label: '은행명 내림차순' },
];

// ===== 계좌 인터페이스 =====
export interface Account {
  id: number;
  bankCode: string;
  bankName: string;
  accountNumber: string;
  accountName: string;
  accountHolder: string;
  status: AccountStatus;
  isPrimary: boolean;
  assignedUserId?: number;
  createdAt: string;
  updatedAt: string;

  // 신규 공통
  category: AccountCategory;
  accountType: string;
  isManual: boolean;

  // 은행계좌 정보
  contractAmount?: number;
  interestRate?: number;
  startDate?: string;
  maturityDate?: string;
  carryoverBalance?: number;

  // 대출계좌 정보
  loanAmount?: number;
  loanBalance?: number;
  interestPaymentCycle?: string;
  repaymentMethod?: string;
  gracePeriod?: string;
  monthlyRepayment?: number;
  collateral?: string;

  // 증권계좌 정보
  investmentAmount?: number;
  returnRate?: number;
  evaluationAmount?: number;

  // 보험계좌 공통
  surrenderValue?: number;
  policyNumber?: string;
  paymentCycle?: string;
  premiumPerCycle?: number;
  // 단체보험
  premiumPerPerson?: number;
  enrolledCount?: number;
  // 화재보험
  insuredProperty?: string;
  propertyAddress?: string;
  // CEO보험
  beneficiary?: string;

  note?: string;
}

// ===== 계좌 폼 데이터 =====
export interface AccountFormData {
  // 공통
  category: AccountCategory;
  accountType: string;
  bankCode: string;
  bankName: string;
  accountNumber: string;
  accountName: string;
  accountHolder: string;
  status: AccountStatus;

  // 은행계좌
  contractAmount?: number;
  interestRate?: number;
  startDate?: string;
  maturityDate?: string;
  carryoverBalance?: number;

  // 대출계좌
  loanAmount?: number;
  loanBalance?: number;
  interestPaymentCycle?: string;
  repaymentMethod?: string;
  gracePeriod?: string;
  monthlyRepayment?: number;
  collateral?: string;

  // 증권계좌
  investmentAmount?: number;
  returnRate?: number;
  evaluationAmount?: number;

  // 보험계좌 공통
  surrenderValue?: number;
  policyNumber?: string;
  paymentCycle?: string;
  premiumPerCycle?: number;
  // 단체보험
  premiumPerPerson?: number;
  enrolledCount?: number;
  // 화재보험
  insuredProperty?: string;
  propertyAddress?: string;
  // CEO보험
  beneficiary?: string;

  note?: string;
}

// ===== 통계 요약 =====
export interface AccountSummary {
  total: number;
  bankAccount: number;
  loanAccount: number;
  securitiesAccount: number;
  insuranceAccount: number;
}

// ===== 예금주/계약자/피보험자 라벨 =====
export const HOLDER_LABEL_BY_CATEGORY: Record<AccountCategory, string> = {
  bank_account: '예금주',
  loan_account: '차입자',
  securities_account: '명의자',
  insurance_account: '피보험자',
};
