"use client";

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

type Row = { id: number; enquirer_name: string; mobile: string; lead_date?: string };

export default function UnattendedPage() {
  const [rows, setRows] = useState<Row[]>([]);
  const [error, setError] = useState("");

  useEffect(() => {
    api<{ data: Row[] }>("/leads/unattended")
      .then((res) => setRows(res.data))
      .catch((err) => setError(err instanceof Error ? err.message : "Failed"));
  }, []);

  return (
    <AppShell>
      <div className="rise">
        <h1 style={{ fontFamily: "var(--font-display)" }}>Unattended leads</h1>
        {error ? <div style={{ color: "var(--danger)" }}>{error}</div> : null}
        <div className="ps-card" style={{ padding: "0.5rem" }}>
          <table className="ps-table">
            <thead>
              <tr>
                <th>Name</th>
                <th>Mobile</th>
                <th>Date</th>
              </tr>
            </thead>
            <tbody>
              {rows.map((r) => (
                <tr key={r.id}>
                  <td>
                    <Link href={`/leads/${r.id}`} style={{ color: "var(--brand-dark)", fontWeight: 600 }}>
                      {r.enquirer_name}
                    </Link>
                  </td>
                  <td>{r.mobile}</td>
                  <td>{r.lead_date || "—"}</td>
                </tr>
              ))}
            </tbody>
          </table>
          {!rows.length && !error ? <p style={{ padding: "1rem", color: "var(--muted)" }}>All clear.</p> : null}
        </div>
      </div>
    </AppShell>
  );
}
