"""Add 'rewarded' to the advertisement_placement enum.

Rewarded advertisements become another placement within the existing Advertisement
module (no new model/type). On MySQL the ENUM column must be widened to accept the
new value; SQLite stores enums as VARCHAR (created fresh via metadata), so no
change is needed there.

Revision ID: f4a6c8b1d3e7
Revises: c3d5e7f9a2b4
Create Date: 2026-07-04

"""

from alembic import op

# revision identifiers, used by Alembic.
revision = "f4a6c8b1d3e7"
down_revision = "c3d5e7f9a2b4"
branch_labels = None
depends_on = None

_WITH_REWARDED = "ENUM('landing','marketplace','chat','sidebar','rewarded')"
_WITHOUT_REWARDED = "ENUM('landing','marketplace','chat','sidebar')"


def upgrade():
    bind = op.get_bind()
    if bind.dialect.name == "mysql":
        op.execute(
            f"ALTER TABLE advertisements MODIFY COLUMN placement {_WITH_REWARDED} NOT NULL"
        )


def downgrade():
    bind = op.get_bind()
    if bind.dialect.name == "mysql":
        # Move any rewarded ads back to a valid value before narrowing the enum.
        op.execute(
            "UPDATE advertisements SET placement='chat' WHERE placement='rewarded'"
        )
        op.execute(
            f"ALTER TABLE advertisements MODIFY COLUMN placement {_WITHOUT_REWARDED} NOT NULL"
        )
