"""Add board_repository_links table for board-to-repository associations.

Revision ID: a1b2c3d4e5f7
Revises: f5a2b3c4d5e6
Create Date: 2026-05-19 01:00:00.000000

"""

from __future__ import annotations

import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = "a1b2c3d4e5f7"
down_revision = "f5a2b3c4d5e6"
branch_labels = None
depends_on = None


def upgrade() -> None:
    """Create board_repository_links table."""
    bind = op.get_bind()
    inspector = sa.inspect(bind)

    if not inspector.has_table("board_repository_links"):
        op.create_table(
            "board_repository_links",
            sa.Column("id", sa.Uuid(), nullable=False),
            sa.Column("board_id", sa.Uuid(), nullable=False),
            sa.Column("repository_id", sa.Uuid(), nullable=False),
            sa.Column("organization_id", sa.Uuid(), nullable=False),
            sa.Column("created_at", sa.DateTime(), nullable=False),
            sa.PrimaryKeyConstraint("id"),
            sa.ForeignKeyConstraint(["board_id"], ["boards.id"]),
            sa.ForeignKeyConstraint(["repository_id"], ["forgejo_repositories.id"]),
            sa.ForeignKeyConstraint(["organization_id"], ["organizations.id"]),
        )
        op.create_index(
            "ix_board_repository_links_board_id",
            "board_repository_links",
            ["board_id"],
        )
        op.create_index(
            "ix_board_repository_links_repository_id",
            "board_repository_links",
            ["repository_id"],
        )
        op.create_index(
            "ix_board_repository_links_org_id",
            "board_repository_links",
            ["organization_id"],
        )
        op.create_unique_constraint(
            "uq_board_repository_links_board_repository",
            "board_repository_links",
            ["board_id", "repository_id"],
        )


def downgrade() -> None:
    """Drop board_repository_links table."""
    op.drop_table("board_repository_links")
