"""loyalty 011 - add acting_terminal_device_id to loyalty_transactions

Lets the audit log distinguish between actions a human user performed
directly via the web terminal and actions a paired POS tablet
performed via its device JWT. The principal-of-record stays the
pairing user (so existing reports keep working); this column adds
"which tablet did it" alongside.

Revision ID: loyalty_011
Revises: loyalty_010
Create Date: 2026-05-05
"""
import sqlalchemy as sa

from alembic import op

revision = "loyalty_011"
down_revision = "loyalty_010"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.add_column(
        "loyalty_transactions",
        sa.Column(
            "acting_terminal_device_id",
            sa.Integer(),
            sa.ForeignKey(
                "loyalty_terminal_devices.id", ondelete="SET NULL"
            ),
            nullable=True,
            comment=(
                "Paired POS terminal device that performed this transaction "
                "(NULL when the action came from a human user via the web)"
            ),
        ),
    )
    op.create_index(
        "ix_loyalty_transactions_acting_terminal_device_id",
        "loyalty_transactions",
        ["acting_terminal_device_id"],
    )


def downgrade() -> None:
    op.drop_index(
        "ix_loyalty_transactions_acting_terminal_device_id",
        table_name="loyalty_transactions",
    )
    op.drop_column("loyalty_transactions", "acting_terminal_device_id")
