import type { PaymentApiData, PaymentHistory, PaymentStatus } from './types';
import { PAYMENT_METHOD_LABELS } from './types';

// ===== API → Frontend 변환 =====
export function transformApiToFrontend(apiData: PaymentApiData): PaymentHistory {
  const subscription = apiData.subscription;
  const plan = subscription?.plan;

  // 결제 수단 라벨 생성
  const paymentMethodLabel = PAYMENT_METHOD_LABELS[apiData.payment_method] || apiData.payment_method;

  // 구독 기간
  const periodStart = subscription?.started_at?.split('T')[0] || '';
  const periodEnd = subscription?.ended_at?.split('T')[0] || '';

  return {
    id: String(apiData.id),
    paymentDate: apiData.paid_at?.split('T')[0] || apiData.created_at.split('T')[0],
    subscriptionName: plan?.name || '구독',
    paymentMethod: paymentMethodLabel,
    subscriptionPeriod: {
      start: periodStart,
      end: periodEnd,
    },
    amount: typeof apiData.amount === 'string' ? parseFloat(apiData.amount) : apiData.amount,
    status: apiData.status as PaymentStatus,
    canViewInvoice: apiData.status === 'completed',
    createdAt: apiData.created_at,
    updatedAt: apiData.updated_at,
  };
}