"use client" import { useMemo } from "react" import { useQuery } from "convex/react" import { IconClockHour4, IconMessages, IconTrendingDown, IconTrendingUp } from "@tabler/icons-react" import { api } from "@/convex/_generated/api" import type { Id } from "@/convex/_generated/dataModel" import { useAuth } from "@/lib/auth-client" import { DEFAULT_TENANT_ID } from "@/lib/constants" import { Badge } from "@/components/ui/badge" import { Card, CardAction, CardDescription, CardFooter, CardHeader, CardTitle, } from "@/components/ui/card" import { Skeleton } from "@/components/ui/skeleton" function formatMinutes(value: number | null) { if (value === null) return "—" if (value < 60) return `${Math.round(value)} min` const hours = Math.floor(value / 60) const minutes = Math.round(value % 60) if (minutes === 0) return `${hours}h` return `${hours}h ${minutes}min` } function formatScore(value: number | null) { if (value === null) return "—" return value.toFixed(2) } export function SectionCards() { const { session, convexUserId, isStaff } = useAuth() const tenantId = session?.user.tenantId ?? DEFAULT_TENANT_ID const dashboardEnabled = Boolean(isStaff && convexUserId) const dashboard = useQuery( api.reports.dashboardOverview, dashboardEnabled ? { tenantId, viewerId: convexUserId as Id<"users"> } : "skip" ) const trendInfo = useMemo(() => { if (!dashboard?.newTickets) return { value: null, label: "Aguardando dados", icon: IconTrendingUp } const trend = dashboard.newTickets.trendPercentage if (trend === null) { return { value: null, label: "Sem histórico", icon: IconTrendingUp } } const positive = trend >= 0 const icon = positive ? IconTrendingUp : IconTrendingDown const label = `${positive ? "+" : ""}${trend.toFixed(1)}%` return { value: trend, label, icon } }, [dashboard]) const responseDelta = useMemo(() => { if (!dashboard?.firstResponse) return { delta: null, label: "Sem dados", positive: false } const delta = dashboard.firstResponse.deltaMinutes if (delta === null) return { delta: null, label: "Sem comparação", positive: false } const positive = delta <= 0 const value = `${delta > 0 ? "+" : ""}${Math.round(delta)} min` return { delta, label: value, positive } }, [dashboard]) const TrendIcon = trendInfo.icon return (