# Generated by Django 3.2.13 on 2022-05-10 20:08

import quantityfield.fields
from django.db import migrations


def forwards(apps, schema_editor):
    Column = apps.get_model("seed", "Column")
    Organization = apps.get_model("orgs", "Organization")

    new_db_fields = [
        {
            'column_name': 'total_ghg_emissions',
            'table_name': 'PropertyState',
            'display_name': 'Total GHG Emissions',
            'data_type': 'number',
        }, {
            'column_name': 'total_marginal_ghg_emissions',
            'table_name': 'PropertyState',
            'display_name': 'Total Marginal GHG Emissions',
            'data_type': 'number',
        }, {
            'column_name': 'total_ghg_emissions_intensity',
            'table_name': 'PropertyState',
            'display_name': 'Total GHG Emissions Intensity',
            'data_type': 'number',
        }, {
            'column_name': 'total_marginal_ghg_emissions_intensity',
            'table_name': 'PropertyState',
            'display_name': 'Total Marginal GHG Emissions Intensity',
            'data_type': 'number',
        }
    ]

    # Go through all the organizations
    for org in Organization.objects.all():
        for new_db_field in new_db_fields:
            columns = Column.objects.filter(
                organization_id=org.id,
                table_name=new_db_field['table_name'],
                column_name=new_db_field['column_name'],
                is_extra_data=False,
            )

            if not columns.count():
                new_db_field['organization_id'] = org.id
                Column.objects.create(**new_db_field)
            elif columns.count() == 1:
                # If the column exists, then just update the display_name and data_type if empty
                c = columns.first()
                if c.display_name is None or c.display_name == '':
                    c.display_name = new_db_field['display_name']
                if c.data_type is None or c.data_type == '' or c.data_type == 'None':
                    c.data_type = new_db_field['data_type']
                c.save()
            else:
                print("  More than one column returned")


class Migration(migrations.Migration):

    dependencies = [
        ('seed', '0163_add_bae_assets_to_buildingsync_default_profile'),
    ]

    operations = [
        migrations.AddField(
            model_name='propertystate',
            name='total_ghg_emissions',
            field=quantityfield.fields.QuantityField(base_units='MtCO2e/year', blank=True, null=True, unit_choices=['MtCO2e/year']),
        ),
        migrations.AddField(
            model_name='propertystate',
            name='total_ghg_emissions_intensity',
            field=quantityfield.fields.QuantityField(base_units='kgCO2e/ft**2/year', blank=True, null=True, unit_choices=['kgCO2e/ft**2/year']),
        ),
        migrations.AddField(
            model_name='propertystate',
            name='total_marginal_ghg_emissions',
            field=quantityfield.fields.QuantityField(base_units='MtCO2e/year', blank=True, null=True, unit_choices=['MtCO2e/year']),
        ),
        migrations.AddField(
            model_name='propertystate',
            name='total_marginal_ghg_emissions_intensity',
            field=quantityfield.fields.QuantityField(base_units='kgCO2e/ft**2/year', blank=True, null=True, unit_choices=['kgCO2e/ft**2/year']),
        ),
        migrations.RunPython(forwards)
    ]
