"""Unify legacy/XMPP id mappings

Revision ID: 0337c90c0b96
Revises: 58b98dacf819
Create Date: 2025-04-25 20:22:47.612652

"""

from typing import Sequence, Union

import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision: str = "0337c90c0b96"
down_revision: Union[str, None] = "58b98dacf819"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "direct_msg",
        sa.Column("foreign_key", sa.Integer(), nullable=False),
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("legacy_id", sa.String(), nullable=False),
        sa.Column("xmpp_id", sa.String(), nullable=False),
        sa.ForeignKeyConstraint(
            ["foreign_key"],
            ["contact.id"],
            name=op.f("fk_direct_msg_foreign_key_contact"),
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_direct_msg")),
    )
    with op.batch_alter_table("direct_msg", schema=None) as batch_op:
        batch_op.create_index(
            "ix_direct_msg_legacy_id", ["legacy_id", "foreign_key"], unique=False
        )

    op.create_table(
        "direct_thread",
        sa.Column("foreign_key", sa.Integer(), nullable=False),
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("legacy_id", sa.String(), nullable=False),
        sa.Column("xmpp_id", sa.String(), nullable=False),
        sa.ForeignKeyConstraint(
            ["foreign_key"],
            ["contact.id"],
            name=op.f("fk_direct_thread_foreign_key_contact"),
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_direct_thread")),
    )
    with op.batch_alter_table("direct_thread", schema=None) as batch_op:
        batch_op.create_index(
            "ix_direct_direct_thread_id", ["legacy_id", "foreign_key"], unique=False
        )

    op.create_table(
        "group_msg",
        sa.Column("foreign_key", sa.Integer(), nullable=False),
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("legacy_id", sa.String(), nullable=False),
        sa.Column("xmpp_id", sa.String(), nullable=False),
        sa.ForeignKeyConstraint(
            ["foreign_key"], ["room.id"], name=op.f("fk_group_msg_foreign_key_room")
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_group_msg")),
    )
    with op.batch_alter_table("group_msg", schema=None) as batch_op:
        batch_op.create_index(
            "ix_group_msg_legacy_id", ["legacy_id", "foreign_key"], unique=False
        )

    op.create_table(
        "group_thread",
        sa.Column("foreign_key", sa.Integer(), nullable=False),
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("legacy_id", sa.String(), nullable=False),
        sa.Column("xmpp_id", sa.String(), nullable=False),
        sa.ForeignKeyConstraint(
            ["foreign_key"], ["room.id"], name=op.f("fk_group_thread_foreign_key_room")
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_group_thread")),
    )
    with op.batch_alter_table("group_thread", schema=None) as batch_op:
        batch_op.create_index(
            "ix_direct_group_thread_id", ["legacy_id", "foreign_key"], unique=False
        )

    with op.batch_alter_table("xmpp_ids_multi", schema=None) as batch_op:
        batch_op.drop_index("legacy_ids_multi_user_account_id_xmpp_id")

    op.drop_table("xmpp_ids_multi")
    with op.batch_alter_table("xmpp_to_legacy_ids", schema=None) as batch_op:
        batch_op.drop_index("xmpp_legacy")

    op.drop_table("xmpp_to_legacy_ids")
    with op.batch_alter_table("legacy_ids_multi", schema=None) as batch_op:
        batch_op.drop_index("legacy_ids_multi_user_account_id_legacy_id")

    op.drop_table("legacy_ids_multi")
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "legacy_ids_multi",
        sa.Column("id", sa.INTEGER(), nullable=False),
        sa.Column("user_account_id", sa.INTEGER(), nullable=False),
        sa.Column("legacy_id", sa.VARCHAR(), nullable=False),
        sa.ForeignKeyConstraint(
            ["user_account_id"],
            ["user_account.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    with op.batch_alter_table("legacy_ids_multi", schema=None) as batch_op:
        batch_op.create_index(
            "legacy_ids_multi_user_account_id_legacy_id",
            ["user_account_id", "legacy_id"],
            unique=1,
        )

    op.create_table(
        "xmpp_to_legacy_ids",
        sa.Column("id", sa.INTEGER(), nullable=False),
        sa.Column("user_account_id", sa.INTEGER(), nullable=False),
        sa.Column("xmpp_id", sa.VARCHAR(), nullable=False),
        sa.Column("legacy_id", sa.VARCHAR(), nullable=False),
        sa.Column("type", sa.VARCHAR(length=10), nullable=False),
        sa.ForeignKeyConstraint(
            ["user_account_id"],
            ["user_account.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    with op.batch_alter_table("xmpp_to_legacy_ids", schema=None) as batch_op:
        batch_op.create_index(
            "xmpp_legacy", ["user_account_id", "xmpp_id", "legacy_id"], unique=1
        )

    op.create_table(
        "xmpp_ids_multi",
        sa.Column("id", sa.INTEGER(), nullable=False),
        sa.Column("user_account_id", sa.INTEGER(), nullable=False),
        sa.Column("xmpp_id", sa.VARCHAR(), nullable=False),
        sa.Column("legacy_ids_multi_id", sa.INTEGER(), nullable=False),
        sa.ForeignKeyConstraint(
            ["legacy_ids_multi_id"],
            ["legacy_ids_multi.id"],
        ),
        sa.ForeignKeyConstraint(
            ["user_account_id"],
            ["user_account.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    with op.batch_alter_table("xmpp_ids_multi", schema=None) as batch_op:
        batch_op.create_index(
            "legacy_ids_multi_user_account_id_xmpp_id",
            ["user_account_id", "xmpp_id"],
            unique=1,
        )

    with op.batch_alter_table("group_thread", schema=None) as batch_op:
        batch_op.drop_index("ix_direct_group_thread_id")

    op.drop_table("group_thread")
    with op.batch_alter_table("group_msg", schema=None) as batch_op:
        batch_op.drop_index("ix_group_msg_legacy_id")

    op.drop_table("group_msg")
    with op.batch_alter_table("direct_thread", schema=None) as batch_op:
        batch_op.drop_index("ix_direct_direct_thread_id")

    op.drop_table("direct_thread")
    with op.batch_alter_table("direct_msg", schema=None) as batch_op:
        batch_op.drop_index("ix_direct_msg_legacy_id")

    op.drop_table("direct_msg")
    # ### end Alembic commands ###
