"use client";

import { useEffect, useMemo, useState } from "react";
import { Check, X, Edit2, ChevronDown, ChevronUp, AlertTriangle } from "lucide-react";
import { toast } from "sonner";
import { Button } from "@/components/ui/button";
import { Badge } from "@/components/ui/badge";
import { Skeleton } from "@/components/ui/skeleton";
import { Textarea } from "@/components/ui/textarea";
import {
  useHalachotPending, useUpdateHalacha, type Halacha,
} from "@/lib/api/precedent-library";

/**
 * Halacha review queue — the chair-only path between automatic
 * extraction and agent visibility. Per the project's review policy,
 * NO halacha is auto-published; every row sits in pending_review until
 * approved.
 *
 * UX is optimised for high-throughput approval:
 *   - Keyboard: J/K cycle rows, A approve, R reject, E edit, Esc cancel
 *   - Side-by-side: rule_statement vs supporting_quote
 *   - Quote-verified pill: green check or red triangle
 *   - Confidence sort: low-confidence rows shown first to be skeptical of
 */

function formatDate(iso: string | null | undefined) {
  if (!iso) return "—";
  try {
    return new Date(iso).toLocaleDateString("he-IL");
  } catch {
    return iso;
  }
}

type EditState = { rule_statement: string; reasoning_summary: string };

