"use client";

import { useState } from "react";
import { Upload } from "lucide-react";
import { toast } from "sonner";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import {
  Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader,
  DialogTitle, DialogTrigger,
} from "@/components/ui/dialog";
import {
  Select, SelectContent, SelectItem, SelectTrigger, SelectValue,
} from "@/components/ui/select";
import { useUploadDigest, type DigestUploadInput } from "@/lib/api/digests";
import type { PracticeArea } from "@/lib/api/precedent-library";
import { PRACTICE_AREAS } from "@/components/precedents/practice-area";

/**
 * Upload a "כל יום" digest PDF. The endpoint is container-safe: it only
 * stages + extracts text, creating a row with status='pending'. The LLM
 * enrichment (concept/headline/citation + embedding + autolink) runs locally
 * via the MCP drainer ``digest_process_pending`` — so the toast tells the
 * user the digest is queued, not yet searchable.
 */
export function DigestUploadDialog({ trigger }: { trigger?: React.ReactNode } = {}) {
  const [open, setOpen] = useState(false);
  const [file, setFile] = useState<File | null>(null);
  const [yomonNumber, setYomonNumber] = useState("");
  const [digestDate, setDigestDate] = useState("");
  const [practiceArea, setPracticeArea] = useState<PracticeArea>("");
  const upload = useUploadDigest();

  const reset = () => {
    setFile(null);
    setYomonNumber("");
    setDigestDate("");
    setPracticeArea("");
  };

  const onSubmit = (e: React.FormEvent) => {
    e.preventDefault();
    if (!file) {
      toast.error("בחר קובץ יומון (PDF)");
      return;
    }
    const input: DigestUploadInput = { file };
    if (yomonNumber.trim()) input.yomon_number = yomonNumber.trim();
    if (digestDate) input.digest_date = digestDate;
    if (practiceArea) input.practice_area = practiceArea;
    upload.mutate(input, {
      onSuccess: (res) => {
        if (res.status === "exists") {
          toast.info("יומון זהה כבר קיים — לא נוצר כפל");
        } else {
          toast.success(
            "היומון נקלט וממתין לעיבוד מקומי (LLM). הרץ digest_process_pending " +
              "או scripts/ingest_digests_batch.py כדי להשלים חילוץ + חיפוש.",
          );
        }
        reset();
        setOpen(false);
      },
      onError: (err) => {
        toast.error(`שגיאה בהעלאה: ${err.message}`);
      },
    });
  };

  return (
    <Dialog open={open} onOpenChange={setOpen}>
      <DialogTrigger asChild>
        {trigger ?? (
          <Button className="bg-gold text-white hover:bg-gold-deep border-transparent">
            <Upload className="w-4 h-4 me-1" />
            העלאת יומון
          </Button>
        )}
      </DialogTrigger>
      <DialogContent dir="rtl">
        <DialogHeader>
          <DialogTitle>העלאת יומון &quot;כל יום&quot;</DialogTitle>
          <DialogDescription>
            סיכום-עמוד של פסק דין. המערכת תחלץ אוטומטית את תג-המושג, ההלכה, ומראה-המקום
            של הפסק המקורי. היומון משמש כ-radar בלבד — אינו מצוטט בהחלטה.
          </DialogDescription>
        </DialogHeader>

        <form onSubmit={onSubmit} className="space-y-4">
          <div>
            <Label htmlFor="digest-file">קובץ יומון (PDF)</Label>
            <Input
              id="digest-file"
              type="file"
              accept=".pdf,.docx,.doc,.rtf,.txt,.md"
              onChange={(e) => setFile(e.target.files?.[0] ?? null)}
            />
          </div>
          <div className="grid grid-cols-2 gap-3">
            <div>
              <Label htmlFor="digest-num">מספר יומון (אופציונלי)</Label>
              <Input
                id="digest-num"
                value={yomonNumber}
                onChange={(e) => setYomonNumber(e.target.value)}
                placeholder="5163"
                dir="ltr"
              />
            </div>
            <div>
              <Label htmlFor="digest-date">תאריך גיליון (אופציונלי)</Label>
              <Input
                id="digest-date"
                type="date"
                value={digestDate}
                onChange={(e) => setDigestDate(e.target.value)}
              />
            </div>
          </div>
          <div>
            <Label>תחום (אופציונלי — יחולץ אם ריק)</Label>
            <Select
              value={practiceArea || "_auto"}
              onValueChange={(v) => setPracticeArea(v === "_auto" ? "" : (v as PracticeArea))}
            >
              <SelectTrigger>
                <SelectValue />
              </SelectTrigger>
              <SelectContent>
                <SelectItem value="_auto">חילוץ אוטומטי</SelectItem>
                {PRACTICE_AREAS.map((a) => (
                  <SelectItem key={a.value} value={a.value}>
                    {a.label}
                  </SelectItem>
                ))}
              </SelectContent>
            </Select>
          </div>

          <DialogFooter>
            <Button
              type="submit"
              disabled={upload.isPending}
              className="bg-gold text-white hover:bg-gold-deep border-transparent"
            >
              {upload.isPending ? "מעלה…" : "העלה"}
            </Button>
          </DialogFooter>
        </form>
      </DialogContent>
    </Dialog>
  );
}
