"""C4 completion: explicit copier meter roles and order cancellation audit.

Revision ID: t5h6i7j8k9l0
Revises: s4g5h6i7j8k9
"""
from alembic import op
import sqlalchemy as sa


revision = "t5h6i7j8k9l0"
down_revision = "s4g5h6i7j8k9"
branch_labels = None
depends_on = None


def upgrade():
    op.add_column("meters", sa.Column("contract_role", sa.String(length=20), nullable=False, server_default="NONE"))
    op.create_check_constraint(
        "ck_meter_contract_role",
        "meters",
        "contract_role in ('COPIER_BW', 'COPIER_COLOR', 'NONE')",
    )
    op.add_column("consumable_orders", sa.Column("cancelled_at", sa.DateTime(), nullable=True))
    op.add_column("consumable_orders", sa.Column("cancelled_by_id", sa.Integer(), nullable=True))
    op.add_column("consumable_orders", sa.Column("cancellation_reason", sa.Text(), nullable=True))
    op.create_foreign_key(
        "fk_consumable_orders_cancelled_by",
        "consumable_orders", "users", ["cancelled_by_id"], ["id"], ondelete="SET NULL",
    )


def downgrade():
    op.drop_constraint("fk_consumable_orders_cancelled_by", "consumable_orders", type_="foreignkey")
    op.drop_column("consumable_orders", "cancellation_reason")
    op.drop_column("consumable_orders", "cancelled_by_id")
    op.drop_column("consumable_orders", "cancelled_at")
    op.drop_constraint("ck_meter_contract_role", "meters", type_="check")
    op.drop_column("meters", "contract_role")
