"""
Legal document schemas — Rechtliche Texte with versioning + approval.

Phase 1 Step 4: extracted from ``compliance.api.legal_document_routes``.
"""

from datetime import datetime
from typing import Optional

from pydantic import BaseModel


class DocumentCreate(BaseModel):
    type: str
    name: str
    description: Optional[str] = None
    mandatory: bool = False
    tenant_id: Optional[str] = None


class DocumentResponse(BaseModel):
    id: str
    tenant_id: Optional[str]
    type: str
    name: str
    description: Optional[str]
    mandatory: bool
    created_at: datetime
    updated_at: Optional[datetime]


class VersionCreate(BaseModel):
    document_id: str
    version: str
    language: str = "de"
    title: str
    content: str
    summary: Optional[str] = None
    created_by: Optional[str] = None


class VersionUpdate(BaseModel):
    title: Optional[str] = None
    content: Optional[str] = None
    summary: Optional[str] = None
    version: Optional[str] = None
    language: Optional[str] = None


class VersionResponse(BaseModel):
    id: str
    document_id: str
    version: str
    language: str
    title: str
    content: str
    summary: Optional[str]
    status: str
    created_by: Optional[str]
    # Legacy single-approval (4-stage)
    approved_by: Optional[str]
    approved_at: Optional[datetime]
    # 5-stage Trail
    submitted_by: Optional[str] = None
    submitted_at: Optional[datetime] = None
    approved_internal_by: Optional[str] = None
    approved_internal_at: Optional[datetime] = None
    approved_client_by: Optional[str] = None
    approved_client_at: Optional[datetime] = None
    rejection_reason: Optional[str]
    created_at: datetime
    updated_at: Optional[datetime]


class ApprovalHistoryEntry(BaseModel):
    id: str
    version_id: str
    action: str
    approver: Optional[str]
    comment: Optional[str]
    created_at: datetime


class ActionRequest(BaseModel):
    approver: Optional[str] = None
    comment: Optional[str] = None


class UserConsentCreate(BaseModel):
    user_id: str
    document_id: str
    document_version_id: Optional[str] = None
    document_type: str
    consented: bool = True
    ip_address: Optional[str] = None
    user_agent: Optional[str] = None


class CookieCategoryCreate(BaseModel):
    name_de: str
    name_en: Optional[str] = None
    description_de: Optional[str] = None
    description_en: Optional[str] = None
    is_required: bool = False
    sort_order: int = 0


class CookieCategoryUpdate(BaseModel):
    name_de: Optional[str] = None
    name_en: Optional[str] = None
    description_de: Optional[str] = None
    description_en: Optional[str] = None
    is_required: Optional[bool] = None
    sort_order: Optional[int] = None
    is_active: Optional[bool] = None


__all__ = [
    "DocumentCreate",
    "DocumentResponse",
    "VersionCreate",
    "VersionUpdate",
    "VersionResponse",
    "ApprovalHistoryEntry",
    "ActionRequest",
    "UserConsentCreate",
    "CookieCategoryCreate",
    "CookieCategoryUpdate",
]
