import type { AccountSubjectCategory } from '@/components/accounting/common/types';

// ===== 계정별원장 API 응답 =====
export interface AccountLedgerResponse {
  account: {
    code: string;
    name: string;
    category: AccountSubjectCategory;
  } | null;
  period: {
    start_date: string;
    end_date: string;
  };
  carry_forward: {
    debit: number;
    credit: number;
    balance: number;
  };
  monthly_data: MonthlyData[];
  grand_total: {
    debit: number;
    credit: number;
    balance: number;
  };
}

export interface MonthlyData {
  month: string; // "2026-03"
  items: LedgerItem[];
  subtotal: { debit: number; credit: number };
  cumulative: { debit: number; credit: number };
}

export interface LedgerItem {
  date: string;
  description: string | null;
  trading_partner_name: string | null;
  biz_no: string | null;
  debit_amount: number;
  credit_amount: number;
  balance: number;
  source_type: string;
  source_id: number;
  card_tx: CardTransaction | null;
}

export interface CardTransaction {
  card_num: string;
  card_company_name: string;
  merchant_name: string;
  merchant_biz_num: string;
  deduction_type: string; // 'deductible' | 'non_deductible'
  supply_amount: number;
  tax_amount: number;
  approval_amount: number;
}

// ===== 전표 상세 (드릴다운 모달) =====
export interface JournalEntryDetail {
  id: number;
  entry_no: string;
  entry_date: string;
  entry_type: string;
  description: string | null;
  total_debit: number;
  total_credit: number;
  status: 'draft' | 'confirmed';
  source_type: string | null;
  created_by_name: string | null;
  lines: JournalEntryLine[];
}

export interface JournalEntryLine {
  line_no: number;
  dc_type: 'debit' | 'credit';
  account_code: string;
  account_name: string;
  trading_partner_name: string | null;
  debit_amount: number;
  credit_amount: number;
  description: string | null;
}

// ===== 숫자 포맷 유틸 =====
export function formatLedgerAmount(n: number | null | undefined): string {
  if (n === 0 || n === null || n === undefined) return '';
  if (n < 0) return '(' + Math.abs(n).toLocaleString() + ')';
  return n.toLocaleString();
}

export function maskCardNumber(cardNum: string): string {
  return '\u00B7\u00B7\u00B7\u00B7' + cardNum.slice(-4);
}
