"""Pydantic models for cases, documents, and related entities."""

from __future__ import annotations

import enum
from datetime import date, datetime
from uuid import UUID

from pydantic import BaseModel, Field


# Core case lifecycle. The canonical key set, order, labels, phases and (future)
# per-status actions all live in ONE place — legal_mcp/case_status_model.py —
# which asserts this enum matches it. The frontend mirror is
# web-ui/src/lib/api/case-status.ts (generated against GET /api/status-model).
# The analyst/research intermediate states are first-class canonical statuses
# (the agents set them) — not legacy.
class CaseStatus(str, enum.Enum):
    NEW = "new"
    PROCESSING = "processing"
    DOCUMENTS_READY = "documents_ready"
    ANALYST_VERIFIED = "analyst_verified"
    RESEARCH_COMPLETE = "research_complete"
    OUTCOME_SET = "outcome_set"
    DIRECTION_APPROVED = "direction_approved"
    QA_REVIEW = "qa_review"
    DRAFTED = "drafted"
    EXPORTED = "exported"
    REVIEWED = "reviewed"
    FINAL = "final"


class DocType(str, enum.Enum):
    APPEAL = "appeal"           # כתב ערר
    RESPONSE = "response"       # תשובה / כתב תשובה
    PROTOCOL = "protocol"       # פרוטוקול דיון
    PLAN = "plan"               # תכנית (תב"ע)
    PERMIT = "permit"           # היתר בנייה
    COURT_DECISION = "court_decision"  # פסק דין / החלטת בית משפט
    DECISION = "decision"       # החלטת ועדה
    APPRAISAL = "appraisal"     # שומה / חוות דעת שמאית
    OBJECTION = "objection"     # התנגדות
    EXHIBIT = "exhibit"         # נספח / מסמך תומך
    REFERENCE = "reference"     # מסמך עזר אחר


class SectionType(str, enum.Enum):
    INTRO = "intro"
    FACTS = "facts"
    APPELLANT_CLAIMS = "appellant_claims"
    RESPONDENT_CLAIMS = "respondent_claims"
    LEGAL_ANALYSIS = "legal_analysis"
    CONCLUSION = "conclusion"
    RULING = "ruling"
    OTHER = "other"


class ExtractionStatus(str, enum.Enum):
    PENDING = "pending"
    PROCESSING = "processing"
    COMPLETED = "completed"
    FAILED = "failed"


class CaseCreate(BaseModel):
    case_number: str = Field(description="מספר תיק הערר (לדוגמה: 123-24)")
    title: str = Field(description="כותרת קצרה של הערר")
    appellants: list[str] = Field(default_factory=list, description="שמות העוררים")
    respondents: list[str] = Field(default_factory=list, description="שמות המשיבים")
    subject: str = Field(default="", description="נושא הערר")
    property_address: str = Field(default="", description="כתובת הנכס")
    permit_number: str = Field(default="", description="מספר היתר")
    committee_type: str = Field(default="ועדה מקומית", description="סוג הוועדה")
    hearing_date: date | None = Field(default=None, description="תאריך דיון")
    notes: str = Field(default="", description="הערות")


class CaseInfo(BaseModel):
    id: UUID
    case_number: str
    title: str
    appellants: list[str]
    respondents: list[str]
    subject: str
    property_address: str
    permit_number: str
    committee_type: str
    status: CaseStatus
    hearing_date: date | None
    decision_date: date | None
    tags: list[str]
    notes: str
    created_at: datetime
    updated_at: datetime


class DocumentInfo(BaseModel):
    id: UUID
    case_id: UUID
    doc_type: DocType
    title: str
    file_path: str
    extraction_status: ExtractionStatus
    page_count: int | None
    created_at: datetime


class SearchResult(BaseModel):
    chunk_content: str
    score: float
    case_number: str
    document_title: str
    section_type: str
    page_number: int | None
    document_id: UUID
    case_id: UUID
