'use client' import { PieChart, Pie, Cell, Tooltip, ResponsiveContainer } from 'recharts' import { FixedDebt, VariableDebt } from '@/lib/types' import { formatCurrency } from '@/lib/utils' interface ExpenseChartProps { fixedDebts: FixedDebt[] variableDebts: VariableDebt[] } // Colores por categoría const CATEGORY_COLORS: Record = { // Deudas fijas housing: '#10b981', // emerald-500 services: '#3b82f6', // blue-500 subscription: '#8b5cf6', // violet-500 other: '#64748b', // slate-500 // Deudas variables shopping: '#f59e0b', // amber-500 food: '#ef4444', // red-500 entertainment: '#ec4899', // pink-500 health: '#06b6d4', // cyan-500 transport: '#84cc16', // lime-500 } // Nombres de categorías en español const CATEGORY_NAMES: Record = { housing: 'Vivienda', services: 'Servicios', subscription: 'Suscripciones', other: 'Otros', shopping: 'Compras', food: 'Comida', entertainment: 'Entretenimiento', health: 'Salud', transport: 'Transporte', } interface ChartData { name: string value: number color: string category: string } export function ExpenseChart({ fixedDebts, variableDebts }: ExpenseChartProps) { // Agrupar gastos por categoría const categoryTotals = new Map() // Agregar deudas fijas no pagadas fixedDebts .filter((debt) => !debt.isPaid) .forEach((debt) => { const current = categoryTotals.get(debt.category) || 0 categoryTotals.set(debt.category, current + debt.amount) }) // Agregar deudas variables no pagadas variableDebts .filter((debt) => !debt.isPaid) .forEach((debt) => { const current = categoryTotals.get(debt.category) || 0 categoryTotals.set(debt.category, current + debt.amount) }) // Convertir a formato de datos para el gráfico const data: ChartData[] = Array.from(categoryTotals.entries()) .map(([category, value]) => ({ name: CATEGORY_NAMES[category] || category, value, color: CATEGORY_COLORS[category] || '#64748b', category, })) .filter((item) => item.value > 0) .sort((a, b) => b.value - a.value) // Calcular total const total = data.reduce((sum, item) => sum + item.value, 0) if (data.length === 0) { return (

No hay gastos pendientes

) } return (

Distribución de Gastos

{/* Gráfico de dona */}
{data.map((entry, index) => ( ))} typeof value === 'number' ? formatCurrency(value) : value } contentStyle={{ backgroundColor: '#1e293b', border: '1px solid #334155', borderRadius: '8px', color: '#fff', }} />
{/* Leyenda */}
{data.map((item) => { const percentage = total > 0 ? (item.value / total) * 100 : 0 return (
{item.name} {percentage.toFixed(1)}%

{formatCurrency(item.value)}

) })} {/* Total */}
Total {formatCurrency(total)}
) }