"""Add payment_mode field to receipts table.

Revision ID: 20251216_payment_mode
Revises: 4b8e5f2a1d93
Create Date: 2024-12-16
"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '20251216_payment_mode'
down_revision = '4b8e5f2a1d93'
branch_labels = None
depends_on = None


def upgrade() -> None:
    """Add payment_mode column and migrate existing data."""
    with op.batch_alter_table('receipts', schema=None) as batch_op:
        batch_op.add_column(sa.Column('payment_mode', sa.String(length=20), nullable=True))

    # Migrate existing data based on cash_register_account
    op.execute("""
        UPDATE receipts
        SET payment_mode = 'casa'
        WHERE cash_register_account LIKE '531%' AND payment_mode IS NULL
    """)
    op.execute("""
        UPDATE receipts
        SET payment_mode = 'banca'
        WHERE cash_register_account LIKE '512%' AND payment_mode IS NULL
    """)
    op.execute("""
        UPDATE receipts
        SET payment_mode = 'avans_decontare'
        WHERE cash_register_account LIKE '542%' AND payment_mode IS NULL
    """)


def downgrade() -> None:
    """Remove payment_mode column."""
    with op.batch_alter_table('receipts', schema=None) as batch_op:
        batch_op.drop_column('payment_mode')
