"""Ditch hats table

Revision ID: 75a62b74b239
Revises: 0337c90c0b96
Create Date: 2025-05-02 14:24:26.141034

"""

from typing import Sequence, Union

import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision: str = "75a62b74b239"
down_revision: Union[str, None] = "0337c90c0b96"
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.drop_table("participant_hats")
    op.drop_table("hat")
    with op.batch_alter_table("contact", schema=None) as batch_op:
        batch_op.drop_constraint("uq_contact_user_account_id", type_="unique")
        batch_op.create_unique_constraint(
            batch_op.f("uq_contact_user_account_id"), ["user_account_id", "jid"]
        )

    with op.batch_alter_table("participant", schema=None) as batch_op:
        batch_op.add_column(
            sa.Column("hats", sa.JSON(), nullable=False, server_default="[]")
        )

    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table("participant", schema=None) as batch_op:
        batch_op.drop_column("hats")

    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"
        )
        batch_op.create_unique_constraint(
            "uq_contact_user_account_id", ["user_account_id", "legacy_id"]
        )

    op.create_table(
        "hat",
        sa.Column("id", sa.INTEGER(), nullable=False),
        sa.Column("title", sa.VARCHAR(), nullable=False),
        sa.Column("uri", sa.VARCHAR(), nullable=False),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("title", "uri", name="uq_hat_title"),
    )
    op.create_table(
        "participant_hats",
        sa.Column("participant_id", sa.INTEGER(), nullable=False),
        sa.Column("hat_id", sa.INTEGER(), nullable=False),
        sa.ForeignKeyConstraint(
            ["hat_id"],
            ["hat.id"],
        ),
        sa.ForeignKeyConstraint(
            ["participant_id"],
            ["participant.id"],
        ),
        sa.PrimaryKeyConstraint("participant_id", "hat_id"),
    )
    # ### end Alembic commands ###
