"""offer acceptance payment window + buyer cancelled state

Revision ID: 632878c8f243
Revises: 1a419114ddf7
Create Date: 2026-05-13 10:23:21.137263

Adds two columns:
- mps_shop.offer_acceptance_payment_hours: how long the buyer has to
  pay after a seller accepts an offer. Default 24h.
- mps_offer.accepted_timestamp: when the seller accepted (or auto-
  accept fired). Drives the buyer-pay deadline. Nullable for offers
  that never reached acceptance.

No new state column is needed for BUYER_CANCELLED — the existing
`state` integer is reused; lib/offer.py defines the new enum value.
"""
from alembic import op
import sqlalchemy as sa


revision = '632878c8f243'
down_revision = '1a419114ddf7'
branch_labels = None
depends_on = None


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():
    if not _column_exists("mps_shop", "offer_acceptance_payment_hours"):
        op.add_column(
            "mps_shop",
            sa.Column(
                "offer_acceptance_payment_hours",
                sa.BigInteger(),
                nullable=False,
                server_default="24",
            ),
        )
    if not _column_exists("mps_offer", "accepted_timestamp"):
        op.add_column(
            "mps_offer",
            sa.Column("accepted_timestamp", sa.BigInteger(), nullable=True),
        )


def downgrade():
    if _column_exists("mps_offer", "accepted_timestamp"):
        op.drop_column("mps_offer", "accepted_timestamp")
    if _column_exists("mps_shop", "offer_acceptance_payment_hours"):
        op.drop_column("mps_shop", "offer_acceptance_payment_hours")
