# Generated by Django 3.1.7 on 2021-04-07 20:00
import annoying.fields
from django.conf import settings
from django.contrib.postgres.indexes import GinIndex
from django.contrib.postgres.search import SearchVector, SearchVectorField
from django.db import migrations, models
from django.db.models import deletion
from django.utils import translation
from django_scopes import scopes_disabled

from cookbook.managers import DICTIONARY
from cookbook.models import (Index, PermissionModelMixin, Recipe, Step, SearchFields)


def allSearchFields():
    return list(SearchFields.objects.values_list('id', flat=True))


def nameSearchField():
    return [SearchFields.objects.get(name='Name').id]


def set_default_search_vector(apps, schema_editor):
    if settings.DATABASES['default']['ENGINE'] not in ['django.db.backends.postgresql_psycopg2', 'django.db.backends.postgresql']:
        return
    language = DICTIONARY.get(translation.get_language(), 'simple')
    with scopes_disabled():
        Recipe.objects.all().update(
            name_search_vector=SearchVector('name__unaccent', weight='A', config=language),
            desc_search_vector=SearchVector('description__unaccent', weight='B', config=language)
        )
        Step.objects.all().update(search_vector=SearchVector('instruction__unaccent', weight='B'))


class Migration(migrations.Migration):
    dependencies = [
        ('cookbook', '0142_alter_userpreference_search_style'),
    ]
    operations = [
        migrations.AddField(
            model_name='recipe',
            name='desc_search_vector',
            field=SearchVectorField(null=True),
        ),
        migrations.AddField(
            model_name='recipe',
            name='name_search_vector',
            field=SearchVectorField(null=True),
        ),
        migrations.AddIndex(
            model_name='recipe',
            index=GinIndex(fields=['name_search_vector', 'desc_search_vector'], name='cookbook_re_name_se_bdf3ca_gin'),
        ),
        migrations.AddField(
            model_name='step',
            name='search_vector',
            field=SearchVectorField(null=True),
        ),
        migrations.AddIndex(
            model_name='step',
            index=GinIndex(fields=['search_vector'], name='cookbook_st_search__2ef7fa_gin'),
        ),
        migrations.AddIndex(
            model_name='cooklog',
            index=Index(fields=['id', 'recipe', '-created_at', 'rating'], name='cookbook_co_id_37485a_idx'),
        ),
        migrations.AddIndex(
            model_name='food',
            index=Index(fields=['id', 'name'], name='cookbook_fo_id_22b733_idx'),
        ),
        migrations.AddIndex(
            model_name='ingredient',
            index=Index(fields=['id', 'food', 'unit'], name='cookbook_in_id_3368be_idx'),
        ),
        migrations.AddIndex(
            model_name='keyword',
            index=Index(fields=['id', 'name'], name='cookbook_ke_id_ebc03f_idx'),
        ),
        migrations.AddIndex(
            model_name='recipe',
            index=Index(fields=['id', 'name', 'description'], name='cookbook_re_id_e4c2d4_idx'),
        ),
        migrations.AddIndex(
            model_name='recipebook',
            index=Index(fields=['name', 'description'], name='cookbook_re_name_bbe446_idx'),
        ),
        migrations.AddIndex(
            model_name='viewlog',
            index=Index(fields=['recipe', '-created_at'], name='cookbook_vi_recipe__5cd178_idx'),
        ),
        migrations.CreateModel(
            name='SearchFields',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('name', models.CharField(max_length=32, unique=True)),
                ('field', models.CharField(max_length=64, unique=True)),
            ],
            bases=(models.Model, PermissionModelMixin),
        ),
        migrations.CreateModel(
            name='SearchPreference',
            fields=[
                ('user', annoying.fields.AutoOneToOneField(on_delete=deletion.CASCADE, primary_key=True, serialize=False, to='auth.user')),
                ('search', models.CharField(choices=[('plain', 'Simple'), ('phrase', 'Phrase'), ('websearch', 'Web'), ('raw', 'Raw')], default='plain', max_length=32)),
                ('lookup', models.BooleanField(default=False)),
                ('fulltext', models.ManyToManyField(blank=True, related_name='fulltext_fields', to='cookbook.SearchFields')),
                ('icontains', models.ManyToManyField(blank=True, default=nameSearchField, related_name='icontains_fields', to='cookbook.SearchFields')),
                ('istartswith', models.ManyToManyField(blank=True, related_name='istartswith_fields', to='cookbook.SearchFields')),
                ('trigram', models.ManyToManyField(blank=True, related_name='trigram_fields', to='cookbook.SearchFields')),
                ('unaccent', models.ManyToManyField(blank=True, default=allSearchFields, related_name='unaccent_fields', to='cookbook.SearchFields')),
            ],
            bases=(models.Model, PermissionModelMixin),
        ),
        migrations.RunPython(
            set_default_search_vector
        ),
    ]