function HalachaCard({
  h, focused, onApprove, onReject, onSave,
}: {
  h: Halacha;
  focused: boolean;
  onApprove: () => void;
  onReject: () => void;
  onSave: (patch: Partial<EditState>) => Promise<void>;
}) {
  const [editing, setEditing] = useState(false);
  const [draft, setDraft] = useState<EditState>({
    rule_statement: h.rule_statement,
    reasoning_summary: h.reasoning_summary,
  });

  // Reset the editable draft whenever the underlying halacha row changes
  // (id or any of its synced fields). Cascade-render is intentional here —
  // the form is keyed off the row so a re-mount would also work but loses
  // editing affordances on rapid navigation.
  useEffect(() => {
    // eslint-disable-next-line react-hooks/set-state-in-effect
    setDraft({
      rule_statement: h.rule_statement,
      reasoning_summary: h.reasoning_summary,
    });
  }, [h.id, h.rule_statement, h.reasoning_summary]);

  const onSubmitEdit = async () => {
    await onSave(draft);
    setEditing(false);
  };

  return (
    <div
      data-halacha-id={h.id}
      className={`
        rounded-lg border bg-surface p-4 space-y-3 transition-colors
        ${focused ? "border-gold ring-2 ring-gold/40 shadow-md" : "border-rule"}
      `}
    >
      {/* Header */}
      <div className="flex items-start gap-2 text-[0.78rem] text-ink-muted flex-wrap">
        <span className="font-mono text-navy" dir="ltr">{h.case_number ?? "—"}</span>
        {h.court && <span>· {h.court}</span>}
        {h.decision_date && <span>· {formatDate(h.decision_date)}</span>}
        {h.precedent_level && <span>· {h.precedent_level}</span>}

        <span className="ms-auto flex items-center gap-2">
          {h.quote_verified ? (
            <Badge variant="outline" className="text-[0.65rem] bg-gold-wash text-gold-deep border-gold/40">
              <Check className="w-3 h-3 me-1" /> ציטוט מאומת
            </Badge>
          ) : (
            <Badge variant="outline" className="text-[0.65rem] bg-danger-bg text-danger border-danger/40">
              <AlertTriangle className="w-3 h-3 me-1" /> ציטוט לא מאומת
            </Badge>
          )}
          <Badge variant="outline" className="text-[0.65rem] tabular-nums">
            confidence {h.confidence.toFixed(2)}
          </Badge>
          <Badge variant="outline" className="text-[0.65rem]">
            {h.rule_type}
          </Badge>
        </span>
      </div>

      {/* Side-by-side rule vs quote */}
      <div className="grid md:grid-cols-2 gap-3">
        <div>
          <div className="text-[0.7rem] text-ink-muted mb-1">ניסוח הכלל</div>
          {editing ? (
            <Textarea
              value={draft.rule_statement} rows={4} dir="rtl"
              onChange={(e) => setDraft({ ...draft, rule_statement: e.target.value })}
              className="bg-gold-wash/50 border-gold/30"
            />
          ) : (
            <p className="text-navy font-medium leading-relaxed" dir="rtl">
              {h.rule_statement}
            </p>
          )}
        </div>
        <div>
          <div className="text-[0.7rem] text-ink-muted mb-1">
            ציטוט תומך
            {h.page_reference && (
              <span className="ms-2 text-[0.65rem]">({h.page_reference})</span>
            )}
          </div>
          <blockquote className="text-ink-soft text-sm leading-relaxed border-r-2 border-gold pr-3" dir="rtl">
            &ldquo;{h.supporting_quote}&rdquo;
          </blockquote>
        </div>
      </div>

      {/* Reasoning + tags (collapsible) */}
      {(editing || h.reasoning_summary) && (
        <div>
          <div className="text-[0.7rem] text-ink-muted mb-1">תמצית ההיגיון</div>
          {editing ? (
            <Textarea
              value={draft.reasoning_summary} rows={2} dir="rtl"
              onChange={(e) => setDraft({ ...draft, reasoning_summary: e.target.value })}
              className="bg-gold-wash/50 border-gold/30"
            />
          ) : (
            <p className="text-ink-soft text-sm leading-relaxed" dir="rtl">
              {h.reasoning_summary}
            </p>
          )}
        </div>
      )}

      <div className="flex items-center gap-2 flex-wrap">
        {h.practice_areas?.map((p) => (
          <Badge key={p} variant="outline" className="text-[0.65rem] bg-navy-soft/30 text-navy">
            {p}
          </Badge>
        ))}
        {h.subject_tags?.map((t) => (
          <Badge key={t} variant="outline" className="text-[0.65rem]">
            {t}
          </Badge>
        ))}
      </div>

      <div className="flex items-center gap-2 justify-end pt-1 border-t border-rule-soft">
        {editing ? (
          <>
            <Button size="sm" variant="ghost" onClick={() => setEditing(false)}>
              ביטול
            </Button>
            <Button size="sm" onClick={onSubmitEdit}
              className="bg-navy text-parchment hover:bg-navy-soft">
              שמור שינויים
            </Button>
          </>
        ) : (
          <>
            <Button size="sm" variant="ghost" onClick={() => setEditing(true)}>
              <Edit2 className="w-3.5 h-3.5 me-1" />
              ערוך (E)
            </Button>
            <Button size="sm" variant="ghost"
              onClick={onReject}
              className="text-danger hover:text-danger hover:bg-danger-bg">
              <X className="w-3.5 h-3.5 me-1" />
              דחה (R)
            </Button>
            <Button size="sm" onClick={onApprove}
              className="bg-gold text-navy hover:bg-gold-deep">
              <Check className="w-3.5 h-3.5 me-1" />
              אשר (A)
            </Button>
          </>
        )}
      </div>
    </div>
  );
}

