"""Add md to kb_allowed_formats defaults

Revision ID: 056
Revises: 055
Create Date: 2026-03-12
"""
from alembic import op
import sqlalchemy as sa

revision = "056"
down_revision = "055"
branch_labels = None
depends_on = None


def upgrade() -> None:
    # Update server default for new rows
    op.alter_column(
        "plan_limits",
        "kb_allowed_formats",
        server_default=sa.text("'[\"txt\",\"paste\",\"md\"]'::jsonb"),
    )
    # Add "md" to existing rows that have the old default ["txt","paste"]
    op.execute(
        """
        UPDATE plan_limits
        SET kb_allowed_formats = kb_allowed_formats || '["md"]'::jsonb
        WHERE NOT kb_allowed_formats @> '"md"'::jsonb
        """
    )


def downgrade() -> None:
    op.alter_column(
        "plan_limits",
        "kb_allowed_formats",
        server_default=sa.text("'[\"txt\",\"paste\"]'::jsonb"),
    )
    op.execute(
        """
        UPDATE plan_limits
        SET kb_allowed_formats = kb_allowed_formats - 'md'
        """
    )
