"""add Forgejo issue to task links

Revision ID: d4e5f6a7b8c9
Revises: d3f4a5b6c7d8
Create Date: 2026-05-22 00:00:00.000000

"""

from __future__ import annotations

import sqlalchemy as sa
from alembic import op

revision = "d4e5f6a7b8c9"
down_revision = "d3f4a5b6c7d8"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.create_table(
        "forgejo_issue_task_links",
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.Column("organization_id", sa.Uuid(), nullable=False),
        sa.Column("board_id", sa.Uuid(), nullable=False),
        sa.Column("issue_id", sa.Uuid(), nullable=False),
        sa.Column("task_id", sa.Uuid(), nullable=False),
        sa.Column("created_by_user_id", sa.Uuid(), nullable=True),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.ForeignKeyConstraint(["board_id"], ["boards.id"]),
        sa.ForeignKeyConstraint(["created_by_user_id"], ["users.id"]),
        sa.ForeignKeyConstraint(["issue_id"], ["forgejo_issues.id"]),
        sa.ForeignKeyConstraint(["organization_id"], ["organizations.id"]),
        sa.ForeignKeyConstraint(["task_id"], ["tasks.id"]),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("board_id", "issue_id", name="uq_forgejo_issue_task_links_board_issue"),
        sa.UniqueConstraint("task_id", name="uq_forgejo_issue_task_links_task"),
    )
    op.create_index(
        op.f("ix_forgejo_issue_task_links_board_id"),
        "forgejo_issue_task_links",
        ["board_id"],
        unique=False,
    )
    op.create_index(
        op.f("ix_forgejo_issue_task_links_created_by_user_id"),
        "forgejo_issue_task_links",
        ["created_by_user_id"],
        unique=False,
    )
    op.create_index(
        op.f("ix_forgejo_issue_task_links_issue_id"),
        "forgejo_issue_task_links",
        ["issue_id"],
        unique=False,
    )
    op.create_index(
        op.f("ix_forgejo_issue_task_links_organization_id"),
        "forgejo_issue_task_links",
        ["organization_id"],
        unique=False,
    )
    op.create_index(
        op.f("ix_forgejo_issue_task_links_task_id"),
        "forgejo_issue_task_links",
        ["task_id"],
        unique=False,
    )


def downgrade() -> None:
    op.drop_index(op.f("ix_forgejo_issue_task_links_task_id"), table_name="forgejo_issue_task_links")
    op.drop_index(
        op.f("ix_forgejo_issue_task_links_organization_id"),
        table_name="forgejo_issue_task_links",
    )
    op.drop_index(op.f("ix_forgejo_issue_task_links_issue_id"), table_name="forgejo_issue_task_links")
    op.drop_index(
        op.f("ix_forgejo_issue_task_links_created_by_user_id"),
        table_name="forgejo_issue_task_links",
    )
    op.drop_index(op.f("ix_forgejo_issue_task_links_board_id"), table_name="forgejo_issue_task_links")
    op.drop_table("forgejo_issue_task_links")

