# Generated by Django 4.2.1 on 2023-10-12 16:12

from django.db import migrations

def transfer_nutrivals(apps, schema_editor):
    NutritionalValues = apps.get_model("receipe", "NutritionalValues")
    Article = apps.get_model("receipe", "Article")
    Nutrient = apps.get_model("receipe", "Nutrient")
    NutrientArticle = apps.get_model("receipe", "NutrientArticle")

    for nutriVal in NutritionalValues.objects.filter(energy__isnull=False):
        keyVal = [['Energie','energy'],
                  ['Fett','fat'],
                  ['gesättigte Fettsäuren','saturatedFat'],
                  ['Kohlenhydrate','carbohydrate'],
                  ['Zucker','sugars'],
                  ['Ballaststoffe','dietaryFiber'],
                  ['Eiweiß','proteins'],
                  ['Salz','salt'],
                  ['Natrium','sodium'],
                  ['Vitamin A','vitamin_a'],
                  ['Vitamin B3','vitamin_b3'],
                  ['Vitamin B1','vitamin_b1'],
                  ['Vitamin B2','vitamin_b2'],
                  ['Vitamin B5','vitamin_b5'],
                  #['Vitamin B6','vitamin_b6'],
                  ['Vitamin B7','vitamin_b7'],
                  ['Vitamin B9','vitamin_b9'],
                  ['Vitamin B12','vitamin_b12'],
                  ['Vitamin C','vitamin_c'],
                  ['Vitamin D','vitamin_d'],
                  ['Vitamin E','vitamin_e'],
                  ['Vitamin K','vitamin_k'],
                  ['Kalium','potassium'],
                  ['Kalzium','calcium'],
                  ['Magnesium','magnesium'],
                  ['Eisen','iron'],
                  ['Zink','zinc'],
                  ['Sulfat','sulfat'],
                  ['Chlorid','chlorid'],
                  ['Hydrogencarbonat','hydrogencarbonat']
        ]

        for element in keyVal:
            if eval('nutriVal.'+element[1]) is not None:
                if element[0] != 'Salz' or element[0] != 'Natrium':
                    nutrient =  Nutrient.objects.get(name=element[0])
                    nutrienArticle = NutrientArticle(article=nutriVal.article,
                                                    nutrient=nutrient,
                                                    value=eval('nutriVal.'+element[1]),
                                                    unit=eval('nutriVal.'+element[1]+'Unit'),
                                                    isEstimated=False
                                                    )
                    nutrienArticle.save()

                elif element[0] == 'Salz':
                    nutrient =  Nutrient.objects.get(name=element[0])
                    nutrienArticle = NutrientArticle(article=nutriVal.article,
                                                    nutrient=nutrient,
                                                    value=eval('nutriVal.'+element[1]),
                                                    unit=eval('nutriVal.'+element[1]+'Unit'),
                                                    isEstimated=False
                                                    )
                    nutrienArticle.save()

                    nutrient =  Nutrient.objects.get(name='Natrium')
                    nutrienArticle = NutrientArticle(article=nutriVal.article,
                                                    nutrient=nutrient,
                                                    value=eval('nutriVal.'+element[1])*0.3937,
                                                    unit=eval('nutriVal.'+element[1]+'Unit'),
                                                    isEstimated=False
                                                    )
                    nutrienArticle.save()
 


class Migration(migrations.Migration):
    dependencies = [
        ("receipe", "0054_nutrientalias"),
    ]

    operations = [
        migrations.RunPython(transfer_nutrivals),
    ]
