# -*- coding: utf-8 -*-
# Generated by Django 1.11.29 on 2022-10-28 17:47
from __future__ import unicode_literals

from django.conf import settings
import django.contrib.postgres.fields
from django.db import migrations, models
import django.db.migrations.operations.special
import django.db.models.deletion
import uuid


# Functions from the following migrations need manual copying.
# Move them and any dependencies into this file, then update the
# RunPython operations to refer to the local versions:
# ads.migrations.0012_populate_uuid_values
# ads.migrations.0018_auto_20221027_1707
# ads.migrations.0019_auto_20221027_1717

def gen_uuid(apps, schema_editor):
    MyModel = apps.get_model('ads', 'Creative')
    for row in MyModel.objects.all():
        row.uuid = uuid.uuid4()
        row.save(update_fields=['uuid'])

def creative_populate_labels(apps, schema_editor):
    Creative = apps.get_model('ads', 'Creative')
    db_alias = schema_editor.connection.alias
    Creative.objects.using(db_alias).filter(labels__isnull=True).update(labels=[])

def line_item_populate_labels(apps, schema_editor):
    LineItem = apps.get_model('ads', 'LineItem')
    db_alias = schema_editor.connection.alias
    LineItem.objects.using(db_alias).filter(labels__isnull=True).update(labels=[])

class Migration(migrations.Migration):

    replaces = [('ads', '0010_creative_fluid'), ('ads', '0011_creative_uuid'), ('ads', '0012_populate_uuid_values'), ('ads', '0013_remove_uuid_null'), ('ads', '0014_log'), ('ads', '0015_auto_20220602_2051'), ('ads', '0016_auto_20220916_1846'), ('ads', '0017_remove_creative_label'), ('ads', '0018_auto_20221027_1707'), ('ads', '0019_auto_20221027_1717')]

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ('ads', '0009_auto_20220425_1718'),
    ]

    operations = [
        migrations.AddField(
            model_name='creative',
            name='fluid',
            field=models.BooleanField(default=False),
        ),
        migrations.AddField(
            model_name='creative',
            name='uuid',
            field=models.UUIDField(default=uuid.uuid4, null=True),
        ),
        migrations.RunPython(
            code=gen_uuid,
            reverse_code=migrations.RunPython.noop,
        ),
        migrations.AlterField(
            model_name='creative',
            name='uuid',
            field=models.UUIDField(default=uuid.uuid4, editable=False, unique=True),
        ),
        migrations.CreateModel(
            name='Log',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('ip', models.GenericIPAddressField()),
                ('country', models.CharField(max_length=128, null=True)),
                ('city', models.CharField(max_length=128, null=True)),
                ('datetime', models.DateTimeField(auto_now_add=True)),
                ('log_type', models.CharField(choices=[('view', 'View'), ('click', 'Click')], max_length=10)),
                ('creative', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='ads.Creative')),
                ('line_item', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='ads.LineItem')),
                ('user', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to=settings.AUTH_USER_MODEL)),
            ],
        ),
        migrations.AddField(
            model_name='lineitem',
            name='targeting_city',
            field=models.CharField(blank=True, max_length=256, null=True),
        ),
        migrations.AddField(
            model_name='lineitem',
            name='targeting_country',
            field=models.CharField(blank=True, max_length=256, null=True),
        ),
        migrations.RemoveField(
            model_name='creative',
            name='click_through_url',
        ),
        migrations.AddField(
            model_name='creative',
            name='image_click_through_url',
            field=models.URLField(blank=True),
        ),
        migrations.RemoveField(
            model_name='creative',
            name='label',
        ),
        migrations.AlterField(
            model_name='creative',
            name='labels',
            field=django.contrib.postgres.fields.ArrayField(base_field=models.CharField(max_length=256), blank=True, default=list, size=None),
        ),
        migrations.RunPython(
            code=creative_populate_labels,
            reverse_code=migrations.RunPython.noop,
        ),
        migrations.AlterField(
            model_name='lineitem',
            name='labels',
            field=django.contrib.postgres.fields.ArrayField(base_field=models.CharField(max_length=256), blank=True, default=list, size=None),
        ),
        migrations.RunPython(
            code=line_item_populate_labels,
            reverse_code=migrations.RunPython.noop,
        ),
    ]
