import React, { useState, useEffect } from 'react'; import { useUserStore } from '@/store/userStore'; import apiService from '@/services/api'; import SimulatorChart from './SimulatorChart'; import { Download, Maximize2, Calendar } from 'lucide-react'; const ForecastTab: React.FC = () => { const { isDark } = useUserStore(); const [data, setData] = useState(null); const [loading, setLoading] = useState(true); const [periods, setPeriods] = useState(12); const [interval, setInterval] = useState('monthly'); const [variables, setVariables] = useState>({}); const [modelInfo, setModelInfo] = useState(null); const cardBg = isDark ? 'rgba(255,255,255,0.03)' : '#ffffff'; const cardBorder = isDark ? 'rgba(255,255,255,0.06)' : 'rgba(0,0,0,0.06)'; const textPrimary = isDark ? '#f8fafc' : '#0f172a'; const textMuted = isDark ? '#94a3b8' : '#64748b'; useEffect(() => { const fetchVars = async () => { try { const [varRes, ovRes] = await Promise.all([ apiService.getSimulatorVariables(), apiService.getSimulatorOverview(), ]); if (ovRes.data?.model_info) setModelInfo(ovRes.data.model_info); if (varRes.data) { const init: Record = {}; varRes.data.forEach((v: any) => { init[v.name] = v.current_value; }); setVariables(init); } } catch {} }; fetchVars(); }, []); useEffect(() => { if (Object.keys(variables).length === 0) return; const fetchForecast = async () => { setLoading(true); try { const res = await apiService.getSimulatorForecast(variables, periods, interval); setData(res.data); } catch (err) { console.error(err); } finally { setLoading(false); } }; fetchForecast(); }, [variables, periods, interval]); const targetUnit = modelInfo?.target_unit || ''; const targetName = modelInfo?.target_name || modelInfo?.target_column?.replace(/_/g, ' ').replace(/\b\w/g, (c: string) => c.toUpperCase()) || 'Target'; const formatValue = (val: any) => { if (typeof val !== 'number' || isNaN(val)) return '0'; if (targetUnit === '%') return `${val.toFixed(1)}%`; if (targetUnit === '₹') { if (val >= 10000000) return `₹${(val / 10000000).toFixed(2)} Cr`; if (val >= 100000) return `₹${(val / 100000).toFixed(1)} L`; return `₹${val.toLocaleString()}`; } return val >= 1000000 ? `${(val / 1000000).toFixed(1)}M` : val.toLocaleString(); }; return (
{/* Controls bar */}
{['3', '6', '12', '24'].map(p => ( ))}
{/* Main Chart */}

Prediction Timeline ({targetName})

{loading ? (
) : data?.chart_data ? ( formatValue(v)} /> ) : (

No forecast data available

)}
{/* Summary Cards */} {data?.summary && (
{[ { label: `Expected ${targetName}`, value: data.summary.expected, color: '#6366f1' }, { label: '95% Range', value: data.summary.confidence_range, color: '#818cf8' }, { label: 'Best Case', value: data.summary.best_case, color: '#22c55e' }, { label: 'Worst Case', value: data.summary.worst_case, color: '#ef4444' }, { label: 'Baseline', value: data.summary.baseline, color: isDark ? '#475569' : '#94a3b8' }, ].map((item, i) => (

{item.label}

{item.value}

))}
)}
); }; export default ForecastTab;