"""add S3 mirror bucket credentials to shop

Revision ID: 6b516114c393
Revises: f898ba460612
Create Date: 2026-02-27 15:43:58.571158

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '6b516114c393'
down_revision = 'f898ba460612'
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():
    columns = [
        ("mirror_s3_endpoint", sa.Unicode(256), True, None),
        ("mirror_s3_region", sa.Unicode(64), True, None),
        ("mirror_s3_bucket", sa.Unicode(128), True, None),
        ("mirror_s3_access_key", sa.Unicode(128), True, None),
        ("mirror_s3_secret_key", sa.Unicode(128), True, None),
        ("mirror_s3_enabled", sa.Boolean(), False, "0"),
    ]
    for col_name, col_type, nullable, server_default in columns:
        if not _column_exists("mps_shop", col_name):
            op.add_column(
                "mps_shop",
                sa.Column(
                    col_name,
                    col_type,
                    nullable=nullable,
                    server_default=server_default,
                ),
            )


def downgrade():
    for col_name in (
        "mirror_s3_enabled",
        "mirror_s3_secret_key",
        "mirror_s3_access_key",
        "mirror_s3_bucket",
        "mirror_s3_region",
        "mirror_s3_endpoint",
    ):
        if _column_exists("mps_shop", col_name):
            op.drop_column("mps_shop", col_name)
