"""add S3 bucket credentials to user

Revision ID: f898ba460612
Revises: a2c13d3117f2
Create Date: 2026-02-27 12:35:45.090589

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = 'f898ba460612'
down_revision = 'a2c13d3117f2'
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():
    columns = [
        ("s3_endpoint", sa.Unicode(256)),
        ("s3_region", sa.Unicode(64)),
        ("s3_bucket", sa.Unicode(128)),
        ("s3_access_key", sa.Unicode(128)),
        ("s3_secret_key", sa.Unicode(128)),
    ]
    for col_name, col_type in columns:
        if not _column_exists("mps_user", col_name):
            op.add_column("mps_user", sa.Column(col_name, col_type, nullable=True))


def downgrade():
    for col_name in ("s3_endpoint", "s3_region", "s3_bucket", "s3_access_key", "s3_secret_key"):
        op.drop_column("mps_user", col_name)
