"use client"; import { useState, useEffect } from "react"; import { UserReport as UserReportType } from "@fitai/shared"; import { Card, CardHeader, CardTitle, CardContent } from "@/components/ui/card"; import { WeeklyCheckInsCard } from "./WeeklyCheckInsCard"; import { NutritionSummaryCard } from "./NutritionSummaryCard"; import { HydrationSummaryCard } from "./HydrationSummaryCard"; import { GoalsSummaryCard } from "./GoalsSummaryCard"; import { RecommendationsCard } from "./RecommendationsCard"; import { Button } from "@/components/ui/button"; import { Download } from "lucide-react"; interface UserReportProps { userId: string; startDate: string; endDate: string; } export function UserReport({ userId, startDate, endDate }: UserReportProps) { const [report, setReport] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); useEffect(() => { fetchReport(); }, [userId, startDate, endDate]); const fetchReport = async () => { try { setLoading(true); setError(null); const response = await fetch( `/api/reports/user/${userId}?startDate=${startDate}&endDate=${endDate}`, ); if (!response.ok) { const errorData = await response.json(); throw new Error(errorData.error || "Failed to fetch report"); } const data = await response.json(); setReport(data); } catch (err) { setError(err instanceof Error ? err.message : "Failed to load report"); console.error("Report fetch error:", err); } finally { setLoading(false); } }; const handleExportPDF = () => { if (!report) return; const filename = `FitAI_Report_${report.user.firstName}_${report.user.lastName}_${startDate}_${endDate}.pdf`; const url = `/api/reports/user/${userId}?startDate=${startDate}&endDate=${endDate}&format=pdf`; // Open in new tab to trigger download window.open(url, "_blank"); }; if (loading) { return (
Loading report...
); } if (error) { return (
{error}
); } if (!report) { return (
No report data available
); } return (
{/* Report Header */}
{report.user.firstName} {report.user.lastName}

{report.user.email} • {report.client?.membershipType || "N/A"}{" "} Member

Report Period: {report.reportPeriod.startDate} to{" "} {report.reportPeriod.endDate}

{/* Report Sections */}
); }