"use client";

/**
 * Reports hub — card grid of available analytics, role-scoped on the API.
 */

import Link from "next/link";
import { useEffect, useState } from "react";
import { AppShell } from "@/components/AppShell";
import { api } from "@/lib/api";

type Item = { slug: string; title: string; description: string; href?: string };

export default function ReportsHubPage() {
  const [items, setItems] = useState<Item[]>([]);

  useEffect(() => {
    api<{ data: Item[] }>("/reports").then((res) => setItems(res.data));
  }, []);

  return (
    <AppShell>
      <div className="ps-page rise">
        <header className="ps-page-header">
          <div>
            <h1 className="ps-page-title">Reports</h1>
            <p className="ps-page-sub">Lead-side analytics · scoped by your role (same filters as leads list)</p>
          </div>
        </header>

        <div className="ps-report-grid rise-stagger">
          {items.map((item, i) => (
            <Link
              key={item.slug}
              href={item.href || `/reports/${item.slug}`}
              className="ps-card ps-report-card rise"
              style={{ animationDelay: `${i * 45}ms` }}
            >
              <div style={{ fontFamily: "var(--font-display)", fontSize: "1.25rem", letterSpacing: "-0.01em" }}>{item.title}</div>
              <div style={{ color: "var(--muted)", fontSize: "0.92rem", lineHeight: 1.45 }}>{item.description}</div>
              <span style={{ color: "var(--brand-mid)", fontWeight: 700, fontSize: "0.85rem", marginTop: "0.25rem" }}>
                Open report →
              </span>
            </Link>
          ))}
          {!items.length ? (
            <div className="ps-card ps-card-static ps-empty" style={{ gridColumn: "1 / -1" }}>
              Loading reports…
            </div>
          ) : null}
        </div>
      </div>
    </AppShell>
  );
}
