from __future__ import annotations

from datetime import datetime
from uuid import UUID

from pydantic import BaseModel, ConfigDict, Field


# ── Decision ─────────────────────────────────────────────────────


class DecisionStepCreate(BaseModel):
    """Payload for creating a step within a decision process."""

    step_order: int = Field(..., ge=0)
    step_type: str = Field(..., max_length=32, description="qualification, review, vote, execution, reporting")
    title: str | None = Field(default=None, max_length=256)
    description: str | None = None


class DecisionStepOut(BaseModel):
    """Full decision step representation."""

    model_config = ConfigDict(from_attributes=True)

    id: UUID
    decision_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


class DecisionCreate(BaseModel):
    """Payload for creating a new decision."""

    title: str = Field(..., min_length=1, max_length=256)
    description: str | None = None
    context: str | None = None
    decision_type: str = Field(..., max_length=64, description="runtime_upgrade, document_change, mandate_vote, custom")
    voting_protocol_id: UUID | None = None


class DecisionUpdate(BaseModel):
    """Partial update for a decision."""

    title: str | None = Field(default=None, max_length=256)
    description: str | None = None
    status: str | None = Field(default=None, max_length=32)
    voting_protocol_id: UUID | None = None


class DecisionOut(BaseModel):
    """Full decision representation returned by the API."""

    model_config = ConfigDict(from_attributes=True)

    id: UUID
    title: str
    description: str | None = None
    context: str | None = None
    decision_type: str
    status: str
    voting_protocol_id: UUID | None = None
    created_by_id: UUID | None = None
    created_at: datetime
    updated_at: datetime
    steps: list[DecisionStepOut] = Field(default_factory=list)


class DecisionAdvanceOut(BaseModel):
    """Output after advancing a decision through its workflow."""

    model_config = ConfigDict(from_attributes=True)

    id: UUID
    title: str
    description: str | None = None
    context: str | None = None
    decision_type: str
    status: str
    voting_protocol_id: UUID | None = None
    created_by_id: UUID | None = None
    created_at: datetime
    updated_at: datetime
    steps: list[DecisionStepOut] = Field(default_factory=list)
    message: str = Field(..., description="Message decrivant l'avancement effectue")
