"""Add Campaign

Revision ID: d2efdf94bfa9
Revises: 301eb03ce91c
Create Date: 2025-04-05 13:36:03.178558

"""

from datetime import UTC, datetime, timezone
from uuid import uuid4

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

# Polar Custom Imports

# revision identifiers, used by Alembic.
revision = "d2efdf94bfa9"
down_revision = "301eb03ce91c"
branch_labels: tuple[str] | None = None
depends_on: tuple[str] | None = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    create_campaign_table()
    add_account_campaign_column()


def create_campaign_table() -> None:
    op.create_table(
        "campaigns",
        sa.Column("code", sa.String(), nullable=False),
        sa.Column("name", sa.String(), nullable=False),
        sa.Column("starts_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.Column("ends_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.Column("max_redemptions", sa.Integer(), nullable=True),
        sa.Column("max_user_redemptions", sa.Integer(), nullable=True),
        sa.Column("fee_percent", sa.Integer(), nullable=True),
        sa.Column("fee_fixed", sa.Integer(), nullable=True),
        sa.Column(
            "user_metadata", postgresql.JSONB(astext_type=sa.Text()), 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.PrimaryKeyConstraint("id", name=op.f("campaigns_pkey")),
    )
    op.create_index(op.f("ix_campaigns_code"), "campaigns", ["code"], unique=True)
    op.create_index(
        op.f("ix_campaigns_created_at"), "campaigns", ["created_at"], unique=False
    )
    op.create_index(
        op.f("ix_campaigns_deleted_at"), "campaigns", ["deleted_at"], unique=False
    )
    op.create_index(
        op.f("ix_campaigns_modified_at"), "campaigns", ["modified_at"], unique=False
    )


def add_account_campaign_column() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column("accounts", sa.Column("campaign_id", sa.Uuid(), nullable=True))
    op.create_index(
        op.f("ix_accounts_campaign_id"), "accounts", ["campaign_id"], unique=False
    )
    op.create_foreign_key(
        op.f("accounts_campaign_id_fkey"),
        "accounts",
        "campaigns",
        ["campaign_id"],
        ["id"],
        ondelete="set null",
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    drop_campaign_table()
    drop_account_campaign_column()


def drop_campaign_table() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(op.f("ix_campaigns_modified_at"), table_name="campaigns")
    op.drop_index(op.f("ix_campaigns_deleted_at"), table_name="campaigns")
    op.drop_index(op.f("ix_campaigns_created_at"), table_name="campaigns")
    op.drop_index(op.f("ix_campaigns_code"), table_name="campaigns")
    op.drop_table("campaigns")
    # ### end Alembic commands ###


def drop_account_campaign_column() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint(
        op.f("accounts_campaign_id_fkey"), "accounts", type_="foreignkey"
    )
    op.drop_index(op.f("ix_accounts_campaign_id"), table_name="accounts")
    op.drop_column("accounts", "campaign_id")
    # ### end Alembic commands ###
