"""Peewee migrations -- 190_MAIN.py.

Drop the ``_record`` suffix from foreign-key fields. The suffix was an internal
convention to disambiguate a live FK from the denormalized snapshot columns that
share its base name (``customer`` FK vs ``customer_name`` etc.). With the API
serialization layer now mapping a bare FK ``<x>`` to ``<x>_id`` on the wire, the
suffix no longer earns its keep, so the fields are renamed to their bare names
(and ``car_record`` -> ``vehicle``, since the API noun is ``/vehicles``).

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", "customer_record", "customer"),
    ("order", "contract_record", "contract"),
    ("order", "construction_site_record", "construction_site"),
    ("order", "recipe_record", "recipe"),
    ("delivery", "car_record", "vehicle"),
    ("delivery", "driver_record", "driver"),
    ("delivery", "transport_zone_record", "transport_zone"),
    ("pumporder", "customer_record", "customer"),
    ("pumporder", "construction_site_record", "construction_site"),
    ("pumporder", "driver_record", "driver"),
    ("pumporder", "order_record", "order"),
    ("pumporder", "pump_record", "pump"),
    ("ordersurcharge", "template_record", "template"),
    ("pumpordersurcharge", "template_record", "template"),
    ("taxdocument", "customer_record", "customer"),
]


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)
