"""Peewee migrations -- 189_MAIN.py.

Add ``pumporder.taxdocument_id`` nullable FK so PumpOrders can be invoiced
explicitly via TaxDocument (mirrors ``order.taxdocument_id`` added in 182).
"""

import peewee as pw
from peewee_migrate import Migrator

from migrations.utils import drop_fk_column_fix


def migrate(migrator: Migrator, database: pw.Database, *, fake: bool = False) -> None:
    migrator.add_fields(
        "pumporder",
        taxdocument=pw.ForeignKeyField(
            backref="pump_orders",
            column_name="taxdocument_id",
            field="id",
            model=migrator.orm["taxdocument"],
            null=True,
            on_delete="SET NULL",
        ),
    )


def rollback(migrator: Migrator, database: pw.Database, *, fake: bool = False) -> None:
    drop_fk_column_fix()
    migrator.remove_fields("pumporder", "taxdocument")
