import enum
import uuid

from sqlalchemy import 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 DomainGoalStatus(str, enum.Enum):
    active = "active"
    archived = "archived"
    superseded = "superseded"


class DomainGoal(Base, TimestampMixin):
    __tablename__ = "domain_goals"

    id: Mapped[uuid.UUID] = mapped_column(
        UUID(as_uuid=True), primary_key=True, default=new_uuid
    )
    domain_id: Mapped[uuid.UUID] = mapped_column(
        UUID(as_uuid=True), ForeignKey("domains.id", ondelete="RESTRICT"), nullable=False, index=True
    )
    title: Mapped[str] = mapped_column(String(500), nullable=False)
    description: Mapped[str] = mapped_column(Text, nullable=False)
    status: Mapped[str] = mapped_column(
        String(20), nullable=False, default=DomainGoalStatus.active.value, server_default="active"
    )

    domain: Mapped["Domain"] = relationship(  # noqa: F821
        "Domain", back_populates="goals", lazy="selectin"
    )
    repo_goals: Mapped[list["RepoGoal"]] = relationship(  # noqa: F821
        "RepoGoal", back_populates="domain_goal", lazy="selectin"
    )

    @property
    def domain_slug(self) -> str:
        return self.domain.slug if self.domain is not None else ""
