"use client"

import { useQuery } from "convex/react"
import { api } from "@/convex/_generated/api"
import { DEFAULT_TENANT_ID } from "@/lib/constants"
import type { TicketQueueSummary } from "@/lib/schemas/ticket"
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"
import { Progress } from "@/components/ui/progress"
import { useAuth } from "@/lib/auth-client"
import type { Id } from "@/convex/_generated/dataModel"

interface TicketQueueSummaryProps {
  queues?: TicketQueueSummary[]
}

export function TicketQueueSummaryCards({ queues }: TicketQueueSummaryProps) {
  const { convexUserId } = useAuth()
  const queueArgs = convexUserId
    ? { tenantId: DEFAULT_TENANT_ID, viewerId: convexUserId as Id<"users"> }
    : "skip"
  const fromServer = useQuery(convexUserId ? api.queues.summary : "skip", queueArgs)
  const data: TicketQueueSummary[] = (queues ?? (fromServer as TicketQueueSummary[] | undefined) ?? [])

  if (!queues && fromServer === undefined) {
    return (
      <div className="grid gap-4 md:grid-cols-2 xl:grid-cols-3">
        {Array.from({ length: 3 }).map((_, index) => (
          <div key={index} className="rounded-2xl border border-slate-200 bg-white p-4 shadow-sm">
            <div className="h-4 w-24 animate-pulse rounded bg-slate-100" />
            <div className="mt-4 h-3 w-full animate-pulse rounded bg-slate-100" />
          </div>
        ))}
      </div>
    )
  }

  return (
    <div className="grid gap-4 md:grid-cols-2 xl:grid-cols-3">
      {data.map((queue) => {
        const total = queue.pending + queue.waiting
        const breachPercent = total === 0 ? 0 : Math.round((queue.breached / total) * 100)
        return (
          <Card key={queue.id} className="rounded-2xl border border-slate-200 bg-white p-4 shadow-sm">
            <CardHeader className="pb-2">
              <CardDescription className="text-xs uppercase tracking-wide text-neutral-500">Fila</CardDescription>
              <CardTitle className="text-lg font-semibold text-neutral-900">{queue.name}</CardTitle>
            </CardHeader>
            <CardContent className="flex flex-col gap-3 text-sm text-neutral-600">
              <div className="flex justify-between">
                <span>Pendentes</span>
                <span className="font-semibold text-neutral-900">{queue.pending}</span>
              </div>
              <div className="flex justify-between">
                <span>Aguardando resposta</span>
                <span className="font-semibold text-neutral-900">{queue.waiting}</span>
              </div>
              <div className="flex items-center justify-between">
                <span>Violados</span>
                <span className="font-semibold text-red-600">{queue.breached}</span>
              </div>
              <div className="pt-1.5">
                <Progress value={breachPercent} className="h-1.5 bg-slate-100" indicatorClassName="bg-[#00e8ff]" />
                <span className="mt-2 block text-xs text-neutral-500">
                  {breachPercent}% com SLA violado nesta fila
                </span>
              </div>
            </CardContent>
          </Card>
        )
      })}
    </div>
  )
}
