"""add page_session table and product view_count

Revision ID: e620bf529ed9
Revises: 05f7eb8cecf1
Create Date: 2026-02-09 14:43:04.019142

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = 'e620bf529ed9'
down_revision = '05f7eb8cecf1'
branch_labels = None
depends_on = None

from make_post_sell.models.meta import UUIDType


def _table_exists(name):
    conn = op.get_bind()
    result = conn.execute(
        sa.text("SELECT name FROM sqlite_master WHERE type='table' AND name=:name"),
        {"name": name},
    )
    return result.fetchone() is not None


def _column_exists(table, column):
    conn = op.get_bind()
    result = conn.execute(sa.text(f"PRAGMA table_info({table})"))
    return any(row[1] == column for row in result.fetchall())


def upgrade():
    if not _table_exists("mps_page_session"):
        op.create_table(
            "mps_page_session",
            sa.Column("id", UUIDType, primary_key=True, index=True),
            sa.Column(
                "shop_id", UUIDType,
                sa.ForeignKey("mps_shop.id"), nullable=False, index=True,
            ),
            sa.Column(
                "product_id", UUIDType,
                sa.ForeignKey("mps_product.id"), nullable=False, index=True,
            ),
            sa.Column("created_timestamp", sa.BigInteger(), nullable=False),
            # Presence
            sa.Column("wall_clock_ms", sa.Integer(), nullable=True),
            sa.Column("visible_ms", sa.Integer(), nullable=True),
            sa.Column("active_ms", sa.Integer(), nullable=True),
            # Scroll
            sa.Column("scroll_depth_max", sa.Float(), nullable=True),
            sa.Column("scroll_direction_changes", sa.Integer(), nullable=True),
            # Media
            sa.Column("media_duration_ms", sa.Integer(), nullable=True),
            sa.Column("media_play_ms", sa.Integer(), nullable=True),
            sa.Column("media_play_count", sa.SmallInteger(), nullable=True),
            sa.Column("media_pause_count", sa.SmallInteger(), nullable=True),
            sa.Column("media_seek_count", sa.SmallInteger(), nullable=True),
            sa.Column("media_seek_back_count", sa.SmallInteger(), nullable=True),
            sa.Column("media_speed", sa.Float(), nullable=True),
            sa.Column("media_completed", sa.Boolean(), nullable=True),
            sa.Column("media_percent_played", sa.Float(), nullable=True),
            # Context
            sa.Column("is_ring_entry", sa.Boolean(), nullable=True),
            sa.Column("ring_position", sa.SmallInteger(), nullable=True),
            sa.Column("referrer_class", sa.SmallInteger(), nullable=True),
            sa.Column("device_class", sa.SmallInteger(), nullable=True),
        )

    if not _column_exists("mps_product", "view_count"):
        op.add_column(
            "mps_product",
            sa.Column(
                "view_count", sa.BigInteger(),
                nullable=False, server_default="0",
            ),
        )


def downgrade():
    op.drop_table("mps_page_session")
    op.drop_column("mps_product", "view_count")
