# Generated by Django 3.0.12 on 2021-04-27 23:15

from django.db import migrations


def setup_biz(apps, schema_editor):
    # We get the model from the versioned app registry;
    # if we directly import it, it'll be the wrong version
    Vendor = apps.get_model("gbe", "Vendor")
    Business = apps.get_model("gbe", "Business")
    for bid in Vendor.objects.all().order_by("-pk"):
        business = None
        if bid.profile.business_set.all().count() > 0:
            business = bid.profile.business_set.first()
        else:
            business = Business(
                name=bid.b_title,
                description=bid.b_description,
                website=bid.website,
                physical_address=bid.physical_address,
                publish_physical_address=bid.publish_physical_address,
                img=bid.img)
            business.save()
            business.owners.add(bid.profile)
        bid.business = business
        bid.save()


def back_to_bid(apps, schema_editor):
    Vendor = apps.get_model("gbe", "Vendor")
    Business = apps.get_model("gbe", "Business")
    for bid in Vendor.objects.all():
        bid.b_title = bid.business.name
        bid.b_description = bid.business.description
        bid.website = bid.business.website
        bid.physical_address = bid.business.physical_address
        bid.publish_physical_address = bid.business.publish_physical_address
        bid.img = bid.business.img
        bid.profile = bid.business.owners.first()
        bid.save()


class Migration(migrations.Migration):

    dependencies = [
        ('gbe', '0007_auto_20210427_2229'),
    ]

    operations = [
        migrations.RunPython(setup_biz, reverse_code=back_to_bid),
    ]
