"""Add Organization.customer_email_settings

Revision ID: 59a5d45ae3fd
Revises: 10baf05ea3ef
Create Date: 2025-10-16 10:30:31.948196

"""

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

# Polar Custom Imports

# revision identifiers, used by Alembic.
revision = "59a5d45ae3fd"
down_revision = "10baf05ea3ef"
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(
        "organizations",
        sa.Column(
            "customer_email_settings",
            postgresql.JSONB(astext_type=sa.Text()),
            nullable=True,
        ),
    )
    op.execute(
        """
        UPDATE organizations
        SET customer_email_settings = '{
            "order_confirmation": true,
            "subscription_cancellation": true,
            "subscription_confirmation": true,
            "subscription_cycled": true,
            "subscription_past_due": true,
            "subscription_revoked": true,
            "subscription_uncanceled": true,
            "subscription_updated": true
        }'
        WHERE customer_email_settings IS NULL
        """
    )
    op.alter_column(
        "organizations",
        "customer_email_settings",
        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("organizations", "customer_email_settings")
    # ### end Alembic commands ###
