import React, { useEffect, useState } from 'react'; import { supabase, type Debt } from '../lib/supabase'; import { Button } from '@/components/ui/button'; import { DebtCard } from './DebtCard'; import { DebtTimeline } from './DebtTimeline'; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs'; import { Badge } from '@/components/ui/badge'; import { Separator } from '@/components/ui/separator'; import { DollarSign, TrendingUp, Mail, CheckCircle, AlertTriangle, RefreshCw, BarChart3, LogOut } from 'lucide-react'; export function Dashboard() { const [debts, setDebts] = useState([]); const [loading, setLoading] = useState(true); const [stats, setStats] = useState({ totalDebts: 0, totalAmount: 0, projectedSavings: 0, settledCount: 0 }); useEffect(() => { fetchDebts(); setupRealtimeSubscription(); }, []); useEffect(() => { calculateStats(); }, [debts]); const fetchDebts = async () => { try { const { data, error } = await supabase .from('debts') .select('*') .order('created_at', { ascending: false }); if (error) throw error; setDebts(data || []); } catch (error) { console.error('Error fetching debts:', error); } finally { setLoading(false); } }; const setupRealtimeSubscription = () => { const subscription = supabase .channel('debts_changes') .on( 'postgres_changes', { event: '*', schema: 'public', table: 'debts' }, (payload) => { if (payload.eventType === 'INSERT') { setDebts(prev => [payload.new as Debt, ...prev]); } else if (payload.eventType === 'UPDATE') { setDebts(prev => prev.map(debt => debt.id === payload.new.id ? payload.new as Debt : debt ) ); } else if (payload.eventType === 'DELETE') { setDebts(prev => prev.filter(debt => debt.id !== payload.old.id)); } } ) .subscribe(); return () => { subscription.unsubscribe(); }; }; const calculateStats = () => { const totalDebts = debts.length; const totalAmount = debts.reduce((sum, debt) => sum + debt.amount, 0); const projectedSavings = debts.reduce((sum, debt) => sum + debt.projected_savings, 0); const settledCount = debts.filter(debt => debt.status === 'settled').length; setStats({ totalDebts, totalAmount, projectedSavings, settledCount }); }; const handleSignOut = async () => { await supabase.auth.signOut(); window.location.href = '/'; }; const formatCurrency = (amount: number) => { return new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' }).format(amount); }; const groupedDebts = { all: debts, active: debts.filter(debt => ['received', 'negotiating'].includes(debt.status)), settled: debts.filter(debt => debt.status === 'settled'), failed: debts.filter(debt => ['failed', 'opted_out'].includes(debt.status)) }; if (loading) { return (
Loading dashboard...
); } return (
{/* Header */}

InboxNegotiator Dashboard

AI-powered debt resolution platform with real-time updates

{/* Stats Cards */}
Total Debts
{stats.totalDebts}

Emails processed

Total Amount
{formatCurrency(stats.totalAmount)}

Across all debts

Projected Savings
{formatCurrency(stats.projectedSavings)}

From negotiations

Settled Cases
{stats.settledCount}

Successfully resolved

{/* Main Content */} All Debts {groupedDebts.all.length} Active {groupedDebts.active.length} Settled {groupedDebts.settled.length} Failed/Opted Out {groupedDebts.failed.length} {Object.entries(groupedDebts).map(([key, debtList]) => ( {debtList.length === 0 ? (

No debts found

{key === 'all' ? 'Forward your first debt email to get started with automated negotiations.' : `No debts with ${key} status found.` }

) : (
{debtList.map((debt) => (
))}
)}
))}
{/* Footer */}

InboxNegotiator - FDCPA-compliant debt resolution platform

Real-time updates powered by Supabase

); }