import uuid
from datetime import datetime

from pydantic import AliasChoices, BaseModel, ConfigDict, Field

from api.models.technical_debt import TDStatus

VALID_SEVERITIES = {"low", "medium", "high", "critical"}


class TDNoteCreate(BaseModel):
    step: str
    author: str | None = None
    content: str


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

    id: uuid.UUID
    td_id: uuid.UUID
    step: str
    author: str | None = None
    content: str
    created_at: datetime


class TDCreate(BaseModel):
    td_id: str | None = None
    domain: str  # slug; router resolves to domain_id FK
    title: str
    description: str | None = None
    location: str | None = None
    debt_type: str = "other"
    severity: str = "medium"
    status: TDStatus = TDStatus.open
    topic_id: uuid.UUID | None = None
    workplan_id: uuid.UUID | None = Field(
        default=None,
        validation_alias=AliasChoices("workplan_id", "workstream_id"),
    )


class TDUpdate(BaseModel):
    title: str | None = None
    description: str | None = None
    location: str | None = None
    debt_type: str | None = None
    severity: str | None = None
    status: TDStatus | None = None
    workplan_id: uuid.UUID | None = Field(
        default=None,
        validation_alias=AliasChoices("workplan_id", "workstream_id"),
    )


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

    id: uuid.UUID
    td_id: str | None = None
    domain_slug: str  # derived from domain relationship
    title: str
    description: str | None = None
    location: str | None = None
    debt_type: str
    severity: str
    status: TDStatus
    topic_id: uuid.UUID | None = None
    workplan_id: uuid.UUID | None = None
    created_at: datetime

    @property
    def workstream_id(self) -> uuid.UUID | None:
        return self.workplan_id
    updated_at: datetime
    notes: list[TDNoteRead] = []
