"""add color_filter to shop

Revision ID: d4c7d6a1e961
Revises: c3b37cb2c9cc
Create Date: 2026-02-15 07:35:31.704574

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = 'd4c7d6a1e961'
down_revision = 'c3b37cb2c9cc'
branch_labels = None
depends_on = 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 _column_exists("mps_shop", "color_filter"):
        op.add_column(
            "mps_shop",
            sa.Column("color_filter", sa.BigInteger(), nullable=False, server_default="0"),
        )

    # Migrate existing grayscale_mode=1 rows to color_filter=1
    conn = op.get_bind()
    if _column_exists("mps_shop", "grayscale_mode"):
        conn.execute(
            sa.text("UPDATE mps_shop SET color_filter = 1 WHERE grayscale_mode = 1")
        )


def downgrade():
    op.drop_column("mps_shop", "color_filter")
