"""Add video-creative fields to advertisements

Adds ``media_type`` (image/video), ``media_url`` and ``video_duration`` so a
rewarded advertisement can carry an HTML5 video while remaining fully backward
compatible with existing image/banner ads (``media_type`` defaults to ``image``).

Revision ID: c7e9a1b3d5f8
Revises: b3d5f7a9c1e4
Create Date: 2026-07-06

"""

import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = "c7e9a1b3d5f8"
down_revision = "b3d5f7a9c1e4"
branch_labels = None
depends_on = None

# Stored as VARCHAR of the enum *values* on SQLite; native ENUM on MySQL.
_MEDIA_TYPE = sa.Enum("image", "video", name="advertisement_media_type")


def upgrade():
    with op.batch_alter_table("advertisements", schema=None) as batch_op:
        # Existing rows are image ads; NOT NULL with a server default keeps them valid.
        batch_op.add_column(
            sa.Column("media_type", _MEDIA_TYPE, nullable=False, server_default="image")
        )
        batch_op.add_column(sa.Column("media_url", sa.String(length=500), nullable=True))
        batch_op.add_column(sa.Column("video_duration", sa.Integer(), nullable=True))


def downgrade():
    with op.batch_alter_table("advertisements", schema=None) as batch_op:
        batch_op.drop_column("video_duration")
        batch_op.drop_column("media_url")
        batch_op.drop_column("media_type")
