"use client"; import { use, useRef, useState } from "react"; import Link from "next/link"; import { FileText } from "lucide-react"; import { AppShell } from "@/components/app-shell"; import { Card, CardContent } from "@/components/ui/card"; import { Button } from "@/components/ui/button"; import { Skeleton } from "@/components/ui/skeleton"; import { SubsectionCard } from "@/components/compose/subsection-card"; import { PrecedentsSection } from "@/components/compose/precedents-section"; import { CitationVerificationPanel } from "@/components/compose/citation-verification-panel"; import { DecisionBlocksPanel } from "@/components/cases/decision-blocks-panel"; import { Markdown } from "@/components/ui/markdown"; import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; import { useCase, type CaseStatus } from "@/lib/api/cases"; import { useCaseLearningStatus, type CaseLearningStatus, } from "@/lib/api/learning"; import { useResearchAnalysis } from "@/lib/api/research"; import { useCasePrecedents } from "@/lib/api/precedents"; import { APPEAL_SUBTYPES } from "@/lib/practice-area"; import { DOC_TYPE_LABELS, type DocType } from "@/lib/doc-types"; // ── Case-status → Hebrew label + tone (mockup 03 status chip) ──────────────── const STATUS_CHIP: Record = { new: { label: "חדש", cls: "bg-rule-soft text-ink-muted border-rule" }, processing: { label: "בעיבוד", cls: "bg-info-bg text-info border-info/30" }, documents_ready: { label: "מסמכים מוכנים", cls: "bg-info-bg text-info border-info/30" }, outcome_set: { label: "תוצאה נקבעה", cls: "bg-info-bg text-info border-info/30" }, direction_approved: { label: "כיוון אושר", cls: "bg-info-bg text-info border-info/30" }, qa_review: { label: "בדיקת-איכות", cls: "bg-gold-wash text-gold-deep border-gold/40" }, drafted: { label: "טיוטה", cls: "bg-gold-wash text-gold-deep border-gold/40" }, exported: { label: "יוצא", cls: "bg-success-bg text-success border-success/40" }, reviewed: { label: "נסקר", cls: "bg-success-bg text-success border-success/40" }, final: { label: "סופי", cls: "bg-success-bg text-success border-success/40" }, }; function StatusChip({ status }: { status?: CaseStatus }) { const c = (status && STATUS_CHIP[status]) || { label: "בעריכה", cls: "bg-info-bg text-info border-info/30", }; return ( {c.label} ); } function subtypeLabel(subtype?: string | null): string | null { if (!subtype) return null; return APPEAL_SUBTYPES.find((s) => s.value === subtype)?.label ?? null; } // ── Staged-pipeline indicator text (mockup 03) — derived from the live // learning-status, same source as the drafts-panel LearningStatusBadges. ────── function voiceLearningText(s?: CaseLearningStatus): string { if (!s?.final_uploaded) return "ממתין להעלאת הסופי"; const v = s.voice_learning; if (v.outcome === "succeeded") { const bits = [`${v.lessons_count} לקחים הופקו`]; if (v.lessons_proposed > 0) bits.push(`${v.lessons_proposed} הוצעו לאישור`); return `✓ הושלם · ${bits.join(" · ")}`; } if (v.outcome === "failed") return v.error ? `✗ נכשל — ${v.error}` : "✗ נכשל"; return "ממתין להרצה"; } function halachaExtractionText(s?: CaseLearningStatus): string { if (!s?.final_uploaded) return "ממתין להעלאת הסופי"; const h = s.halacha_extraction; if (!h.enrolled_in_corpus) return h.not_enrolled_reason ?? "לא נכנס לקורפוס-הפסיקה"; switch (h.status) { case "completed": return `✓ הושלם · חולצו ${h.halachot_count} · ${h.approved} אושרו · ${h.rejected} נדחו`; case "processing": return "רץ עכשיו…"; case "pending": case "busy": return "בתור"; case "partial": return `חלקי · חולצו ${h.halachot_count}`; case "failed": case "extraction_failed": return "✗ נכשל"; case "no_chunks": return "אין טקסט לחילוץ"; default: return "ממתין להרצה"; } } function ProseSection({ title, content }: { title: string; content?: string }) { if (!content?.trim()) return null; return (

