"""add users table

Revision ID: 510018b229c3
Revises: 9e9fd5dc5c44
Create Date: 2026-04-21 23:55:42.574583

"""

from __future__ import annotations

from collections.abc import Sequence

import sqlalchemy as sa
import sqlmodel

from alembic import op

# revision identifiers, used by Alembic.
revision: str = "510018b229c3"
down_revision: str | None = "9e9fd5dc5c44"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "users",
        sa.Column("id", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("display_name", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("email", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("slack_team_id", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("slack_user_id", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("role", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("access_level", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("pool", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("is_operator", sa.Boolean(), nullable=False),
        sa.Column("password_hash", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("active", sa.Boolean(), nullable=False),
        sa.Column("last_assigned_at", sa.DateTime(), nullable=True),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), nullable=False),
        sa.PrimaryKeyConstraint("id"),
    )
    with op.batch_alter_table("users", schema=None) as batch_op:
        batch_op.create_index(
            "ix_users_email_unique",
            ["email"],
            unique=True,
            sqlite_where=sa.text("email IS NOT NULL"),
            postgresql_where=sa.text("email IS NOT NULL"),
        )
        batch_op.create_index(
            "ix_users_slack_unique",
            ["slack_team_id", "slack_user_id"],
            unique=True,
            sqlite_where=sa.text("slack_user_id IS NOT NULL"),
            postgresql_where=sa.text("slack_user_id IS NOT NULL"),
        )

    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table("users", schema=None) as batch_op:
        batch_op.drop_index(
            "ix_users_slack_unique",
            sqlite_where=sa.text("slack_user_id IS NOT NULL"),
            postgresql_where=sa.text("slack_user_id IS NOT NULL"),
        )
        batch_op.drop_index(
            "ix_users_email_unique",
            sqlite_where=sa.text("email IS NOT NULL"),
            postgresql_where=sa.text("email IS NOT NULL"),
        )

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