"use client";

import Link from "next/link";
import { AppShell } from "@/components/app-shell";
import { Card, CardContent } from "@/components/ui/card";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
import { Badge } from "@/components/ui/badge";
import { LibraryListPanel } from "@/components/precedents/library-list-panel";
import { LibrarySearchPanel } from "@/components/precedents/library-search-panel";
import { HalachaReviewPanel } from "@/components/precedents/halacha-review-panel";
import { LibraryStatsPanel } from "@/components/precedents/library-stats-panel";
import { useHalachotPending } from "@/lib/api/precedent-library";

/**
 * Precedent Library admin page.
 *
 * Four tabs:
 *   - ספרייה — browse all uploaded precedents (filters + upload + delete)
 *   - חיפוש סמנטי — semantic search across halachot + chunks
 *   - ממתין לאישור — chair review queue (PRIMARY tab; halachot from
 *     auto-extraction must be approved before agents can use them)
 *   - סטטיסטיקה — counts and coverage
 *
 * Distinct from /training (style corpus = Daphna's voice) and the
 * per-case precedent attacher (chair-attached quotes scoped to a case).
 */

function PendingBadge() {
  const { data } = useHalachotPending();
  const n = data?.count ?? 0;
  if (!n) return null;
  return (
    <Badge
      variant="outline"
      className="ms-1 bg-gold-wash text-gold-deep border-gold/40 text-[0.65rem]"
    >
      {n}
    </Badge>
  );
}

export default function PrecedentsPage() {
  return (
    <AppShell>
      <section className="space-y-6">
        <header>
          <nav className="text-[0.78rem] text-ink-muted mb-1">
            <Link href="/" className="hover:text-gold-deep">בית</Link>
            <span aria-hidden> · </span>
            <span className="text-navy">ספריית פסיקה</span>
          </nav>
          <h1 className="text-navy mb-0">ספריית הפסיקה הסמכותית</h1>
          <p className="text-ink-muted text-sm mt-1 max-w-3xl">
            פסיקה חיצונית — פסקי דין של ערכאות עליונות והחלטות של ועדות ערר אחרות.
            כל קובץ עובר חילוץ הלכות אוטומטי, וההלכות ממתינות לאישור היו&quot;ר לפני
            שהן זמינות לסוכני הכתיבה (legal-writer וכו&apos;).
          </p>
        </header>

        <div className="h-[2px] bg-gradient-to-l from-transparent via-gold to-transparent" />

        <Card className="bg-surface border-rule shadow-sm">
          <CardContent className="px-6 py-5">
            <Tabs defaultValue="library" dir="rtl">
              <TabsList className="bg-rule-soft/60">
                <TabsTrigger value="library">ספרייה</TabsTrigger>
                <TabsTrigger value="search">חיפוש סמנטי</TabsTrigger>
                <TabsTrigger value="review">
                  ממתין לאישור
                  <PendingBadge />
                </TabsTrigger>
                <TabsTrigger value="stats">סטטיסטיקה</TabsTrigger>
              </TabsList>

              <TabsContent value="library" className="mt-5">
                <LibraryListPanel />
              </TabsContent>

              <TabsContent value="search" className="mt-5">
                <LibrarySearchPanel />
              </TabsContent>

              <TabsContent value="review" className="mt-5">
                <HalachaReviewPanel />
              </TabsContent>

              <TabsContent value="stats" className="mt-5">
                <LibraryStatsPanel />
              </TabsContent>
            </Tabs>
          </CardContent>
        </Card>
      </section>
    </AppShell>
  );
}
