"""Peewee migrations -- 197_MAIN.py.

KDX cement-as-addition workaround: ``RecipeMaterial.declared_type``.

KDX lists the same physical material under more than one type slot - most often a
cement that also appears as ``Addition`` (a second cement dosed through the
cement bin, because legacy atx300 control has no ``Cement2`` slot: see
``control/src/Objed.pas``, which reads ``Cement1`` + ``Addition1..N`` as one
cement-dosing group). ``Material.name`` is globally unique, so the second slot
can't create a second ``Material`` row of a different type - it must reuse the
existing one and remember, per recipe, which slot (role) it was declared as.

``declared_type`` carries that role from import (``standby.make_recipe_materials``)
through order dispatch (``OrderMaterial.type``) back out to the ``.ord`` we emit
to manager/control, so the slot label is preserved and the legacy control doses
the right material into the right bin. Nullable: existing rows behave as before
(``OrderMaterial.type`` falls back to ``material.type``).

Intended to be dropped once KDX stops emitting cements as additions; rollback is
``remove_fields``.
"""

import peewee as pw
from peewee_migrate import Migrator


def migrate(migrator: Migrator, database: pw.Database, *, fake=False):
    """Write your migrations here."""

    migrator.add_fields(
        "recipematerial",
        declared_type=pw.TextField(null=True),
    )


def rollback(migrator: Migrator, database: pw.Database, *, fake=False):
    """Write your rollback migrations here."""

    migrator.remove_fields("recipematerial", "declared_type")
