import uuid
from datetime import datetime

from pydantic import AliasChoices, BaseModel, ConfigDict, Field, computed_field

from api.models.contribution import ContributionStatus, ContributionType


class ContributionCreate(BaseModel):
    type: ContributionType
    target_org: str | None = None
    target_repo: str | None = None
    slug: str | None = None
    title: str
    body_path: str | None = None
    related_topic_id: uuid.UUID | None = None
    related_workplan_id: uuid.UUID | None = Field(
        default=None,
        validation_alias=AliasChoices("related_workplan_id", "related_workstream_id"),
    )
    repo_id: uuid.UUID | None = None
    notes: str | None = None


class ContributionStatusPatch(BaseModel):
    status: ContributionStatus
    notes: str | None = None


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

    id: uuid.UUID
    type: ContributionType
    target_org: str | None = None
    target_repo: str | None = None
    slug: str | None = None
    title: str
    status: ContributionStatus
    body_path: str | None = None
    related_topic_id: uuid.UUID | None = None
    related_workplan_id: uuid.UUID | None = None
    repo_id: uuid.UUID | None = None
    submitted_at: datetime | None = None
    resolved_at: datetime | None = None
    notes: str | None = None
    created_at: datetime
    updated_at: datetime

    @computed_field  # type: ignore[prop-decorator]
    @property
    def related_workstream_id(self) -> uuid.UUID | None:
        return self.related_workplan_id
