import uuid
from datetime import datetime

from sqlalchemy import String, Integer, ForeignKey, Index, func, Text
from sqlalchemy import Uuid
from sqlalchemy.orm import Mapped, mapped_column, relationship

from app.database import Base, JSONType


class LifecycleProject(Base):
    __tablename__ = "lifecycle_projects"

    id: Mapped[uuid.UUID] = mapped_column(
        Uuid(as_uuid=True),
        primary_key=True,
        default=uuid.uuid4,
    )
    organization_id: Mapped[uuid.UUID] = mapped_column(
        Uuid(as_uuid=True),
        ForeignKey("organizations.id", ondelete="CASCADE"),
        nullable=False,
    )
    brand_name: Mapped[str] = mapped_column(String(100), nullable=False)
    brand_aliases: Mapped[list] = mapped_column(JSONType, server_default="[]", nullable=False)
    current_stage: Mapped[int] = mapped_column(Integer, server_default="1", nullable=False)
    status: Mapped[str] = mapped_column(String(20), server_default="active", nullable=False)
    created_by: Mapped[uuid.UUID] = mapped_column(
        Uuid(as_uuid=True),
        ForeignKey("users.id", ondelete="SET NULL"),
        nullable=True,
    )
    created_at: Mapped[datetime] = mapped_column(
        server_default=func.now(),
        nullable=False,
    )
    updated_at: Mapped[datetime] = mapped_column(
        server_default=func.now(),
        onupdate=func.now(),
        nullable=False,
    )

    # Relationships
    organization: Mapped["Organization"] = relationship(
        "Organization", back_populates="lifecycle_projects"
    )
    stages: Mapped[list["ProjectStage"]] = relationship(
        "ProjectStage", back_populates="project", cascade="all, delete-orphan"
    )
    creator: Mapped["User"] = relationship(
        "User", foreign_keys=[created_by]
    )

    __table_args__ = (
        Index("idx_lifecycle_projects_organization_id", "organization_id"),
        Index("idx_lifecycle_projects_status", "status"),
        Index("idx_lifecycle_projects_brand_name", "brand_name"),
    )


class ProjectStage(Base):
    __tablename__ = "project_stages"

    id: Mapped[uuid.UUID] = mapped_column(
        Uuid(as_uuid=True),
        primary_key=True,
        default=uuid.uuid4,
    )
    project_id: Mapped[uuid.UUID] = mapped_column(
        Uuid(as_uuid=True),
        ForeignKey("lifecycle_projects.id", ondelete="CASCADE"),
        nullable=False,
    )
    stage_number: Mapped[int] = mapped_column(Integer, nullable=False)
    status: Mapped[str] = mapped_column(String(20), server_default="pending", nullable=False)
    started_at: Mapped[datetime | None] = mapped_column(nullable=True)
    completed_at: Mapped[datetime | None] = mapped_column(nullable=True)
    notes: Mapped[str | None] = mapped_column(Text, nullable=True)
    metrics: Mapped[dict | None] = mapped_column(JSONType, nullable=True)

    # Relationships
    project: Mapped["LifecycleProject"] = relationship(
        "LifecycleProject", back_populates="stages"
    )

    __table_args__ = (
        Index("idx_project_stages_project_id", "project_id"),
        Index("idx_project_stages_status", "status"),
        Index("idx_project_stages_project_stage", "project_id", "stage_number", unique=True),
    )
