"""Per-participant chat quota

Replace the shared per-conversation message pool
(``free_messages_remaining`` / ``rewarded_messages_remaining`` / ``ads_watched``)
with independent buyer/seller counters so each participant has their own quota
and rewarded unlocks.

Revision ID: c1a2b3d4e5f6
Revises: b83e1f2c04aa
Create Date: 2026-07-03

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = "c1a2b3d4e5f6"
down_revision = "b83e1f2c04aa"
branch_labels = None
depends_on = None

# New per-participant integer counters (all default 0).
_NEW_COLUMNS = (
    "buyer_messages_used",
    "seller_messages_used",
    "buyer_unlocked_messages",
    "seller_unlocked_messages",
    "buyer_ads_watched",
    "seller_ads_watched",
)

# Legacy shared-pool columns removed by this migration.
_OLD_COLUMNS = (
    ("free_messages_remaining", "10"),
    ("rewarded_messages_remaining", "0"),
    ("ads_watched", "0"),
)


def upgrade():
    with op.batch_alter_table("conversations") as batch:
        for name in _NEW_COLUMNS:
            batch.add_column(
                sa.Column(name, sa.Integer(), nullable=False, server_default="0")
            )
        for name, _ in _OLD_COLUMNS:
            batch.drop_column(name)


def downgrade():
    with op.batch_alter_table("conversations") as batch:
        for name, default in _OLD_COLUMNS:
            batch.add_column(
                sa.Column(name, sa.Integer(), nullable=False, server_default=default)
            )
        for name in _NEW_COLUMNS:
            batch.drop_column(name)
