"use client" import { useState } from "react" import { Sparkles, Loader2, X, AlertTriangle, TrendingUp, Wrench } from "lucide-react" import { formatCurrency } from "@/lib/utils" interface Summary { reportTitle: string reportDate: string propertyName: string totalRequests: number openRequests: number resolvedRequests: number urgentItems: string[] summary: string recommendations: string[] estimatedTotalCost: number } export function AiMaintenanceSummary({ propertyId }: { propertyId: string }) { const [loading, setLoading] = useState(false) const [summary, setSummary] = useState(null) const [error, setError] = useState(null) const [open, setOpen] = useState(false) async function generate() { setLoading(true) setError(null) setOpen(true) try { const res = await fetch("/api/ai/maintenance-summary", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ property_id: propertyId }), }) const data = await res.json() if (!res.ok) { setError(data.error ?? "Failed to generate summary"); return } setSummary(data) } catch { setError("Network error. Please try again.") } finally { setLoading(false) } } return ( <> {/* Slide-over panel */} {open && (
setOpen(false)} />

AI Maintenance Report

{loading && (

Analysing maintenance data…

)} {error && (

{error}

)} {summary && !loading && (

{summary.reportTitle}

{summary.reportDate} · {summary.propertyName}

{/* Stats */}
{[ { label: "Total", value: summary.totalRequests, color: "text-white" }, { label: "Open", value: summary.openRequests, color: "text-amber-400" }, { label: "Resolved", value: summary.resolvedRequests, color: "text-emerald-400" }, ].map((s) => (

{s.value}

{s.label}

))}
{/* Cost */} {summary.estimatedTotalCost > 0 && (
Estimated total cost {formatCurrency(summary.estimatedTotalCost)}
)} {/* Summary */}

Overview

{summary.summary}

{/* Urgent items */} {summary.urgentItems?.length > 0 && (

Urgent Items

    {summary.urgentItems.map((item, i) => (
  • {item}
  • ))}
)} {/* Recommendations */} {summary.recommendations?.length > 0 && (

Recommendations

    {summary.recommendations.map((rec, i) => (
  • {i + 1} {rec}
  • ))}
)}
)}
)} ) }