"""Drop IssueReference and IssueDependency

Revision ID: e9d8922faba6
Revises: dc0951eede55
Create Date: 2024-10-09 15:14:48.755794

"""

import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import postgresql

# Polar Custom Imports

# revision identifiers, used by Alembic.
revision = "e9d8922faba6"
down_revision = "dc0951eede55"
branch_labels: tuple[str] | None = None
depends_on: tuple[str] | None = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index("ix_issue_dependencies_created_at", table_name="issue_dependencies")
    op.drop_index("ix_issue_dependencies_deleted_at", table_name="issue_dependencies")
    op.drop_index("ix_issue_dependencies_modified_at", table_name="issue_dependencies")
    op.drop_table("issue_dependencies")
    op.drop_index("ix_issue_references_created_at", table_name="issue_references")
    op.drop_index("ix_issue_references_deleted_at", table_name="issue_references")
    op.drop_index("ix_issue_references_issue_id", table_name="issue_references")
    op.drop_index("ix_issue_references_modified_at", table_name="issue_references")
    op.drop_table("issue_references")
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "issue_references",
        sa.Column("issue_id", sa.UUID(), autoincrement=False, nullable=False),
        sa.Column("reference_type", sa.VARCHAR(), autoincrement=False, nullable=False),
        sa.Column("external_id", sa.VARCHAR(), autoincrement=False, nullable=False),
        sa.Column("pull_request_id", sa.UUID(), autoincrement=False, nullable=True),
        sa.Column(
            "external_source",
            postgresql.JSONB(astext_type=sa.Text()),
            autoincrement=False,
            nullable=True,
        ),
        sa.Column(
            "created_at",
            postgresql.TIMESTAMP(timezone=True),
            autoincrement=False,
            nullable=False,
        ),
        sa.Column(
            "modified_at",
            postgresql.TIMESTAMP(timezone=True),
            autoincrement=False,
            nullable=True,
        ),
        sa.Column(
            "deleted_at",
            postgresql.TIMESTAMP(timezone=True),
            autoincrement=False,
            nullable=True,
        ),
        sa.ForeignKeyConstraint(
            ["issue_id"], ["issues.id"], name="issue_references_issue_id_fkey"
        ),
        sa.ForeignKeyConstraint(
            ["pull_request_id"],
            ["pull_requests.id"],
            name="issue_references_pull_request_id_fkey",
        ),
        sa.PrimaryKeyConstraint(
            "issue_id", "reference_type", "external_id", name="issue_references_pkey"
        ),
    )
    op.create_index(
        "ix_issue_references_modified_at",
        "issue_references",
        ["modified_at"],
        unique=False,
    )
    op.create_index(
        "ix_issue_references_issue_id", "issue_references", ["issue_id"], unique=False
    )
    op.create_index(
        "ix_issue_references_deleted_at",
        "issue_references",
        ["deleted_at"],
        unique=False,
    )
    op.create_index(
        "ix_issue_references_created_at",
        "issue_references",
        ["created_at"],
        unique=False,
    )
    op.create_table(
        "issue_dependencies",
        sa.Column("organization_id", sa.UUID(), autoincrement=False, nullable=False),
        sa.Column("repository_id", sa.UUID(), autoincrement=False, nullable=False),
        sa.Column("dependent_issue_id", sa.UUID(), autoincrement=False, nullable=False),
        sa.Column(
            "dependency_issue_id", sa.UUID(), autoincrement=False, nullable=False
        ),
        sa.Column(
            "created_at",
            postgresql.TIMESTAMP(timezone=True),
            autoincrement=False,
            nullable=False,
        ),
        sa.Column(
            "modified_at",
            postgresql.TIMESTAMP(timezone=True),
            autoincrement=False,
            nullable=True,
        ),
        sa.Column(
            "deleted_at",
            postgresql.TIMESTAMP(timezone=True),
            autoincrement=False,
            nullable=True,
        ),
        sa.ForeignKeyConstraint(
            ["dependency_issue_id"],
            ["issues.id"],
            name="issue_dependencies_dependency_issue_id_fkey",
        ),
        sa.ForeignKeyConstraint(
            ["dependent_issue_id"],
            ["issues.id"],
            name="issue_dependencies_dependent_issue_id_fkey",
        ),
        sa.ForeignKeyConstraint(
            ["organization_id"],
            ["external_organizations.id"],
            name="issue_dependencies_organization_id_fkey",
        ),
        sa.ForeignKeyConstraint(
            ["repository_id"],
            ["repositories.id"],
            name="issue_dependencies_repository_id_fkey",
        ),
        sa.PrimaryKeyConstraint(
            "dependent_issue_id", "dependency_issue_id", name="issue_dependencies_pkey"
        ),
    )
    op.create_index(
        "ix_issue_dependencies_modified_at",
        "issue_dependencies",
        ["modified_at"],
        unique=False,
    )
    op.create_index(
        "ix_issue_dependencies_deleted_at",
        "issue_dependencies",
        ["deleted_at"],
        unique=False,
    )
    op.create_index(
        "ix_issue_dependencies_created_at",
        "issue_dependencies",
        ["created_at"],
        unique=False,
    )
    # ### end Alembic commands ###
