"use client"; import { useMemo } from "react"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Progress } from "@/components/ui/progress"; import type { BankingData, Account, Folder } from "@/lib/types"; import { cn } from "@/lib/utils"; import { Building2, Folder as FolderIcon } from "lucide-react"; import { getAccountBalance } from "@/lib/account-utils"; interface AccountsSummaryProps { data: BankingData; } export function AccountsSummary({ data }: AccountsSummaryProps) { const formatCurrency = (amount: number) => { return new Intl.NumberFormat("fr-FR", { style: "currency", currency: "EUR", }).format(amount); }; // Group accounts by folder const accountsByFolder = useMemo(() => { const grouped: Record = {}; data.accounts.forEach((account) => { const folderId = account.folderId || "no-folder"; if (!grouped[folderId]) { grouped[folderId] = []; } grouped[folderId].push(account); }); // Sort accounts within each folder by name Object.keys(grouped).forEach((folderId) => { grouped[folderId].sort((a, b) => a.name.localeCompare(b.name)); }); return grouped; }, [data.accounts]); // Get root folders (folders without parent) sorted by name const rootFolders = useMemo(() => { return data.folders .filter((f) => !f.parentId) .sort((a, b) => a.name.localeCompare(b.name)); }, [data.folders]); // Helper to get child folders recursively const getChildFolders = (parentId: string): Folder[] => { return data.folders .filter((f) => f.parentId === parentId) .sort((a, b) => a.name.localeCompare(b.name)); }; // Render folder section recursively const renderFolderSection = (folder: Folder, level: number = 0) => { const folderAccounts = accountsByFolder[folder.id] || []; const childFolders = getChildFolders(folder.id); const folderTotal = folderAccounts.reduce( (sum, a) => sum + getAccountBalance(a), 0, ); if (folderAccounts.length === 0 && childFolders.length === 0) { return null; } return (
0 ? "mt-4" : ""}> {/* Folder header */}

0 && "text-muted-foreground")}> {folder.name}

{folderAccounts.length > 0 && ( ({folderAccounts.length}) )} {folderTotal !== 0 && ( = 0 ? "text-emerald-600" : "text-red-600", )} > {formatCurrency(folderTotal)} )}
{/* Accounts in this folder */} {folderAccounts.length > 0 && (
0 && "ml-4")}> {folderAccounts.map((account) => { const realBalance = getAccountBalance(account); const totalPositive = data.accounts .filter((a) => getAccountBalance(a) > 0) .reduce((sum, a) => sum + getAccountBalance(a), 0); const percentage = totalPositive > 0 ? Math.max(0, (realBalance / totalPositive) * 100) : 0; return (

{account.name}

{account.accountNumber}

= 0 ? "text-emerald-600" : "text-red-600", )} > {formatCurrency(realBalance)}
{realBalance > 0 && ( )}
); })}
)} {/* Child folders */} {childFolders.map((childFolder) => renderFolderSection(childFolder, level + 1), )}
); }; if (data.accounts.length === 0) { return ( Mes Comptes

Aucun compte

Importez un fichier OFX pour ajouter un compte

); } const orphanAccounts = accountsByFolder["no-folder"] || []; const orphanTotal = orphanAccounts.reduce( (sum, a) => sum + getAccountBalance(a), 0, ); return ( Mes Comptes
{/* Accounts without folder */} {orphanAccounts.length > 0 && (

Sans dossier

({orphanAccounts.length}) {orphanTotal !== 0 && ( = 0 ? "text-emerald-600" : "text-red-600", )} > {formatCurrency(orphanTotal)} )}
{orphanAccounts.map((account) => { const realBalance = getAccountBalance(account); const totalPositive = data.accounts .filter((a) => getAccountBalance(a) > 0) .reduce((sum, a) => sum + getAccountBalance(a), 0); const percentage = totalPositive > 0 ? Math.max(0, (realBalance / totalPositive) * 100) : 0; return (

{account.name}

{account.accountNumber}

= 0 ? "text-emerald-600" : "text-red-600", )} > {formatCurrency(realBalance)}
{realBalance > 0 && ( )}
); })}
)} {/* Folders */} {rootFolders.map((folder) => renderFolderSection(folder))}
); }