"""add forgejo models and issues tables

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

"""

from __future__ import annotations

import sqlalchemy as sa
from alembic import op

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


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

    if not inspector.has_table("forgejo_connections"):
        op.create_table(
            "forgejo_connections",
            sa.Column("id", sa.Uuid(), nullable=False),
            sa.Column("organization_id", sa.Uuid(), nullable=False),
            sa.Column("name", sa.String(), nullable=False),
            sa.Column("base_url", sa.String(), nullable=False),
            sa.Column("token", sa.String(), nullable=True),
            sa.Column("token_last_eight", sa.String(), nullable=True),
            sa.Column("active", sa.Boolean(), nullable=False, server_default=sa.text("true")),
            sa.Column("created_at", sa.DateTime(), nullable=False),
            sa.Column("updated_at", sa.DateTime(), nullable=False),
            sa.PrimaryKeyConstraint("id"),
            sa.ForeignKeyConstraint(["organization_id"], ["organizations.id"]),
        )
        op.create_index("ix_forgejo_connections_org_id", "forgejo_connections", ["organization_id"])

    if not inspector.has_table("forgejo_repositories"):
        op.create_table(
            "forgejo_repositories",
            sa.Column("id", sa.Uuid(), nullable=False),
            sa.Column("organization_id", sa.Uuid(), nullable=False),
            sa.Column("connection_id", sa.Uuid(), nullable=False),
            sa.Column("owner", sa.String(), nullable=False),
            sa.Column("repo", sa.String(), nullable=False),
            sa.Column("display_name", sa.String(), nullable=False, server_default=""),
            sa.Column("default_branch", sa.String(), nullable=False, server_default="main"),
            sa.Column("active", sa.Boolean(), nullable=False, server_default=sa.text("true")),
            sa.Column("last_sync_at", sa.DateTime(), nullable=True),
            sa.Column("last_sync_error", sa.String(), nullable=True),
            sa.Column("created_at", sa.DateTime(), nullable=False),
            sa.Column("updated_at", sa.DateTime(), nullable=False),
            sa.PrimaryKeyConstraint("id"),
            sa.ForeignKeyConstraint(["organization_id"], ["organizations.id"]),
            sa.ForeignKeyConstraint(["connection_id"], ["forgejo_connections.id"]),
        )
        op.create_index("ix_forgejo_repos_org_id", "forgejo_repositories", ["organization_id"])
        op.create_index("ix_forgejo_repos_conn_id", "forgejo_repositories", ["connection_id"])

    if not inspector.has_table("forgejo_issues"):
        op.create_table(
            "forgejo_issues",
            sa.Column("id", sa.Uuid(), nullable=False),
            sa.Column("organization_id", sa.Uuid(), nullable=False),
            sa.Column("repository_id", sa.Uuid(), nullable=False),
            sa.Column("forgejo_issue_number", sa.Integer(), nullable=False),
            sa.Column("title", sa.String(), nullable=False),
            sa.Column("body_preview", sa.String(1000), nullable=True),
            sa.Column("state", sa.String(), nullable=False, server_default="open"),
            sa.Column("is_pull_request", sa.Boolean(), nullable=False, server_default=sa.text("false")),
            sa.Column("labels", sa.JSON(), nullable=True),
            sa.Column("assignees", sa.JSON(), nullable=True),
            sa.Column("author", sa.String(), nullable=False),
            sa.Column("html_url", sa.String(), nullable=False),
            sa.Column("forgejo_created_at", sa.DateTime(), nullable=False),
            sa.Column("forgejo_updated_at", sa.DateTime(), nullable=False),
            sa.Column("forgejo_closed_at", sa.DateTime(), nullable=True),
            sa.Column("last_synced_at", sa.DateTime(), nullable=False),
            sa.Column("created_at", sa.DateTime(), nullable=False),
            sa.Column("updated_at", sa.DateTime(), nullable=False),
            sa.PrimaryKeyConstraint("id"),
            sa.ForeignKeyConstraint(["organization_id"], ["organizations.id"]),
            sa.ForeignKeyConstraint(["repository_id"], ["forgejo_repositories.id"]),
        )
        op.create_index("ix_forgejo_issues_org_id", "forgejo_issues", ["organization_id"])
        op.create_index("ix_forgejo_issues_repo_id", "forgejo_issues", ["repository_id"])
        op.create_index("ix_forgejo_issues_repo_number", "forgejo_issues", ["repository_id", "forgejo_issue_number"], unique=True)


def downgrade() -> None:
    op.drop_table("forgejo_issues")
    op.drop_table("forgejo_repositories")
    op.drop_table("forgejo_connections")