/**
 * 미수금 (Receivable) + 채권추심 (DebtCollection) 변환
 */

import type { ReceivablesApiResponse, BadDebtApiResponse } from '../types';
import type {
  ReceivableData,
  DebtCollectionData,
  CheckPoint,
  CheckPointType,
} from '@/components/business/CEODashboard/types';
import { formatAmount, normalizePath, validateHighlightColor } from './common';

// ============================================
// 미수금 (Receivable) — D1.7 cards + check_points 구조
// ============================================

/**
 * Receivables API 응답 → Frontend 타입 변환
 * 백엔드에서 cards[] + check_points[] 구조로 직접 전달
 */
export function transformReceivableResponse(api: ReceivablesApiResponse): ReceivableData {
  return {
    cards: api.cards.map((card) => ({
      id: card.id,
      label: card.label,
      amount: card.amount,
      subLabel: card.subLabel,
      unit: card.unit,
      // sub_items → subItems 매핑
      subItems: card.sub_items?.map((item) => ({
        label: item.label,
        value: item.value,
      })),
      // top_items → subItems 매핑 (Top 3 거래처)
      ...(card.top_items && card.top_items.length > 0
        ? {
            subItems: card.top_items.map((item, idx) => ({
              label: `${idx + 1}. ${item.name}`,
              value: item.amount,
            })),
          }
        : {}),
    })),
    checkPoints: api.check_points.map((cp) => ({
      id: cp.id,
      type: cp.type as CheckPointType,
      message: cp.message,
      highlights: cp.highlights?.map((h) => ({
        text: h.text,
        color: validateHighlightColor(h.color),
      })),
    })),
    detailButtonPath: normalizePath('/accounting/receivables-status'),
  };
}

// ============================================
// 채권추심 (DebtCollection)
// ============================================

/**
 * 채권추심 CheckPoints 생성
 */
function generateDebtCollectionCheckPoints(api: BadDebtApiResponse): CheckPoint[] {
  const checkPoints: CheckPoint[] = [];

  // 법적조치 진행 중
  if (api.legal_action_amount > 0) {
    checkPoints.push({
      id: 'dc-legal',
      type: 'warning' as CheckPointType,
      message: `법적조치 진행 중 ${formatAmount(api.legal_action_amount)}입니다.`,
      highlights: [
        { text: formatAmount(api.legal_action_amount), color: 'red' as const },
      ],
    });
  }

  // 회수 완료
  if (api.recovered_amount > 0) {
    checkPoints.push({
      id: 'dc-recovered',
      type: 'success' as CheckPointType,
      message: `총 ${formatAmount(api.recovered_amount)}을 회수 완료했습니다.`,
      highlights: [
        { text: formatAmount(api.recovered_amount), color: 'green' as const },
        { text: '회수 완료', color: 'green' as const },
      ],
    });
  }

  return checkPoints;
}

// TODO: 백엔드 per-card sub_label/count 제공 시 더미값 제거
// 채권추심 카드별 더미 서브라벨 (회사명 + 건수)
const DEBT_COLLECTION_FALLBACK_SUB_LABELS: Record<string, { company: string; count: number }> = {
  dc1: { company: '(주)부산화학 외', count: 5 },
  dc2: { company: '(주)삼성테크 외', count: 3 },
  dc3: { company: '(주)대한전자 외', count: 2 },
  dc4: { company: '(주)한국정밀 외', count: 3 },
};

/**
 * 채권추심 subLabel 생성 헬퍼
 * dc1(누적)은 API client_count 사용, 나머지는 더미값
 */
function buildDebtSubLabel(cardId: string, clientCount?: number): string | undefined {
  const fallback = DEBT_COLLECTION_FALLBACK_SUB_LABELS[cardId];
  if (!fallback) return undefined;

  const count = cardId === 'dc1' && clientCount !== undefined ? clientCount : fallback.count;
  if (count <= 0) return undefined;

  const remaining = count - 1;
  if (remaining > 0) {
    return `${fallback.company} ${remaining}건`;
  }
  return fallback.company.replace(/ 외$/, '');
}

/**
 * BadDebt API 응답 → Frontend 타입 변환
 */
export function transformDebtCollectionResponse(api: BadDebtApiResponse): DebtCollectionData {
  return {
    cards: [
      {
        id: 'dc1',
        label: '누적 악성채권',
        amount: api.total_amount,
        subLabel: buildDebtSubLabel('dc1', api.client_count),
      },
      {
        id: 'dc2',
        label: '추심중',
        amount: api.collecting_amount,
        subLabel: buildDebtSubLabel('dc2'),
      },
      {
        id: 'dc3',
        label: '법적조치',
        amount: api.legal_action_amount,
        subLabel: buildDebtSubLabel('dc3'),
      },
      {
        id: 'dc4',
        label: '회수완료',
        amount: api.recovered_amount,
        subLabel: buildDebtSubLabel('dc4'),
      },
    ],
    checkPoints: generateDebtCollectionCheckPoints(api),
    detailButtonPath: normalizePath('/accounting/bad-debt-collection'),
  };
}