export function HalachaReviewPanel() {
  const { data, isPending, error } = useHalachotPending(500);
  const update = useUpdateHalacha();
  const [focusIdx, setFocusIdx] = useState(0);

  // Sort: low confidence first → forces the chair to scrutinize doubtful rows
  const items = useMemo(() => {
    const list = [...(data?.items ?? [])];
    list.sort((a, b) => a.confidence - b.confidence);
    return list;
  }, [data]);

  // Keep focus index in range as items change (e.g. after approve/reject
  // shrinks the queue). Cascade-render is intentional and bounded.
  useEffect(() => {
    if (focusIdx >= items.length && items.length > 0) {
      // eslint-disable-next-line react-hooks/set-state-in-effect
      setFocusIdx(items.length - 1);
    }
  }, [items.length, focusIdx]);

  // Scroll the focused row into view
  useEffect(() => {
    if (!items.length) return;
    const target = items[focusIdx];
    if (!target) return;
    const el = document.querySelector(`[data-halacha-id="${target.id}"]`);
    el?.scrollIntoView({ block: "nearest", behavior: "smooth" });
  }, [focusIdx, items]);

  const focused = items[focusIdx];

  const review = async (
    h: Halacha,
    status: "approved" | "rejected",
    extra?: Partial<EditState>,
  ) => {
    try {
      await update.mutateAsync({
        id: h.id,
        patch: {
          review_status: status,
          ...extra,
        },
      });
      toast.success(status === "approved" ? "אושר" : "נדחה");
    } catch (e) {
      toast.error(e instanceof Error ? e.message : "שגיאה");
    }
  };

  // Keyboard navigation
  useEffect(() => {
    const onKey = (e: KeyboardEvent) => {
      const tag = (e.target as HTMLElement)?.tagName?.toLowerCase();
      if (tag === "input" || tag === "textarea") return;
      if (!focused) return;
      if (e.key === "j") {
        e.preventDefault();
        setFocusIdx((i) => Math.min(items.length - 1, i + 1));
      } else if (e.key === "k") {
        e.preventDefault();
        setFocusIdx((i) => Math.max(0, i - 1));
      } else if (e.key === "a" || e.key === "A") {
        e.preventDefault();
        review(focused, "approved");
      } else if (e.key === "r" || e.key === "R") {
        e.preventDefault();
        if (window.confirm("לדחות הלכה זו?")) {
          review(focused, "rejected");
        }
      }
    };
    window.addEventListener("keydown", onKey);
    return () => window.removeEventListener("keydown", onKey);
  }, [focused, items.length]);

  if (error) {
    return (
      <div className="rounded bg-danger-bg border border-danger/40 px-6 py-5 text-danger text-center">
        {error.message}
      </div>
    );
  }

  if (isPending) {
    return (
      <div className="space-y-3">
        {[...Array(3)].map((_, i) => <Skeleton key={i} className="h-40 w-full" />)}
      </div>
    );
  }

  if (!items.length) {
    return (
      <div className="text-center text-ink-muted py-16">
        <p className="text-lg">אין הלכות הממתינות לאישור.</p>
        <p className="text-sm mt-2">העלה פסיקה חדשה — ההלכות שיחולצו ממנה יופיעו כאן.</p>
      </div>
    );
  }

  return (
    <div className="space-y-4">
      <div className="flex items-center gap-3 text-sm text-ink-muted">
        <span>
          <span className="text-navy font-semibold">{items.length}</span> ממתינות לאישור
        </span>
        <span className="me-auto text-[0.72rem]">
          ניווט: <kbd className="bg-rule-soft px-1.5 rounded">J</kbd>/<kbd className="bg-rule-soft px-1.5 rounded">K</kbd>
          {" "}· אישור: <kbd className="bg-rule-soft px-1.5 rounded">A</kbd>
          {" "}· דחייה: <kbd className="bg-rule-soft px-1.5 rounded">R</kbd>
          {" "}· עריכה: <kbd className="bg-rule-soft px-1.5 rounded">E</kbd>
        </span>
        <Button
          size="sm" variant="ghost"
          onClick={() => setFocusIdx((i) => Math.max(0, i - 1))}
          disabled={focusIdx === 0}>
          <ChevronUp className="w-4 h-4" />
        </Button>
        <Button
          size="sm" variant="ghost"
          onClick={() => setFocusIdx((i) => Math.min(items.length - 1, i + 1))}
          disabled={focusIdx >= items.length - 1}>
          <ChevronDown className="w-4 h-4" />
        </Button>
      </div>

      <div className="space-y-4">
        {items.map((h, i) => (
          <HalachaCard
            key={h.id} h={h} focused={i === focusIdx}
            onApprove={() => review(h, "approved")}
            onReject={() => {
              if (window.confirm("לדחות הלכה זו?")) review(h, "rejected");
            }}
            onSave={async (patch) => {
              try {
                await update.mutateAsync({ id: h.id, patch });
                toast.success("נשמר");
              } catch (e) {
                toast.error(e instanceof Error ? e.message : "שגיאה");
              }
            }}
          />
        ))}
      </div>
    </div>
  );
}
