from __future__ import annotations

from datetime import datetime
from uuid import UUID

from pydantic import BaseModel, ConfigDict, Field


# ── Mandate Step ─────────────────────────────────────────────────


class MandateStepCreate(BaseModel):
    step_order: int = Field(..., ge=0)
    step_type: str = Field(..., max_length=32)
    title: str | None = Field(default=None, max_length=256)
    description: str | None = None


class MandateStepOut(BaseModel):
    model_config = ConfigDict(from_attributes=True)

    id: UUID
    mandate_id: UUID
    step_order: int
    step_type: str
    title: str | None = None
    description: str | None = None
    status: str
    vote_session_id: UUID | None = None
    outcome: str | None = None
    created_at: datetime


# ── Mandate ──────────────────────────────────────────────────────


class MandateCreate(BaseModel):
    title: str = Field(..., min_length=1, max_length=256)
    origin_id: UUID | None = None
    description: str | None = None
    mandate_type: str = Field(..., max_length=64)
    nomination_mode: str = Field(
        default="postpone",
        description="auto (auto-assign author), collective, postpone",
    )
    decision_id: UUID | None = None
    starts_at: datetime | None = None
    ends_at: datetime | None = None


class MandateUpdate(BaseModel):
    title: str | None = Field(default=None, max_length=256)
    origin_id: UUID | None = None
    description: str | None = None
    mandate_type: str | None = Field(default=None, max_length=64)
    decision_id: UUID | None = None
    starts_at: datetime | None = None
    ends_at: datetime | None = None


class MandateAssignRequest(BaseModel):
    mandatee_id: UUID = Field(..., description="UUID de l'identite Duniter du mandataire")


class MandateOut(BaseModel):
    model_config = ConfigDict(from_attributes=True)

    id: UUID
    title: str
    origin_id: UUID | None = None
    origin_display_name: str | None = None
    description: str | None = None
    mandate_type: str
    status: str
    mandatee_id: UUID | None = None
    mandatee_display_name: str | None = None
    decision_id: UUID | None = None
    starts_at: datetime | None = None
    ends_at: datetime | None = None
    created_at: datetime
    updated_at: datetime
    steps: list[MandateStepOut] = Field(default_factory=list)


class MandateAdvanceOut(MandateOut):
    message: str = Field(..., description="Message decrivant l'avancement effectue")
