"""Add unique constraints that we should have added before

Revision ID: 58b98dacf819
Revises: 54ce3cde350c
Create Date: 2025-04-23 22:55:11.731158

"""

from typing import Sequence, Union

import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision: str = "58b98dacf819"
down_revision: Union[str, None] = "54ce3cde350c"
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! ###
    with op.batch_alter_table("avatar", schema=None) as batch_op:
        batch_op.drop_constraint("avatar_unique_legacy_id", type_="unique")
        batch_op.create_unique_constraint(batch_op.f("uq_avatar_hash"), ["hash"])
        batch_op.create_unique_constraint(
            batch_op.f("uq_avatar_legacy_id"), ["legacy_id"]
        )

    with op.batch_alter_table("bob", schema=None) as batch_op:
        batch_op.create_unique_constraint(batch_op.f("uq_bob_sha_1"), ["sha_1"])
        batch_op.create_unique_constraint(batch_op.f("uq_bob_sha_256"), ["sha_256"])
        batch_op.create_unique_constraint(batch_op.f("uq_bob_sha_512"), ["sha_512"])

    with op.batch_alter_table("contact", schema=None) as batch_op:
        batch_op.create_unique_constraint(
            batch_op.f("uq_contact_user_account_id"), ["user_account_id", "legacy_id"]
        )

    with op.batch_alter_table("contact_sent", schema=None) as batch_op:
        batch_op.create_unique_constraint(
            batch_op.f("uq_contact_sent_contact_id"), ["contact_id", "msg_id"]
        )

    with op.batch_alter_table("hat", schema=None) as batch_op:
        batch_op.create_unique_constraint(batch_op.f("uq_hat_title"), ["title", "uri"])

    with op.batch_alter_table("mam", schema=None) as batch_op:
        batch_op.create_unique_constraint(
            batch_op.f("uq_mam_room_id"), ["room_id", "stanza_id"]
        )

    with op.batch_alter_table("participant", schema=None) as batch_op:
        batch_op.alter_column("resource", existing_type=sa.VARCHAR(), nullable=False)
        batch_op.alter_column("nickname", existing_type=sa.VARCHAR(), nullable=False)
        batch_op.alter_column(
            "nickname_no_illegal", existing_type=sa.VARCHAR(), nullable=False
        )
        batch_op.create_unique_constraint(
            batch_op.f("uq_participant_room_id"), ["room_id", "resource"]
        )

    with op.batch_alter_table("room", schema=None) as batch_op:
        batch_op.alter_column(
            "muc_type", existing_type=sa.VARCHAR(length=21), nullable=False
        )

    with op.batch_alter_table("user_account", schema=None) as batch_op:
        batch_op.create_unique_constraint(batch_op.f("uq_user_account_jid"), ["jid"])

    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table("user_account", schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f("uq_user_account_jid"), type_="unique")

    with op.batch_alter_table("room", schema=None) as batch_op:
        batch_op.alter_column(
            "muc_type", existing_type=sa.VARCHAR(length=21), nullable=True
        )

    with op.batch_alter_table("participant", schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f("uq_participant_room_id"), type_="unique")
        batch_op.alter_column(
            "nickname_no_illegal", existing_type=sa.VARCHAR(), nullable=True
        )
        batch_op.alter_column("nickname", existing_type=sa.VARCHAR(), nullable=True)
        batch_op.alter_column("resource", existing_type=sa.VARCHAR(), nullable=True)

    with op.batch_alter_table("mam", schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f("uq_mam_room_id"), type_="unique")

    with op.batch_alter_table("hat", schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f("uq_hat_title"), type_="unique")

    with op.batch_alter_table("contact_sent", schema=None) as batch_op:
        batch_op.drop_constraint(
            batch_op.f("uq_contact_sent_contact_id"), type_="unique"
        )

    with op.batch_alter_table("contact", schema=None) as batch_op:
        batch_op.drop_constraint(
            batch_op.f("uq_contact_user_account_id"), type_="unique"
        )

    with op.batch_alter_table("bob", schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f("uq_bob_sha_512"), type_="unique")
        batch_op.drop_constraint(batch_op.f("uq_bob_sha_256"), type_="unique")
        batch_op.drop_constraint(batch_op.f("uq_bob_sha_1"), type_="unique")

    with op.batch_alter_table("avatar", schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f("uq_avatar_legacy_id"), type_="unique")
        batch_op.drop_constraint(batch_op.f("uq_avatar_hash"), type_="unique")
        batch_op.create_unique_constraint("avatar_unique_legacy_id", ["legacy_id"])

    # ### end Alembic commands ###
