"""
Infrastructure Tag junction table for many-to-many relationship.

This model creates the many-to-many relationship between infrastructure and tags,
allowing flexible categorization and filtering of infrastructure items.
"""

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

from .base import Base


class InfrastructureTag(Base):
    """
    Junction table linking infrastructure to tags.

    Implements many-to-many relationship between infrastructure and tags tables.
    Allows infrastructure items to be tagged with multiple categories for filtering
    and organization (e.g., docker, postgresql, backup-server, production).

    Attributes:
        infrastructure_id: Foreign key to infrastructure table
        tag_id: Foreign key to tags table
    """

    __tablename__ = "infrastructure_tags"

    # Composite primary key
    infrastructure_id: Mapped[str] = mapped_column(
        CHAR(36),
        ForeignKey("infrastructure.id", ondelete="CASCADE"),
        nullable=False,
        doc="Infrastructure item being tagged",
    )
    tag_id: Mapped[str] = mapped_column(
        CHAR(36),
        ForeignKey("tags.id", ondelete="CASCADE"),
        nullable=False,
        doc="Tag applied to the infrastructure",
    )

    # Table constraints and indexes
    __table_args__ = (
        PrimaryKeyConstraint("infrastructure_id", "tag_id"),
        Index("idx_it_infrastructure", "infrastructure_id"),
        Index("idx_it_tag", "tag_id"),
    )

    # Relationships
    # infrastructure = relationship("Infrastructure", back_populates="tags")
    # tag = relationship("Tag", back_populates="infrastructure_items")

    def __repr__(self) -> str:
        """String representation of the infrastructure tag relationship."""
        return f"<InfrastructureTag(infrastructure_id={self.infrastructure_id!r}, tag_id={self.tag_id!r})>"
