import React, { useState, useEffect } from 'react'; import { useTranslation } from 'react-i18next'; import { financialHealthService } from '../services/api'; import useFormatters from '../hooks/useFormatters'; import { Chart as ChartJS, CategoryScale, LinearScale, PointElement, LineElement, Title, Tooltip, Legend, Filler, } from 'chart.js'; import { Line } from 'react-chartjs-2'; ChartJS.register( CategoryScale, LinearScale, PointElement, LineElement, Title, Tooltip, Legend, Filler ); const FinancialHealth = () => { const { t } = useTranslation(); const { currency } = useFormatters(); const [loading, setLoading] = useState(true); const [data, setData] = useState(null); const [history, setHistory] = useState([]); useEffect(() => { loadData(); }, []); const loadData = async () => { setLoading(true); try { const [healthData, historyData] = await Promise.all([ financialHealthService.get(), financialHealthService.getHistory({ months: 6 }) ]); setData(healthData); setHistory(historyData); } catch (error) { console.error('Error loading financial health:', error); } finally { setLoading(false); } }; const getScoreColor = (score) => { if (score >= 80) return '#10b981'; if (score >= 60) return '#22c55e'; if (score >= 40) return '#f59e0b'; if (score >= 20) return '#f97316'; return '#ef4444'; }; const getScoreLabel = (score) => { if (score >= 80) return t('financialHealth.excellent'); if (score >= 60) return t('financialHealth.good'); if (score >= 40) return t('financialHealth.regular'); if (score >= 20) return t('financialHealth.bad'); return t('financialHealth.critical'); }; const metricConfigs = { savingsCapacity: { icon: 'bi-piggy-bank', color: '#10b981', gradient: 'linear-gradient(135deg, #059669 0%, #10b981 100%)', }, debtControl: { icon: 'bi-credit-card', color: '#3b82f6', gradient: 'linear-gradient(135deg, #2563eb 0%, #3b82f6 100%)', }, budgetManagement: { icon: 'bi-wallet2', color: '#8b5cf6', gradient: 'linear-gradient(135deg, #7c3aed 0%, #8b5cf6 100%)', }, investments: { icon: 'bi-graph-up-arrow', color: '#f59e0b', gradient: 'linear-gradient(135deg, #d97706 0%, #f59e0b 100%)', }, emergencyFund: { icon: 'bi-shield-check', color: '#06b6d4', gradient: 'linear-gradient(135deg, #0891b2 0%, #06b6d4 100%)', }, futurePlanning: { icon: 'bi-calendar-check', color: '#ec4899', gradient: 'linear-gradient(135deg, #db2777 0%, #ec4899 100%)', }, }; const getInsightIcon = (type) => { switch (type) { case 'success': return 'bi-check-circle-fill'; case 'warning': return 'bi-exclamation-triangle-fill'; case 'danger': return 'bi-x-circle-fill'; case 'info': return 'bi-info-circle-fill'; default: return 'bi-lightbulb-fill'; } }; const getInsightColor = (type) => { switch (type) { case 'success': return '#10b981'; case 'warning': return '#f59e0b'; case 'danger': return '#ef4444'; case 'info': return '#3b82f6'; default: return '#8b5cf6'; } }; if (loading) { return (
{t('common.loading')}
); } if (!data) { return (
No se pudo cargar la información de salud financiera.
); } const score = data.score; const scoreColor = getScoreColor(score); return (
{/* Header */}

{t('financialHealth.title')}

{t('financialHealth.subtitle')}

{/* Score Circle */}
{/* Score Ring */}
{/* Background circle */} {/* Progress circle */} {/* Score text */} {score} de 100
{getScoreLabel(score)}

{t('financialHealth.scoreDescription')}

{/* History Chart */} {history.length > 0 && (
h.month), datasets: [{ data: history.map(h => h.score), borderColor: scoreColor, backgroundColor: `${scoreColor}20`, fill: true, tension: 0.4, pointRadius: 3, pointBackgroundColor: scoreColor, }], }} options={{ responsive: true, maintainAspectRatio: false, plugins: { legend: { display: false } }, scales: { x: { display: false }, y: { display: false, min: 0, max: 100 }, }, }} />
)}
{/* Metrics Grid */}
{Object.entries(data.metrics).map(([key, metric]) => { const config = metricConfigs[key]; if (!config) return null; return (
{t(`financialHealth.metrics.${key}`)}

{metric.score}/100

{/* Progress bar */}
{/* Value if available */} {metric.value !== undefined && ( {typeof metric.value === 'number' && key !== 'emergencyFund' ? `${metric.value}%` : key === 'emergencyFund' ? `${metric.value} meses` : metric.value } )}
); })}
{/* Insights */}
{t('financialHealth.insights')}
{data.insights && data.insights.map((insight, index) => (

{insight.message}

))}
{/* Recommendations */} {data.recommendations && data.recommendations.length > 0 && (
{t('financialHealth.recommendations')}
{data.recommendations.map((rec, index) => ( {rec} ))}
)} {/* Quick Stats */}
{t('financialHealth.totalBalance')}
= 0 ? 'text-success' : 'text-danger'}`}> {currency(data.totals?.balance || 0)}
{t('financialHealth.monthlyIncome')}
{currency(data.totals?.income || 0)}
{t('financialHealth.monthlyExpenses')}
{currency(data.totals?.expense || 0)}
{t('financialHealth.savingsRate')}
{data.totals?.savings_rate || 0}%
); }; export default FinancialHealth;