import enum
import uuid
from datetime import datetime

from sqlalchemy import CheckConstraint, DateTime, Enum, ForeignKey, String, Text
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.orm import Mapped, mapped_column, relationship

from api.models.base import Base, TimestampMixin, new_uuid


class DecisionType(str, enum.Enum):
    made = "made"
    pending = "pending"


class DecisionStatus(str, enum.Enum):
    open = "open"
    resolved = "resolved"
    escalated = "escalated"
    superseded = "superseded"


class Decision(Base, TimestampMixin):
    __tablename__ = "decisions"
    __table_args__ = (
        CheckConstraint(
            "topic_id IS NOT NULL OR workplan_id IS NOT NULL",
            name="ck_decisions_topic_or_workplan",
        ),
    )

    id: Mapped[uuid.UUID] = mapped_column(
        UUID(as_uuid=True), primary_key=True, default=new_uuid
    )
    topic_id: Mapped[uuid.UUID | None] = mapped_column(
        UUID(as_uuid=True), ForeignKey("topics.id", ondelete="RESTRICT"), nullable=True, index=True
    )
    workplan_id: Mapped[uuid.UUID | None] = mapped_column(
        UUID(as_uuid=True), ForeignKey("workplans.id", ondelete="RESTRICT"), nullable=True, index=True
    )
    title: Mapped[str] = mapped_column(String(255), nullable=False)
    description: Mapped[str | None] = mapped_column(Text, nullable=True)
    decision_type: Mapped[DecisionType] = mapped_column(
        Enum(DecisionType), nullable=False, default=DecisionType.pending
    )
    status: Mapped[DecisionStatus] = mapped_column(
        Enum(DecisionStatus), nullable=False, default=DecisionStatus.open
    )
    rationale: Mapped[str | None] = mapped_column(Text, nullable=True)
    decided_by: Mapped[str | None] = mapped_column(String(100), nullable=True)
    decided_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
    deadline: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
    escalation_note: Mapped[str | None] = mapped_column(Text, nullable=True)
    superseded_by: Mapped[uuid.UUID | None] = mapped_column(
        UUID(as_uuid=True), ForeignKey("decisions.id", ondelete="SET NULL"), nullable=True
    )

    topic: Mapped["Topic | None"] = relationship("Topic", back_populates="decisions")  # noqa: F821
    workplan: Mapped["Workplan | None"] = relationship("Workplan", back_populates="decisions")  # noqa: F821
    progress_events: Mapped[list["ProgressEvent"]] = relationship(  # noqa: F821
        "ProgressEvent", back_populates="decision", lazy="selectin"
    )
