"""Add Benefit.metadata

Revision ID: ce111f3235e3
Revises: 7a95c37b2a26
Create Date: 2025-04-16 10:50:02.481144

"""

import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import postgresql

# Polar Custom Imports

# revision identifiers, used by Alembic.
revision = "ce111f3235e3"
down_revision = "7a95c37b2a26"
branch_labels: tuple[str] | None = None
depends_on: tuple[str] | None = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column(
        "benefits",
        sa.Column(
            "user_metadata", postgresql.JSONB(astext_type=sa.Text()), nullable=True
        ),
    )

    op.execute(
        """
        UPDATE benefits
        SET user_metadata = '{}'
        WHERE user_metadata IS NULL;
        """
    )

    op.alter_column(
        "benefits",
        "user_metadata",
        existing_type=postgresql.JSONB(astext_type=sa.Text()),
        nullable=False,
    )

    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column("benefits", "user_metadata")
    # ### end Alembic commands ###
