"use client"; import { useState, useEffect } from "react"; import { Calendar, Download } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Card, CardContent } from "@/components/ui/card"; import { fetchBalanceSheet, type BalanceSheetData } from "@/lib/supabase/reports-actions"; import PageSkeleton from "./PageSkeleton"; const PERIOD_OPTIONS = [ { label: "Today", value: "today" }, { label: "End of Last Month", value: "last-month-end" }, { label: "End of Last Quarter", value: "last-quarter-end" }, { label: "End of Last Year", value: "last-year-end" }, ]; function getAsOfDate(period: string): string { const now = new Date(); const y = now.getFullYear(); const m = now.getMonth(); switch (period) { case "today": return `${y}-${String(m + 1).padStart(2, "0")}-${String(now.getDate()).padStart(2, "0")}`; case "last-month-end": return `${y}-${String(m).padStart(2, "0")}-${new Date(y, m, 0).getDate()}`; case "last-quarter-end": { const qm = Math.floor((m - 1) / 3) * 3; return `${y}-${String(Math.min(qm + 3, 12)).padStart(2, "0")}-${new Date(y, Math.min(qm + 3, 12), 0).getDate()}`; } case "last-year-end": return `${y - 1}-12-31`; default: return `${y}-${String(m + 1).padStart(2, "0")}-${String(now.getDate()).padStart(2, "0")}`; } } function formatCurrency(amount: number): string { return `$${amount.toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2 })}`; } export default function ReportsBalanceSheetPage() { const [period, setPeriod] = useState("today"); const [data, setData] = useState(null); const [loading, setLoading] = useState(true); useEffect(() => { const asOf = getAsOfDate(period); let cancelled = false; fetchBalanceSheet(asOf) .then((d) => { if (!cancelled) setData(d); }) .catch(() => {}) .finally(() => { if (!cancelled) setLoading(false); }); return () => { cancelled = true; }; }, [period]); if (loading) return ; if (!data) return null; return (
{/* Controls */}
As of {data.asOfDate}
{/* Summary */}

Total Assets

{formatCurrency(data.totalAssets)}

Total Liabilities

{formatCurrency(data.totalLiabilities)}

Net Assets

= 0 ? "text-emerald-600" : "text-red-600"}`}> {formatCurrency(data.netAssets)}

{/* Detailed Balance Sheet */}

Balance Sheet

{/* Assets */}

Assets

{data.assets.length > 0 ? ( data.assets.map((item) => (
{item.name} {formatCurrency(item.amount)}
)) ) : (

No assets recorded

)}
Total Assets {formatCurrency(data.totalAssets)}
{/* Liabilities */}

Liabilities

{data.liabilities.length > 0 ? ( data.liabilities.map((item) => (
{item.name} {formatCurrency(item.amount)}
)) ) : (

No liabilities recorded

)}
Total Liabilities {formatCurrency(data.totalLiabilities)}
{/* Equity */}

Equity

{data.equity.length > 0 ? ( data.equity.map((item) => (
{item.name} {formatCurrency(item.amount)}
)) ) : (

No equity tracked

)}
Net Assets {formatCurrency(data.netAssets)}
{/* Verification */}
Assets = Liabilities + Equity {Math.abs(data.totalAssets - (data.totalLiabilities + data.netAssets)) < 0.01 ? "Balanced ✓" : "Out of balance ✗"}
); }