"""
Session tag junction table for many-to-many relationships.

Associates sessions with tags for categorization and filtering.
"""

from sqlalchemy import CHAR, ForeignKey, Index, PrimaryKeyConstraint
from sqlalchemy.orm import Mapped, mapped_column

from .base import Base


class SessionTag(Base):
    """
    Session tag junction table for many-to-many relationships.

    Links sessions to tags, allowing sessions to have multiple tags
    and tags to be associated with multiple sessions.

    Attributes:
        session_id: Reference to the session
        tag_id: Reference to the tag
    """

    __tablename__ = "session_tags"

    # Composite primary key
    session_id: Mapped[str] = mapped_column(
        CHAR(36),
        ForeignKey("sessions.id", ondelete="CASCADE"),
        nullable=False,
        doc="Reference to the session"
    )

    tag_id: Mapped[str] = mapped_column(
        CHAR(36),
        ForeignKey("tags.id", ondelete="CASCADE"),
        nullable=False,
        doc="Reference to the tag"
    )

    # Table constraints
    __table_args__ = (
        PrimaryKeyConstraint("session_id", "tag_id"),
        Index("idx_st_session", "session_id"),
        Index("idx_st_tag", "tag_id"),
    )

    def __repr__(self) -> str:
        """String representation of the session tag."""
        return f"<SessionTag(session_id='{self.session_id}', tag_id='{self.tag_id}')>"
