"use client";

import { useEffect, useState, useCallback } from "react";
import { toast } from "sonner";
import { fetchLeakStatus, type LeakSensorStatus } from "@/lib/api";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { Skeleton } from "@/components/ui/skeleton";
import { Droplets, RefreshCw, CheckCircle2, AlertTriangle, MapPin, Wind, Clock } from "lucide-react";
import { cn } from "@/lib/utils";

const SITE_ID = "sg-01";

function SensorBadge({ state }: { state: string }) {
  const cfg = {
    detected: { cls: "bg-destructive/10 text-destructive border-destructive/30", label: "LEAK DETECTED" },
    clear:    { cls: "bg-green-500/10 text-green-400 border-green-500/20",      label: "Clear" },
    unknown:  { cls: "bg-muted/30 text-muted-foreground border-border",         label: "Unknown" },
  }[state] ?? { cls: "bg-muted/30 text-muted-foreground border-border", label: state };
  return (
    <span className={cn(
      "text-[10px] font-semibold px-2 py-0.5 rounded-full uppercase tracking-wide border",
      cfg.cls,
    )}>
      {cfg.label}
    </span>
  );
}

function SensorCard({ sensor }: { sensor: LeakSensorStatus }) {
  const detected = sensor.state === "detected";
  const sensorAny = sensor as LeakSensorStatus & { last_triggered_at?: string | null; trigger_count_30d?: number };
  const triggerCount30d = sensorAny.trigger_count_30d ?? 0;
  const lastTriggeredAt = sensorAny.last_triggered_at ?? null;

  let lastTriggeredText: string;
  if (lastTriggeredAt) {
    const daysAgo = Math.floor((Date.now() - new Date(lastTriggeredAt).getTime()) / (1000 * 60 * 60 * 24));
    lastTriggeredText = daysAgo === 0 ? "Today" : `${daysAgo}d ago`;
  } else if (detected) {
    lastTriggeredText = "Currently active";
  } else {
    lastTriggeredText = "No recent events";
  }

  return (
    <div className={cn(
      "rounded-xl border p-4 space-y-3 transition-colors",
      detected ? "border-destructive/50 bg-destructive/5" : "border-border bg-muted/5",
    )}>
      <div className="flex items-start justify-between gap-2">
        <div className="flex items-center gap-2">
          <div className={cn(
            "w-2.5 h-2.5 rounded-full shrink-0 mt-0.5",
            detected ? "bg-destructive animate-pulse" :
            sensor.state === "unknown" ? "bg-muted-foreground/30" : "bg-green-500",
          )} />
          <div>
            <p className="text-sm font-semibold leading-none">{sensor.sensor_id}</p>
            {sensor.floor_zone && (
              <p className="text-[10px] text-muted-foreground mt-0.5 flex items-center gap-1">
                <MapPin className="w-3 h-3" /> {sensor.floor_zone}
              </p>
            )}
          </div>
        </div>
        <div className="flex items-center gap-2">
          <span className="text-[10px] text-muted-foreground">{triggerCount30d} events (30d)</span>
          <SensorBadge state={sensor.state} />
        </div>
      </div>

      <div className="grid grid-cols-2 gap-2 text-xs">
        {sensor.room_id && (
          <div className="flex items-center gap-1.5 text-muted-foreground">
            <span>Room:</span>
            <span className="font-medium text-foreground capitalize">{sensor.room_id}</span>
          </div>
        )}
        {sensor.near_crac && (
          <div className="flex items-center gap-1.5 text-muted-foreground">
            <Wind className="w-3 h-3 shrink-0" />
            <span className="font-medium text-foreground">Near CRAC</span>
          </div>
        )}
        <div className="col-span-2 flex items-center gap-1.5 text-muted-foreground">
          <span>{sensor.under_floor ? "Under raised floor" : "Above floor level"}</span>
        </div>
        <div className="col-span-2 flex items-center gap-1.5 text-[10px] text-muted-foreground mt-0.5">
          <Clock className="w-3 h-3 shrink-0" />
          <span>{lastTriggeredText}</span>
        </div>
      </div>

      {detected && (
        <div className="rounded-lg bg-destructive/10 border border-destructive/30 px-3 py-2 text-xs text-destructive font-medium">
          Water detected — inspect immediately and isolate if necessary
        </div>
      )}
    </div>
  );
}

