# Generated by Django 4.2.24 on 2025-09-29 19:46

import django.db.models.deletion
from django.db import migrations, models


def set_adj_request_original_item_data(apps, schema_editor):
    AdjustmentRequest = apps.get_model("NEMO", "AdjustmentRequest")
    for adj in AdjustmentRequest.objects.filter(item_type__isnull=False, item_id__isnull=False):
        model = apps.get_model(adj.item_type.app_label, adj.item_type.model)
        item = model.objects.get(pk=adj.item_id)
        if item:
            updated = False
            tool_id = getattr(item, "tool_id", None)
            area_id = getattr(item, "area_id", None)
            if tool_id:
                adj.item_tool_id = tool_id
                updated = True
            if area_id:
                adj.item_area_id = area_id
                updated = True
            if adj.status == 0:
                for att in ["start", "end", "quantity", "project_id"]:
                    item_att = getattr(item, att, None)
                    if item_att:
                        setattr(adj, f"original_{att}", item_att)
                        updated = True
            if updated:
                adj.save(
                    update_fields=[
                        "item_tool_id",
                        "item_area_id",
                        "original_start",
                        "original_end",
                        "original_quantity",
                        "original_project_id",
                    ]
                )


class Migration(migrations.Migration):

    dependencies = [
        ("NEMO", "0128_adjustmentrequest_new_project"),
    ]

    operations = [
        migrations.AddField(
            model_name="adjustmentrequest",
            name="original_end",
            field=models.DateTimeField(blank=True, null=True),
        ),
        migrations.AddField(
            model_name="adjustmentrequest",
            name="original_project",
            field=models.ForeignKey(
                blank=True,
                null=True,
                on_delete=django.db.models.deletion.CASCADE,
                related_name="original_adjustmentrequest_set",
                to="NEMO.project",
            ),
        ),
        migrations.AddField(
            model_name="adjustmentrequest",
            name="original_quantity",
            field=models.PositiveIntegerField(blank=True, null=True),
        ),
        migrations.AddField(
            model_name="adjustmentrequest",
            name="original_start",
            field=models.DateTimeField(blank=True, null=True),
        ),
        migrations.AddField(
            model_name="adjustmentrequest",
            name="item_area",
            field=models.ForeignKey(
                blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to="NEMO.area"
            ),
        ),
        migrations.AddField(
            model_name="adjustmentrequest",
            name="item_tool",
            field=models.ForeignKey(
                blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to="NEMO.tool"
            ),
        ),
        migrations.RunPython(set_adj_request_original_item_data, migrations.RunPython.noop),
    ]
