'use client';

import { useRouter } from 'next/navigation';
import { Banknote, Clock, AlertTriangle, CircleDollarSign } from 'lucide-react';
import { Card, CardContent } from '@/components/ui/card';
import { SectionTitle, AmountCardItem, CheckPointItem, type SectionColorTheme } from '../components';
import type { ReceivableData } from '../types';

// 카드별 아이콘 매핑 (미수금 합계, 30일 이내, 30~90일, 90일 초과)
const CARD_ICONS = [CircleDollarSign, Banknote, Clock, AlertTriangle];
const CARD_THEMES: SectionColorTheme[] = ['amber', 'green', 'orange', 'red'];

interface ReceivableSectionProps {
  data: ReceivableData;
}

export function ReceivableSection({ data }: ReceivableSectionProps) {
  const router = useRouter();

  const handleDetailClick = () => {
    if (data.detailButtonPath) {
      router.push(data.detailButtonPath);
    }
  };

  return (
    <Card>
      <CardContent className="p-6">
        <SectionTitle
          title="미수금 현황"
          badge="warning"
          icon={Banknote}
          colorTheme="amber"
          actionButton={
            data.detailButtonLabel
              ? {
                  label: data.detailButtonLabel,
                  onClick: handleDetailClick,
                }
              : undefined
          }
        />

        <div className="grid grid-cols-1 xs:grid-cols-2 md:grid-cols-4 gap-3 xs:gap-4 mb-4">
          {data.cards.map((card, idx) => (
            <AmountCardItem
              key={card.id}
              card={card}
              onClick={handleDetailClick}
              icon={CARD_ICONS[idx] || Banknote}
              colorTheme={CARD_THEMES[idx] || 'amber'}
              showTrend={!!card.previousLabel}
              trendValue={card.previousLabel}
              trendDirection={idx === 3 ? 'down' : 'up'}
            />
          ))}
        </div>

        {data.checkPoints.length > 0 && (
          <div className="border-t pt-4 space-y-1">
            {data.checkPoints.map((cp) => (
              <CheckPointItem key={cp.id} checkpoint={cp} />
            ))}
          </div>
        )}
      </CardContent>
    </Card>
  );
}