"use client";

import { useState } from "react";

type AnalyticsOverview = {
  overview: {
    total_queries: number;
    tool_usage: Record<string, number>;
    redflag_count: number;
    active_users: number;
  };
};

const API_BASE =
  process.env.NEXT_PUBLIC_API_URL?.replace(/\/$/, "") || "http://localhost:8000";

export function AnalyticsPanel() {
  const [tenantId, setTenantId] = useState("tenant123");
  const [loading, setLoading] = useState(false);
  const [error, setError] = useState<string | null>(null);
  const [data, setData] = useState<AnalyticsOverview["overview"] | null>(null);

  async function fetchAnalytics() {
    setLoading(true);
    setError(null);
    try {
      const res = await fetch(`${API_BASE}/analytics/overview`, {
        headers: {
          "x-tenant-id": tenantId,
        },
      });
      if (!res.ok) {
        throw new Error(`Analytics endpoint returned ${res.status}`);
      }
      const payload: AnalyticsOverview = await res.json();
      setData(payload.overview);
    } catch (err) {
      console.error(err);
      setError(
        err instanceof Error
          ? err.message
          : "Unable to reach analytics API. Is the FastAPI service running?",
      );
    } finally {
      setLoading(false);
    }
  }

  return (
    <section
      id="analytics"
      className="glass-panel border border-white/10 p-6 text-white"
    >
      <div className="flex flex-wrap items-center justify-between gap-4">
        <div>
          <p className="text-sm uppercase tracking-[0.5em] text-cyan-200/70">
            Compliance Pulse
          </p>
          <h2 className="mt-2 text-3xl font-semibold">Analytics snapshot</h2>
        </div>
        <div className="flex flex-col gap-2 text-sm text-slate-200">
          <label className="text-xs uppercase tracking-widest text-slate-400">
            Tenant ID
          </label>
          <input
            value={tenantId}
            onChange={(e) => setTenantId(e.target.value)}
            className="rounded-full border border-white/15 bg-white/10 px-4 py-1.5 text-sm text-white outline-none focus:border-cyan-300"
          />
        </div>
        <button
          onClick={fetchAnalytics}
          disabled={loading}
          className="rounded-full bg-white/90 px-5 py-2.5 text-sm font-semibold text-slate-900 shadow-lg shadow-cyan-500/30 transition hover:-translate-y-0.5 disabled:opacity-60"
        >
          {loading ? "Loading…" : "Refresh metrics"}
        </button>
      </div>

      <div className="mt-6 grid gap-4 md:grid-cols-4">
        {["total_queries", "active_users", "redflag_count"].map((key) => (
          <div
            key={key}
            className="rounded-2xl border border-white/5 bg-slate-900/40 p-4 text-center"
          >
            <p className="text-sm uppercase tracking-widest text-slate-400">
              {key.replace("_", " ")}
            </p>
            <p className="mt-2 text-3xl font-semibold">
              {data ? data[key as keyof typeof data] : "—"}
            </p>
          </div>
        ))}
        <div className="rounded-2xl border border-white/5 bg-slate-900/40 p-4 text-center">
          <p className="text-sm uppercase tracking-widest text-slate-400">
            Tool usage (top)
          </p>
          <p className="mt-2 text-3xl font-semibold">
            {data
              ? Object.entries(data.tool_usage)
                  .sort((a, b) => b[1] - a[1])[0]?.[0] ?? "—"
              : "—"}
          </p>
        </div>
      </div>

      <div className="mt-6 rounded-2xl border border-white/5 bg-slate-950/50 p-4">
        <p className="text-sm uppercase tracking-[0.5em] text-slate-400">
          Raw tool usage
        </p>
        <div className="mt-4 grid gap-3 sm:grid-cols-3">
          {data
            ? Object.entries(data.tool_usage).map(([tool, count]) => (
                <div
                  key={tool}
                  className="rounded-xl border border-white/10 bg-white/5 px-4 py-3"
                >
                  <p className="text-sm uppercase tracking-widest text-slate-400">
                    {tool}
                  </p>
                  <p className="text-2xl font-semibold text-white">{count}</p>
                </div>
              ))
            : Array.from({ length: 3 }).map((_, idx) => (
                <div
                  key={idx}
                  className="rounded-xl border border-white/10 bg-white/5 px-4 py-3 text-slate-500"
                >
                  <p className="text-sm uppercase tracking-widest text-slate-500">
                    Tool {idx + 1}
                  </p>
                  <p className="text-2xl font-semibold text-slate-500">—</p>
                </div>
              ))}
        </div>
      </div>

      {error && (
        <p className="mt-4 rounded-2xl border border-red-500/40 bg-red-500/10 px-4 py-3 text-sm text-red-200">
          {error}
        </p>
      )}
    </section>
  );
}

