"""add environment trial and primary s3 columns to shop

Revision ID: 9884324a48e3
Revises: f8201a9ba045
Create Date: 2026-03-07 17:49:35.847633

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '9884324a48e3'
down_revision = 'f8201a9ba045'
branch_labels = None
depends_on = None

from make_post_sell.models.meta import UUIDType


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():
    # MPS-14: Shop environment
    if not _column_exists("mps_shop", "environment"):
        op.add_column(
            "mps_shop",
            sa.Column("environment", sa.BigInteger(), nullable=False, server_default="0"),
        )

    # MPS-15: Trial and billing
    if not _column_exists("mps_shop", "trial_started_timestamp"):
        op.add_column(
            "mps_shop",
            sa.Column("trial_started_timestamp", sa.BigInteger(), nullable=True),
        )

    if not _column_exists("mps_shop", "trial_ended"):
        op.add_column(
            "mps_shop",
            sa.Column("trial_ended", sa.Boolean(), nullable=False, server_default="0"),
        )

    if not _column_exists("mps_shop", "plan_active"):
        op.add_column(
            "mps_shop",
            sa.Column("plan_active", sa.Boolean(), nullable=False, server_default="0"),
        )

    # MPS-16: Primary S3 bucket (BYOB)
    if not _column_exists("mps_shop", "primary_s3_endpoint"):
        op.add_column(
            "mps_shop",
            sa.Column("primary_s3_endpoint", sa.Unicode(256), nullable=True),
        )

    if not _column_exists("mps_shop", "primary_s3_region"):
        op.add_column(
            "mps_shop",
            sa.Column("primary_s3_region", sa.Unicode(64), nullable=True),
        )

    if not _column_exists("mps_shop", "primary_s3_bucket"):
        op.add_column(
            "mps_shop",
            sa.Column("primary_s3_bucket", sa.Unicode(128), nullable=True),
        )

    if not _column_exists("mps_shop", "primary_s3_access_key"):
        op.add_column(
            "mps_shop",
            sa.Column("primary_s3_access_key", sa.Unicode(128), nullable=True),
        )

    if not _column_exists("mps_shop", "primary_s3_secret_key"):
        op.add_column(
            "mps_shop",
            sa.Column("primary_s3_secret_key", sa.Unicode(128), nullable=True),
        )

    if not _column_exists("mps_shop", "primary_s3_cdn_endpoint"):
        op.add_column(
            "mps_shop",
            sa.Column("primary_s3_cdn_endpoint", sa.Unicode(256), nullable=True),
        )

    if not _column_exists("mps_shop", "primary_s3_enabled"):
        op.add_column(
            "mps_shop",
            sa.Column("primary_s3_enabled", sa.Boolean(), nullable=False, server_default="0"),
        )


def downgrade():
    op.drop_column("mps_shop", "primary_s3_enabled")
    op.drop_column("mps_shop", "primary_s3_cdn_endpoint")
    op.drop_column("mps_shop", "primary_s3_secret_key")
    op.drop_column("mps_shop", "primary_s3_access_key")
    op.drop_column("mps_shop", "primary_s3_bucket")
    op.drop_column("mps_shop", "primary_s3_region")
    op.drop_column("mps_shop", "primary_s3_endpoint")
    op.drop_column("mps_shop", "plan_active")
    op.drop_column("mps_shop", "trial_ended")
    op.drop_column("mps_shop", "trial_started_timestamp")
    op.drop_column("mps_shop", "environment")
