"""add platform and make token unique for devices

Revision ID: 4015480760bd
Revises: f2941d6cfae7
Create Date: 2025-04-06 21:47:42.184750

"""

import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import postgresql

# Polar Custom Imports

# revision identifiers, used by Alembic.
revision = "4015480760bd"
down_revision = "f2941d6cfae7"
branch_labels: tuple[str] | None = None
depends_on: tuple[str] | None = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "notification_recipients",
        sa.Column("user_id", sa.Uuid(), nullable=False),
        sa.Column("platform", sa.String(), nullable=False),
        sa.Column("expo_push_token", sa.String(), nullable=False),
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.Column("created_at", sa.TIMESTAMP(timezone=True), nullable=False),
        sa.Column("modified_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.Column("deleted_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.ForeignKeyConstraint(
            ["user_id"],
            ["users.id"],
            name=op.f("notification_recipients_user_id_fkey"),
            ondelete="cascade",
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("notification_recipients_pkey")),
    )
    op.create_index(
        op.f("ix_notification_recipients_created_at"),
        "notification_recipients",
        ["created_at"],
        unique=False,
    )
    op.create_index(
        op.f("ix_notification_recipients_deleted_at"),
        "notification_recipients",
        ["deleted_at"],
        unique=False,
    )
    op.create_index(
        op.f("ix_notification_recipients_expo_push_token"),
        "notification_recipients",
        ["expo_push_token"],
        unique=True,
    )
    op.create_index(
        op.f("ix_notification_recipients_modified_at"),
        "notification_recipients",
        ["modified_at"],
        unique=False,
    )
    op.drop_index("ix_devices_created_at", table_name="devices")
    op.drop_index("ix_devices_deleted_at", table_name="devices")
    op.drop_index("ix_devices_expo_push_token", table_name="devices")
    op.drop_index("ix_devices_modified_at", table_name="devices")
    op.drop_table("devices")
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "devices",
        sa.Column("user_id", sa.UUID(), autoincrement=False, nullable=False),
        sa.Column("platform", sa.VARCHAR(), autoincrement=False, nullable=False),
        sa.Column("expo_push_token", sa.VARCHAR(), autoincrement=False, nullable=False),
        sa.Column("id", sa.UUID(), autoincrement=False, nullable=False),
        sa.Column(
            "created_at",
            postgresql.TIMESTAMP(timezone=True),
            autoincrement=False,
            nullable=False,
        ),
        sa.Column(
            "modified_at",
            postgresql.TIMESTAMP(timezone=True),
            autoincrement=False,
            nullable=True,
        ),
        sa.Column(
            "deleted_at",
            postgresql.TIMESTAMP(timezone=True),
            autoincrement=False,
            nullable=True,
        ),
        sa.ForeignKeyConstraint(
            ["user_id"], ["users.id"], name="devices_user_id_fkey", ondelete="CASCADE"
        ),
        sa.PrimaryKeyConstraint("id", name="devices_pkey"),
    )
    op.create_index("ix_devices_modified_at", "devices", ["modified_at"], unique=False)
    op.create_index(
        "ix_devices_expo_push_token", "devices", ["expo_push_token"], unique=True
    )
    op.create_index("ix_devices_deleted_at", "devices", ["deleted_at"], unique=False)
    op.create_index("ix_devices_created_at", "devices", ["created_at"], unique=False)
    op.drop_index(
        op.f("ix_notification_recipients_modified_at"),
        table_name="notification_recipients",
    )
    op.drop_index(
        op.f("ix_notification_recipients_expo_push_token"),
        table_name="notification_recipients",
    )
    op.drop_index(
        op.f("ix_notification_recipients_deleted_at"),
        table_name="notification_recipients",
    )
    op.drop_index(
        op.f("ix_notification_recipients_created_at"),
        table_name="notification_recipients",
    )
    op.drop_table("notification_recipients")
    # ### end Alembic commands ###