export default function LeakDetectionPage() {
  const [sensors, setSensors] = useState<LeakSensorStatus[]>([]);
  const [loading, setLoading] = useState(true);

  const load = useCallback(async () => {
    try {
      setSensors(await fetchLeakStatus(SITE_ID));
    } catch { toast.error("Failed to load leak sensor data"); }
    finally { setLoading(false); }
  }, []);

  useEffect(() => {
    load();
    const id = setInterval(load, 15_000);
    return () => clearInterval(id);
  }, [load]);

  const active   = sensors.filter((s) => s.state === "detected");
  const offline  = sensors.filter((s) => s.state === "unknown");
  const dry      = sensors.filter((s) => s.state === "clear");

  // Group by floor_zone
  const byZone = sensors.reduce<Record<string, LeakSensorStatus[]>>((acc, s) => {
    const zone = s.floor_zone ?? "Unassigned";
    (acc[zone] ??= []).push(s);
    return acc;
  }, {});

  const zoneEntries = Object.entries(byZone).sort(([a], [b]) => a.localeCompare(b));

  return (
    <div className="p-6 space-y-6">
      {/* Header */}
      <div className="flex items-start justify-between gap-4 flex-wrap">
        <div>
          <h1 className="text-xl font-semibold">Leak Detection</h1>
          <p className="text-sm text-muted-foreground">Singapore DC01 — water sensor site map · refreshes every 15s</p>
        </div>
        <div className="flex items-center gap-3">
          {!loading && (
            <span className={cn(
              "flex items-center gap-1.5 text-xs font-semibold px-3 py-1.5 rounded-full",
              active.length > 0
                ? "bg-destructive/10 text-destructive"
                : "bg-green-500/10 text-green-400",
            )}>
              {active.length > 0
                ? <><AlertTriangle className="w-3.5 h-3.5" /> {active.length} leak{active.length > 1 ? "s" : ""} detected</>
                : <><CheckCircle2 className="w-3.5 h-3.5" /> No leaks detected</>}
            </span>
          )}
          <button
            onClick={load}
            className="flex items-center gap-1.5 text-xs text-muted-foreground hover:text-foreground transition-colors"
          >
            <RefreshCw className="w-3.5 h-3.5" /> Refresh
          </button>
        </div>
      </div>

      {/* KPI bar */}
      {!loading && (
        <div className="grid grid-cols-3 gap-3">
          {[
            {
              label: "Active Leaks",
              value: active.length,
              sub: "require immediate action",
              cls: active.length > 0 ? "border-destructive/40 bg-destructive/5 text-destructive" : "border-border bg-muted/10 text-green-400",
            },
            {
              label: "Sensors Clear",
              value: dry.length,
              sub: `of ${sensors.length} total sensors`,
              cls: "border-border bg-muted/10 text-foreground",
            },
            {
              label: "Offline",
              value: offline.length,
              sub: "no signal",
              cls: offline.length > 0 ? "border-amber-500/30 bg-amber-500/5 text-amber-400" : "border-border bg-muted/10 text-muted-foreground",
            },
          ].map(({ label, value, sub, cls }) => (
            <div key={label} className={cn("rounded-xl border px-4 py-3", cls)}>
              <p className="text-[10px] uppercase tracking-wider mb-1 opacity-70">{label}</p>
              <p className="text-2xl font-bold tabular-nums leading-none">{value}</p>
              <p className="text-[10px] opacity-60 mt-1">{sub}</p>
            </div>
          ))}
        </div>
      )}

      {/* Active leak alert */}
      {!loading && active.length > 0 && (
        <div className="rounded-xl border border-destructive/50 bg-destructive/10 px-5 py-4">
          <div className="flex items-center gap-3 mb-3">
            <Droplets className="w-5 h-5 text-destructive shrink-0" />
            <p className="text-sm font-semibold text-destructive">
              {active.length} water leak{active.length > 1 ? "s" : ""} detected — immediate action required
            </p>
          </div>
          <div className="space-y-1">
            {active.map((s) => (
              <p key={s.sensor_id} className="text-xs text-destructive/80">
                • <strong>{s.sensor_id}</strong>
                {s.floor_zone ? ` — ${s.floor_zone}` : ""}
                {s.near_crac  ? ` (near ${s.near_crac})` : ""}
                {s.under_floor ? " — under raised floor" : ""}
              </p>
            ))}
          </div>
        </div>
      )}

      {/* Zone panels */}
      {loading ? (
        <div className="grid grid-cols-1 md:grid-cols-2 gap-4">
          {Array.from({ length: 4 }).map((_, i) => <Skeleton key={i} className="h-48" />)}
        </div>
      ) : (
        zoneEntries.map(([zone, zoneSensors]) => {
          const zoneActive = zoneSensors.filter((s) => s.state === "detected");
          return (
            <div key={zone}>
              <div className="flex items-center gap-2 mb-3">
                <h2 className="text-xs font-semibold uppercase tracking-widest text-muted-foreground">{zone}</h2>
                {zoneActive.length > 0 && (
                  <span className="text-[10px] font-semibold text-destructive bg-destructive/10 px-2 py-0.5 rounded-full">
                    {zoneActive.length} LEAK
                  </span>
                )}
              </div>
              <div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-3">
                {zoneSensors.map((s) => <SensorCard key={s.sensor_id} sensor={s} />)}
              </div>
            </div>
          );
        })
      )}
    </div>
  );
}
