"""Calendar subscription tokens table

Revision ID: 005
Revises: 004
Create Date: 2025-02-19

"""

from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa

revision: str = "005"
down_revision: Union[str, None] = "004"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    op.create_table(
        "calendar_subscription_tokens",
        sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
        sa.Column("user_id", sa.Integer(), nullable=False),
        sa.Column("token_hash", sa.Text(), nullable=False),
        sa.Column("created_at", sa.Text(), nullable=False),
        sa.ForeignKeyConstraint(["user_id"], ["users.id"]),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint(
            "token_hash", name="uq_calendar_subscription_tokens_token_hash"
        ),
    )
    op.create_index(
        "ix_calendar_subscription_tokens_token_hash",
        "calendar_subscription_tokens",
        ["token_hash"],
        unique=True,
    )


def downgrade() -> None:
    op.drop_index(
        "ix_calendar_subscription_tokens_token_hash",
        table_name="calendar_subscription_tokens",
    )
    op.drop_table("calendar_subscription_tokens")