{title}

); } // ── "השלמה והעברה" rail card — DOCX export, upload, download (all real) ────── function FinishRail({ caseNumber, hasAnalysis, onUploaded, }: { caseNumber: string; hasAnalysis: boolean; onUploaded: () => void; }) { const fileRef = useRef(null); const [uploading, setUploading] = useState(false); const [uploadMsg, setUploadMsg] = useState<{ ok: boolean; text: string } | null>(null); const learning = useCaseLearningStatus(caseNumber); async function handleUpload(file: File) { setUploading(true); setUploadMsg(null); try { const form = new FormData(); form.append("file", file); const res = await fetch(`/api/cases/${caseNumber}/research/analysis/upload`, { method: "PUT", body: form, }); const data = await res.json(); if (!res.ok) { setUploadMsg({ ok: false, text: data.detail || "שגיאה בהעלאה" }); return; } setUploadMsg({ ok: true, text: `הקובץ הועלה — ${data.sections.threshold_claims} טענות סף, ${data.sections.issues} סוגיות`, }); onUploaded(); } catch { setUploadMsg({ ok: false, text: "שגיאת רשת" }); } finally { setUploading(false); if (fileRef.current) fileRef.current.value = ""; } } return (

השלמה והעברה

{ const f = e.target.files?.[0]; if (f) handleUpload(f); }} />
{hasAnalysis && ( )} {hasAnalysis && ( )}
{uploadMsg && (

{uploadMsg.text}

)} {/* mockup 03: stage indicators — informational pointers, not actions */}
הרץ למידת-קול — {voiceLearningText(learning.data)}
הרץ אימות-הלכות — {halachaExtractionText(learning.data)}
); } export default function ComposePage({ params, }: { params: Promise<{ caseNumber: string }>; }) { const { caseNumber } = use(params); const caseQuery = useCase(caseNumber); const analysis = useResearchAnalysis(caseNumber); const precedentsQuery = useCasePrecedents(caseNumber); /* Partition the flat list into scopes so each child renders its own slice * without re-fetching. Done once at the page level. */ const allPrecedents = precedentsQuery.data ?? []; const caseLevelPrecedents = allPrecedents.filter((p) => p.section_id === null); const precedentsBySection = new Map(); for (const p of allPrecedents) { if (p.section_id) { const existing = precedentsBySection.get(p.section_id) ?? []; existing.push(p); precedentsBySection.set(p.section_id, existing); } } const practiceArea = caseQuery.data?.practice_area ?? null; const subtype = subtypeLabel(caseQuery.data?.appeal_subtype); const parties = (() => { const c = caseQuery.data; if (!c) return null; const app = c.appellants?.length ? c.appellants.join(", ") : null; const resp = c.respondents?.length ? c.respondents.join(", ") : null; const out: string[] = []; if (app) out.push(`עוררים: ${app}`); if (resp) out.push(`משיבה: ${resp}`); return out.length ? out.join(" · ") : c.title || null; })(); const documents = caseQuery.data?.documents ?? []; const isNotFound = analysis.error instanceof Error && /404|לא נמצא|טרם בוצע/.test(analysis.error.message); return ( {/* ── Case header band (mockup 03) — parchment strip, full-bleed to the AppShell
edges (which pads px-10 py-10) ── */}
חזרה לדף התיק

ערר {caseNumber}

{subtype && ( {subtype} )} {/* INV-G10: source-of-truth pill — the blocks are the canonical text */} מקור-אמת: בלוקים
{parties &&

{parties}

}
{caseQuery.isPending ? ( ) : ( /* ── Two-column workspace: tabbed main editor + 320px side rail ──── */
{/* MAIN — block editor (default) + chair positions, as tabs (mockup 03) */}
עורך הבלוקים עמדות וטענות אימות פסיקה {/* Tab 1 — the 12-block decision editor (reused DecisionBlocksPanel) */} {/* Tab 3 — citation verification: per-argument support + verify gate (#154) */} {/* Tab 2 — chair positions on the analyst's threshold-claims + issues */} {analysis.isPending ? ( ) : isNotFound ? (

טרם בוצע ניתוח משפטי לתיק זה

לאחר שקובץ analysis-and-research.md ייווצר, תוכלי לערוך כאן את עמדת הוועדה לכל טענת סף וסוגיה.

) : analysis.error ? (

{analysis.error.message}

) : analysis.data ? (
{/* Threshold claims */} {analysis.data.threshold_claims && analysis.data.threshold_claims.length > 0 && (

טענות סף

{analysis.data.threshold_claims.length}
{analysis.data.threshold_claims.map((tc) => ( ))}
)} {/* Issues */} {analysis.data.issues && analysis.data.issues.length > 0 && (

סוגיות להכרעה

{analysis.data.issues.length}
{analysis.data.issues.map((iss) => ( ))}
)} {!analysis.data.threshold_claims?.length && !analysis.data.issues?.length && ( לא נמצאו טענות סף או סוגיות בניתוח זה. )} {/* Background prose — supporting context after the decision points */}

רקע לניתוח

{analysis.data.conclusions?.trim() && (

מסקנות

)}
) : null}
{/* SIDE RAIL — documents · attached precedents · finish-and-transfer */}
)} ); }