import uuid
from datetime import datetime

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

from hub_core.schemas.capability import (
    CapabilityRequestDispute,
    CapabilityRequestStatusPatch,
    CatalogCreate,
    CatalogPatch,
    CatalogRead,
)


# ---------------------------------------------------------------------------
# Capability Request schemas
# ---------------------------------------------------------------------------

class CapabilityRequestCreate(BaseModel):
    title: str
    description: str | None = None
    capability_type: str
    priority: str = "medium"
    requesting_domain: str  # slug, resolved to domain_id in router
    requesting_agent: str
    requesting_workplan_id: uuid.UUID | None = Field(
        default=None,
        validation_alias=AliasChoices("requesting_workplan_id", "requesting_workstream_id"),
    )
    blocking_task_id: uuid.UUID | None = None


class CapabilityRequestAccept(BaseModel):
    fulfilling_agent: str
    fulfilling_workplan_id: uuid.UUID | None = Field(
        default=None,
        validation_alias=AliasChoices("fulfilling_workplan_id", "fulfilling_workstream_id"),
    )


class CapabilityRequestPatch(BaseModel):
    catalog_entry_id: uuid.UUID | None = None
    priority: str | None = None
    blocking_task_id: uuid.UUID | None = None
    fulfilling_workplan_id: uuid.UUID | None = Field(
        default=None,
        validation_alias=AliasChoices("fulfilling_workplan_id", "fulfilling_workstream_id"),
    )


class CapabilityRequestReroute(BaseModel):
    note: str
    rerouted_by: str
    domain: str | None = None            # slug — used if catalog_entry_id not given
    catalog_entry_id: uuid.UUID | None = None  # preferred: re-derives domain


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

    id: uuid.UUID
    title: str
    description: str | None = None
    capability_type: str
    priority: str
    status: str
    requesting_domain_slug: str
    requesting_agent: str
    requesting_workplan_id: uuid.UUID | None = None
    fulfilling_domain_slug: str | None = None
    fulfilling_agent: str | None = None
    fulfilling_workplan_id: uuid.UUID | None = None
    blocking_task_id: uuid.UUID | None = None
    catalog_entry_id: uuid.UUID | None = None
    resolution_note: str | None = None
    routing_note: str | None = None
    dispute_reason: str | None = None
    disputed_by: str | None = None
    dispute_suggested_domain: str | None = None
    disputed_at: datetime | None = None
    accepted_at: datetime | None = None
    completed_at: datetime | None = None
    created_at: datetime
    updated_at: datetime

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

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