"""
Work Item Tag junction table for many-to-many relationship.

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

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

from .base import Base


class WorkItemTag(Base):
    """
    Junction table linking work items to tags.

    Implements many-to-many relationship between work_items and tags tables.
    Allows work items to be tagged with multiple categories for filtering
    and organization.

    Attributes:
        work_item_id: Foreign key to work_items table
        tag_id: Foreign key to tags table
    """

    __tablename__ = "work_item_tags"

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

    # Table constraints and indexes
    __table_args__ = (
        PrimaryKeyConstraint("work_item_id", "tag_id"),
        Index("idx_wit_work_item", "work_item_id"),
        Index("idx_wit_tag", "tag_id"),
    )

    # Relationships
    # work_item = relationship("WorkItem", back_populates="tags")
    # tag = relationship("Tag", back_populates="work_items")

    def __repr__(self) -> str:
        """String representation of the work item tag relationship."""
        return f"<WorkItemTag(work_item_id={self.work_item_id!r}, tag_id={self.tag_id!r})>"
