import uuid
from datetime import datetime, timezone
from typing import TYPE_CHECKING, Optional
from sqlalchemy import String, DateTime, ForeignKey, Integer, UniqueConstraint, Table, Column
from sqlalchemy.orm import Mapped, mapped_column, relationship
from sqlalchemy.dialects.postgresql import UUID
from app.core.database import Base

if TYPE_CHECKING:
    from app.models.tree import Tree
    from app.models.user import User


# Junction table for folder-tree many-to-many relationship
user_folder_trees = Table(
    'user_folder_trees',
    Base.metadata,
    Column('folder_id', UUID(as_uuid=True), ForeignKey('user_folders.id', ondelete='CASCADE'), primary_key=True),
    Column('tree_id', UUID(as_uuid=True), ForeignKey('trees.id', ondelete='CASCADE'), primary_key=True),
    Column('added_at', DateTime(timezone=True), nullable=False, default=lambda: datetime.now(timezone.utc)),
    Column('display_order', Integer, nullable=False, default=0)
)


class UserFolder(Base):
    """User-specific folders for organizing trees.

    - Each folder belongs to a single user
    - Trees can be in multiple folders (like labels/tags)
    - Folders are purely organizational - they don't affect tree visibility or permissions
    """
    __tablename__ = "user_folders"
    __table_args__ = (
        # Allow same name under different parents
        UniqueConstraint('user_id', 'name', 'parent_id', name='uq_user_folders_user_name_parent'),
    )

    id: Mapped[uuid.UUID] = mapped_column(
        UUID(as_uuid=True),
        primary_key=True,
        default=uuid.uuid4
    )
    user_id: Mapped[uuid.UUID] = mapped_column(
        UUID(as_uuid=True),
        ForeignKey("users.id", ondelete="CASCADE"),
        nullable=False,
        index=True
    )
    name: Mapped[str] = mapped_column(String(100), nullable=False)
    color: Mapped[str] = mapped_column(String(7), nullable=False, default="#6366f1")
    icon: Mapped[str] = mapped_column(String(50), nullable=False, default="folder")
    display_order: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
    parent_id: Mapped[Optional[uuid.UUID]] = mapped_column(
        UUID(as_uuid=True),
        ForeignKey("user_folders.id", ondelete="CASCADE"),
        nullable=True,
        index=True
    )
    created_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True),
        default=lambda: datetime.now(timezone.utc)
    )
    updated_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True),
        default=lambda: datetime.now(timezone.utc),
        onupdate=lambda: datetime.now(timezone.utc)
    )

    # Relationships
    user: Mapped["User"] = relationship("User", back_populates="folders")
    trees: Mapped[list["Tree"]] = relationship(
        "Tree",
        secondary=user_folder_trees,
        back_populates="folders"
    )
    # Self-referential relationships for folder hierarchy
    parent: Mapped[Optional["UserFolder"]] = relationship(
        "UserFolder",
        remote_side="UserFolder.id",
        back_populates="children",
        foreign_keys=[parent_id]
    )
    children: Mapped[list["UserFolder"]] = relationship(
        "UserFolder",
        back_populates="parent",
        cascade="all, delete-orphan",
        foreign_keys="UserFolder.parent_id"
    )

    @property
    def tree_count(self) -> int:
        """Returns the number of trees in this folder."""
        return len(self.trees) if self.trees else 0
