# Generated by Django 2.2.9 on 2020-02-10 12:09

import django.contrib.postgres.fields.citext
import django.contrib.postgres.fields.jsonb
import django.db.models.deletion
import phonenumber_field.modelfields
from django.contrib.postgres.operations import CITextExtension
from django.db import migrations, models


class Migration(migrations.Migration):

    initial = True

    dependencies = []

    operations = [
        CITextExtension(),
        migrations.CreateModel(
            name="Commit",
            fields=[
                (
                    "id",
                    models.AutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("created_at", models.DateTimeField(auto_now=True)),
                ("source", models.TextField()),
                ("extra", django.contrib.postgres.fields.jsonb.JSONField(default=dict)),
            ],
        ),
        migrations.CreateModel(
            name="Consent",
            fields=[
                (
                    "id",
                    models.AutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("name", models.CharField(max_length=255, unique=True)),
                ("description", models.TextField()),
            ],
        ),
        migrations.CreateModel(
            name="LegalBasis",
            fields=[
                (
                    "id",
                    models.AutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("key", models.BinaryField()),
                (
                    "email",
                    django.contrib.postgres.fields.citext.CIEmailField(
                        db_index=True, max_length=254
                    ),
                ),
                (
                    "phone",
                    phonenumber_field.modelfields.PhoneNumberField(
                        db_index=True, max_length=128, region=None
                    ),
                ),
                (
                    "key_type",
                    models.TextField(choices=[("email", "Email"), ("phone", "Phone")]),
                ),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("modified_at", models.DateTimeField(auto_now=True)),
                ("current", models.BooleanField(db_index=True, default=False)),
                (
                    "commit",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.PROTECT, to="main.Commit"
                    ),
                ),
                ("consents", models.ManyToManyField(to="main.Consent")),
            ],
            options={"verbose_name_plural": "LegalBases"},
        ),
        migrations.AddIndex(
            model_name="legalbasis",
            index=models.Index(
                fields=["email", "-modified_at"], name="main_legalb_email_fc08e7_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="legalbasis",
            index=models.Index(
                fields=["phone", "-modified_at"], name="main_legalb_phone_05c39f_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="legalbasis",
            index=models.Index(
                fields=["key", "-modified_at"], name="main_legalb_key_d9887a_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="legalbasis",
            index=models.Index(
                fields=["key", "key_type", "-modified_at"],
                name="main_legalb_key_54e6ba_idx",
            ),
        ),
        migrations.AddIndex(
            model_name="legalbasis",
            index=models.Index(
                fields=["key", "current"], name="main_legalb_key_d41d2e_idx"
            ),
        ),
        migrations.AddConstraint(
            model_name="legalbasis",
            constraint=models.CheckConstraint(
                check=models.Q(
                    models.Q(("email", ""), models.Q(_negated=True, phone="")),
                    models.Q(models.Q(_negated=True, email=""), ("phone", "")),
                    _connector="OR",
                ),
                name="only_one_key",
            ),
        ),
    ]
