# Generated by Django 5.2.9 on 2026-01-09 14:32

from django.db import migrations

"""
Will look at the currency code and return the appropriate conversion factor to convert to cents.
For example, USD has 2 decimal places, so the factor would be 100.
JPY has 0 decimal places, so the factor would be 1.
"""


def convert_prices_to_integers(apps, schema_editor):
    from vendor.models.utils import convert_to_minor_units

    Price = apps.get_model("vendor", "Price")
    Invoice = apps.get_model("vendor", "Invoice")

    # Convert Price.cost
    for price in Price.objects.all():
        price.cost = convert_to_minor_units(price.cost, price.currency)
        price.save(update_fields=["cost"])

    # Convert Invoice monetary fields
    for invoice in Invoice.objects.all():
        invoice.subtotal = convert_to_minor_units(invoice.subtotal, invoice.currency)

        if invoice.tax is not None:
            invoice.tax = convert_to_minor_units(invoice.tax, invoice.currency)

        if invoice.shipping is not None:
            invoice.shipping = convert_to_minor_units(
                invoice.shipping, invoice.currency
            )

        if invoice.total is not None:
            invoice.total = convert_to_minor_units(invoice.total, invoice.currency)

        if invoice.global_discount is not None:
            invoice.global_discount = convert_to_minor_units(
                invoice.global_discount, invoice.currency
            )

        invoice.save(
            update_fields=["subtotal", "tax", "shipping", "total", "global_discount"]
        )


def revert_prices_to_decimals(apps, schema_editor):
    from vendor.models.utils import revert_to_major_units

    Price = apps.get_model("vendor", "Price")
    Invoice = apps.get_model("vendor", "Invoice")

    # Revert Price.cost
    for price in Price.objects.all():
        price.cost = revert_to_major_units(price.cost, price.currency)
        price.save(update_fields=["cost"])

    # Revert Invoice monetary fields
    for invoice in Invoice.objects.all():
        invoice.subtotal = revert_to_major_units(invoice.subtotal, invoice.currency)

        if invoice.tax is not None:
            invoice.tax = revert_to_major_units(invoice.tax, invoice.currency)

        if invoice.shipping is not None:
            invoice.shipping = revert_to_major_units(invoice.shipping, invoice.currency)

        if invoice.total is not None:
            invoice.total = revert_to_major_units(invoice.total, invoice.currency)

        if invoice.global_discount is not None:
            invoice.global_discount = revert_to_major_units(
                invoice.global_discount, invoice.currency
            )
        invoice.save(
            update_fields=["subtotal", "tax", "shipping", "total", "global_discount"]
        )


class Migration(migrations.Migration):

    dependencies = [
        ("vendor", "0045_alter_address_country"),
    ]

    operations = [
        migrations.RunPython(
            convert_prices_to_integers, reverse_code=revert_prices_to_decimals
        ),
    ]
