"""Board-to-Forgejo-repository link model for board-specific issue filtering."""

from __future__ import annotations

from datetime import datetime
from uuid import UUID, uuid4

from sqlalchemy import UniqueConstraint
from sqlmodel import Field

from app.core.time import utcnow
from app.models.tenancy import TenantScoped

RUNTIME_ANNOTATION_TYPES = (datetime,)


class BoardRepositoryLink(TenantScoped, table=True):
    """Link between a board and a Forgejo-tracked repository."""

    __tablename__ = "board_repository_links"  # pyright: ignore[reportAssignmentType]
    __table_args__ = (
        UniqueConstraint(
            "board_id",
            "repository_id",
            name="uq_board_repository_links_board_repository",
        ),
    )

    id: UUID = Field(default_factory=uuid4, primary_key=True)
    board_id: UUID = Field(foreign_key="boards.id", index=True)
    repository_id: UUID = Field(foreign_key="forgejo_repositories.id", index=True)
    organization_id: UUID = Field(foreign_key="organizations.id", index=True)
    created_at: datetime = Field(default_factory=utcnow)
