import { Card, CardContent, CardHeader, CardTitle } from "../ui/card";
import { Clock } from "lucide-react";
import { ResponsiveContainer, LineChart, Line, XAxis, YAxis, CartesianGrid, Tooltip } from "recharts";

export function ReceivablesTrendWidget() {
  // Mock 데이터 (실제로는 API에서 가져옴)
  const monthlyTrend = [
    { month: '2024-01', receivables: 380 },
    { month: '2024-02', receivables: 420 },
    { month: '2024-03', receivables: 450 },
    { month: '2024-04', receivables: 410 },
    { month: '2024-05', receivables: 390 },
    { month: '2024-06', receivables: 460 },
    { month: '2024-07', receivables: 440 },
    { month: '2024-08', receivables: 400 },
    { month: '2024-09', receivables: 430 },
    { month: '2024-10', receivables: 420 },
    { month: '2024-11', receivables: 410 },
    { month: '2024-12', receivables: 395 }
  ];

  return (
    <Card className="border border-border/20 bg-card rounded-xl">
      <CardHeader>
        <CardTitle className="flex items-center justify-between">
          <div className="flex items-center space-x-3">
            <div className="w-6 h-6 bg-orange-100 dark:bg-orange-900/30 rounded-lg flex items-center justify-center">
              <Clock className="h-4 w-4 text-orange-600 dark:text-orange-400" />
            </div>
            <span>월별 미수금 추이</span>
          </div>
        </CardTitle>
      </CardHeader>
      <CardContent>
        <ResponsiveContainer width="100%" height={250} minHeight={250}>
          <LineChart data={monthlyTrend}>
            <CartesianGrid strokeDasharray="3 3" stroke="#f1f5f9" />
            <XAxis 
              dataKey="month" 
              stroke="#64748b"
              tickFormatter={(value) => value.split('-')[1] + '월'}
              tick={{ fontSize: 12 }}
              interval={0}
              angle={-45}
              textAnchor="end"
              height={60}
            />
            <YAxis stroke="#64748b" tick={{ fontSize: 12 }} />
            <Tooltip 
              contentStyle={{ 
                backgroundColor: 'white', 
                border: '1px solid #e2e8f0', 
                borderRadius: '8px',
                boxShadow: '0 4px 6px -1px rgba(0, 0, 0, 0.1)'
              }}
              formatter={(value: any) => [`${value}M원`, '']}
              labelFormatter={(label) => `${label.split('-')[1]}월`}
            />
            <Line 
              type="monotone" 
              dataKey="receivables" 
              stroke="#f97316" 
              strokeWidth={3}
              name="미수금"
              dot={{ fill: '#f97316', r: 4 }}
            />
          </LineChart>
        </ResponsiveContainer>
        <div className="mt-4 grid grid-cols-3 gap-3">
          <div className="text-center p-2 bg-orange-50 dark:bg-orange-950/20 rounded-lg">
            <p className="text-xs text-muted-foreground">평균 미수금</p>
            <p className="font-bold text-orange-600">
              {(monthlyTrend.reduce((sum: number, m: any) => sum + m.receivables, 0) / monthlyTrend.length).toFixed(0)}M원
            </p>
          </div>
          <div className="text-center p-2 bg-red-50 dark:bg-red-950/20 rounded-lg">
            <p className="text-xs text-muted-foreground">최고 미수금</p>
            <p className="font-bold text-red-600">
              {Math.max(...monthlyTrend.map((m: any) => m.receivables))}M원
            </p>
          </div>
          <div className="text-center p-2 bg-green-50 dark:bg-green-950/20 rounded-lg">
            <p className="text-xs text-muted-foreground">최저 미수금</p>
            <p className="font-bold text-green-600">
              {Math.min(...monthlyTrend.map((m: any) => m.receivables))}M원
            </p>
          </div>
        </div>
      </CardContent>
    </Card>
  );
}