import uuid
from datetime import datetime, timezone
from typing import TYPE_CHECKING
from sqlalchemy import String, DateTime
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.user import User
    from app.models.tree import Tree
    from app.models.category import TreeCategory
    from app.models.tag import TreeTag
    from app.models.step_category import StepCategory


class Team(Base):
    __tablename__ = "teams"

    id: Mapped[uuid.UUID] = mapped_column(
        UUID(as_uuid=True),
        primary_key=True,
        default=uuid.uuid4
    )
    name: Mapped[str] = mapped_column(String(255), nullable=False)
    created_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True),
        default=lambda: datetime.now(timezone.utc)
    )

    # Relationships
    users: Mapped[list["User"]] = relationship("User", back_populates="team")
    trees: Mapped[list["Tree"]] = relationship("Tree", back_populates="team")
    categories: Mapped[list["TreeCategory"]] = relationship("TreeCategory", back_populates="team")
    tags: Mapped[list["TreeTag"]] = relationship("TreeTag", back_populates="team")
    step_categories: Mapped[list["StepCategory"]] = relationship("StepCategory", back_populates="team")
