"use client";

import { useEffect, useState } from "react";
import {
  fetchGroups,
  fetchSignals,
  Group,
  Signal,
  formatRelativeTime,
  getSignalIcon,
  getSeverityColor,
} from "../lib/api";

function GroupAgentCard({ group, signals, index }: { group: Group; signals: Signal[]; index: number }) {
  const recentSignals = signals.slice(0, 3);
  const latestTimestamp = signals[0]?.metadata?.timestamp;
  const criticalCount = signals.filter(
    (s) => s.metadata.severity === "critical" || s.metadata.severity === "high"
  ).length;

  const isActive = group.signal_count > 0;
  const statusLabel = isActive ? "ACTIVE" : "IDLE";
  const statusBg = isActive ? "rgba(16, 185, 129, 0.1)" : "rgba(168, 140, 251, 0.1)";
  const statusColor = isActive ? "#10b981" : "#a88cfb";

  const gradients = [
    ["#a88cfb", "#00daf3"],
    ["#432390", "#a88cfb"],
    ["#ee7d77", "#ffb300"],
    ["#00daf3", "#a88cfb"],
  ];
  const [colorA, colorB] = gradients[index % gradients.length];

  return (
    <div
      className="glass rounded-xl border flex flex-col relative overflow-hidden group card-interactive animate-fade-in-up opacity-0"
      style={{ borderColor: "rgba(255,255,255,0.05)", animationDelay: `${index * 100}ms` }}
    >
      {/* Top gradient bar */}
      <div
        className="h-1 w-full absolute top-0 left-0 opacity-80"
        style={{ background: `linear-gradient(to right, ${colorA}, ${colorB}, transparent)` }}
      />

      <div className="p-6 flex flex-col gap-5 w-full h-full mt-1">
        {/* Header */}
        <div className="flex justify-between items-start">
          <div className="space-y-1">
            <h4
              className="font-bold text-[15px] tracking-wide text-white drop-shadow-md"
              style={{ fontFamily: "'Inter Tight', sans-serif" }}
            >
              {group.group_name.toUpperCase()}
            </h4>
            <p className="text-[10px] font-mono-data opacity-50" style={{ color: "#a88cfb" }}>
              LENS: {group.lens?.toUpperCase() || "UNKNOWN"} · {group.signal_count} signals
            </p>
          </div>
          <span
            className="px-2.5 py-0.5 rounded text-[9px] font-bold font-mono-data border uppercase tracking-widest shadow-sm"
            style={{
              backgroundColor: statusBg,
              color: statusColor,
              borderColor: `${statusColor}4d`,
            }}
          >
            {statusLabel}
          </span>
        </div>

        {/* Current Task */}
        <div
          className="p-4 rounded-xl bg-black/40 border shadow-inner"
          style={{ borderColor: "rgba(255,255,255,0.03)" }}
        >
          <p className="text-[9px] uppercase tracking-widest mb-2 text-zinc-500 font-mono-data">
            Latest Signal
          </p>
          <p className="text-[12px] font-medium tracking-wide text-zinc-300 line-clamp-2">
            {recentSignals[0]?.document || "No signals yet"}
          </p>
        </div>

        {/* Metrics */}
        <div className="grid grid-cols-2 gap-4 py-4 border-y border-white/5">
          <div className="flex flex-col gap-1.5">
            <p className="text-[9px] uppercase tracking-widest text-zinc-500 font-mono-data">
              Total Signals
            </p>
            <p className="text-[13px] text-zinc-300 font-mono-data">{group.signal_count}</p>
          </div>
          <div className="flex flex-col gap-1.5">
            <p className="text-[9px] uppercase tracking-widest text-zinc-500 font-mono-data">
              High Priority
            </p>
            <p
              className="text-[13px] font-mono-data"
              style={{ color: criticalCount > 0 ? "#ff6f78" : "#10b981" }}
            >
              {criticalCount > 0 ? `${criticalCount} alerts` : "all clear"}
            </p>
          </div>
        </div>

        {/* Terminal Output - last 3 signals */}
        <div className="space-y-3 mt-auto flex-1 flex flex-col">
          <div className="flex justify-between items-center opacity-70">
            <p className="text-[9px] uppercase tracking-widest text-zinc-400 font-mono-data">
              Signal Stream
            </p>
            <span className="material-symbols-outlined text-[16px] text-zinc-500">terminal</span>
          </div>

          <div
            className="text-[10px] p-4 rounded-xl bg-black/50 border overflow-y-auto font-mono-data shadow-inner flex-1 mt-2"
            style={{
              borderColor: "rgba(255,255,255,0.03)",
              color: "#9ca3af",
              minHeight: "5rem",
              maxHeight: "7rem",
            }}
          >
            {recentSignals.length === 0 ? (
              <p className="opacity-40">no signals yet_</p>
            ) : (
              recentSignals.map((sig, idx) => (
                <p key={idx} className="mb-1.5 leading-relaxed">
                  <span style={{ color: getSeverityColor(sig.metadata.severity), opacity: 0.9 }}>
                    {sig.metadata.type}:
                  </span>{" "}
                  <span className="opacity-70 text-zinc-300">
                    {sig.document.slice(0, 60)}
                    {sig.document.length > 60 ? "…" : ""}
                  </span>
                </p>
              ))
            )}
            <p className="animate-pulse mt-2 opacity-40">_</p>
          </div>
        </div>
      </div>
    </div>
  );
}

export default function AgentCards() {
  const [groups, setGroups] = useState<Group[]>([]);
  const [groupSignals, setGroupSignals] = useState<Record<string, Signal[]>>({});
  const [loading, setLoading] = useState(true);

  useEffect(() => {
    async function load() {
      try {
        const grps = await fetchGroups();
        setGroups(grps);
        const sigMap: Record<string, Signal[]> = {};
        await Promise.all(
          grps.map(async (g) => {
            try {
              const sigs = await fetchSignals(g.group_id);
              sigMap[g.group_id] = sigs;
            } catch {
              sigMap[g.group_id] = [];
            }
          })
        );
        setGroupSignals(sigMap);
      } catch {
        // ignore
      } finally {
        setLoading(false);
      }
    }
    load();
  }, []);

  if (loading) {
    return (
      <div className="flex items-center justify-center py-20 text-zinc-600 col-span-3">
        <span className="material-symbols-outlined animate-spin mr-3">autorenew</span>
        Loading groups...
      </div>
    );
  }

  if (groups.length === 0) {
    return (
      <div className="text-center py-20 text-zinc-600 col-span-3">
        <span className="material-symbols-outlined text-4xl mb-3 block">group_off</span>
        <p>No monitored groups yet.</p>
        <p className="text-[11px] mt-2">Connect Telegram groups to see agents here.</p>
      </div>
    );
  }

  return (
    <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6 animate-fade-in-up">
      {groups.map((group, idx) => (
        <GroupAgentCard
          key={group.group_id}
          group={group}
          signals={groupSignals[group.group_id] || []}
          index={idx}
        />
      ))}
    </div>
  );
}
