"""Peewee migrations -- 191_MAIN.py.

Fix the prefix of two frozen-snapshot field groups so it matches the FK they
mirror (the convention is ``<fkname>_<attr>``):

* ``Order.r_*`` -> ``Order.recipe_*`` (snapshot of the ``recipe`` FK target)
* ``Delivery.zone_*`` -> ``Delivery.transport_zone_*`` (snapshot of the
  ``transport_zone`` FK target)

This only renames columns; no data is moved.
"""

import peewee as pw
from peewee_migrate import Migrator


# (table, old field name, new field name)
RENAMES = [
    # Order: recipe snapshot, r_* -> recipe_*
    ("order", "r_name", "recipe_name"),
    ("order", "r_price", "recipe_price"),
    ("order", "r_recipe_class", "recipe_recipe_class"),
    ("order", "r_description", "recipe_description"),
    ("order", "r_comment", "recipe_comment"),
    ("order", "r_consistency_class", "recipe_consistency_class"),
    ("order", "r_exposure_classes", "recipe_exposure_classes"),
    ("order", "r_batch_volume_limit", "recipe_batch_volume_limit"),
    ("order", "r_lift_pour_duration", "recipe_lift_pour_duration"),
    ("order", "r_lift_semi_pour_duration", "recipe_lift_semi_pour_duration"),
    ("order", "r_mixer_semi_opening_duration", "recipe_mixer_semi_opening_duration"),
    ("order", "r_mixer_semi_opening2_duration", "recipe_mixer_semi_opening2_duration"),
    ("order", "r_mixer_opening_duration", "recipe_mixer_opening_duration"),
    ("order", "r_mixing_duration", "recipe_mixing_duration"),
    ("order", "r_k_value", "recipe_k_value"),
    ("order", "r_k_ratio", "recipe_k_ratio"),
    ("order", "r_number", "recipe_number"),
    ("order", "r_ufi_code", "recipe_ufi_code"),
    ("order", "r_d_max", "recipe_d_max"),
    ("order", "r_cl_content", "recipe_cl_content"),
    ("order", "r_air_content", "recipe_air_content"),
    ("order", "r_vc", "recipe_vc"),
    ("order", "r_cement_min", "recipe_cement_min"),
    ("order", "r_workability_time", "recipe_workability_time"),
    # Delivery: transport-zone snapshot, zone_* -> transport_zone_*
    ("delivery", "zone_price_per_m3", "transport_zone_price_per_m3"),
    ("delivery", "zone_price_is_per_m3", "transport_zone_price_is_per_m3"),
    ("delivery", "zone_minimal_volume", "transport_zone_minimal_volume"),
]


def migrate(migrator: Migrator, database: pw.Database, *, fake: bool = False) -> None:
    for table, old, new in RENAMES:
        migrator.rename_field(table, old, new)


def rollback(migrator: Migrator, database: pw.Database, *, fake: bool = False) -> None:
    for table, old, new in reversed(RENAMES):
        migrator.rename_field(table, new, old)
