# -*- coding: utf-8 -*-
# Generated by Django 1.11.3 on 2018-03-02 17:16
from __future__ import unicode_literals

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
import django.db.models.manager
import django.utils.timezone

import django_extensions.db.fields

from mtp.apps.common.management import create_ordered_contenttypes
import mtp.apps.common.utils
import mtp.apps.media.fields
import mtp.apps.media.managers
import mtp.apps.sync.models


def force_create_content_types(apps, schema_editor):
    """Force the creation of the content types for the ``media`` app"""

    app_config = apps.get_app_config('media')
    create_ordered_contenttypes(app_config)


def migrate_documents_to_media(apps, schema_editor):
    """Migrate the ``documents`` app models to their ``media``app equivalent"""

    Media = apps.get_model(
        'media',
        'Media'
    )

    Media.objects.migrate_documents_to_media()


def noop_backward_func(apps, schema_editor):
    """No turning back"""

    pass


class Migration(migrations.Migration):

    replaces = [
        ('media', '0001_initial'),
        ('media', '0002_pdfmedia_file_hash'),
        ('media', '0003_create_note_model'),
        ('media', '0004_change_media_to_one_to_one'),
        ('media', '0005_documents_to_media'),
        ('media', '0006_fields_wording'),
        ('media', '0007_add_timestamps')
    ]

    initial = True

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ('contenttypes', '0002_remove_content_type_name'),
        ('trainings', '0049_add_timestamps'),
        ('sync', '0003_add_index_on_update_synchornized'),
    ]

    operations = [
        migrations.CreateModel(
            name='Media',
            fields=[
                ('id', django_extensions.db.fields.UUIDField(blank=True, editable=False, primary_key=True, serialize=False)),
                ('is_deleted', models.BooleanField(default=False)),
                ('update_locally', models.BooleanField(default=False)),
            ],
            options={
                'abstract': False,
            },
            bases=(mtp.apps.sync.models.SyncableForPolymorphicMixin, models.Model),
        ),
        migrations.CreateModel(
            name='Presentation',
            fields=[
                ('trainingactivity_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='trainings.TrainingActivity')),
                ('is_accessible_by_trainees', models.BooleanField(default=True, help_text='If checked, the trainees can access the presentation by themselves', verbose_name='Is accessible by trainees')),
            ],
            options={
                'abstract': False,
            },
            bases=('trainings.trainingactivity',),
            managers=[
                ('objects', django.db.models.manager.Manager()),
                ('dependency_objects', django.db.models.manager.Manager()),
            ],
        ),
        migrations.CreateModel(
            name='PresentationMedia',
            fields=[
                ('id', django_extensions.db.fields.UUIDField(blank=True, editable=False, primary_key=True, serialize=False)),
                ('is_deleted', models.BooleanField(default=False)),
                ('update_locally', models.BooleanField(default=False)),
                ('is_restricted', models.BooleanField(default=False, verbose_name='Is restricted')),
                ('first_update', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='media_presentationmedia_by_first', to='sync.Update')),
                ('last_update', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='media_presentationmedia_by_last', to='sync.Update')),
            ],
        ),
        migrations.CreateModel(
            name='Video',
            fields=[
                ('trainingactivity_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='trainings.TrainingActivity')),
            ],
            options={
                'abstract': False,
            },
            bases=('trainings.trainingactivity',),
            managers=[
                ('objects', django.db.models.manager.Manager()),
                ('dependency_objects', django.db.models.manager.Manager()),
            ],
        ),
        migrations.CreateModel(
            name='PdfMedia',
            fields=[
                ('media_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='media.Media')),
                ('file', mtp.apps.media.fields.MimeTypeRestrictedFileField(blank=True, help_text='Only PDF (max 10Mo) are allowed.', upload_to=mtp.apps.common.utils.upload_to_generator, verbose_name='Pdf file')),
                ('file_hash', models.CharField(blank=True, max_length=36)),
            ],
            options={
                'abstract': False,
            },
            bases=('media.media',),
        ),
        migrations.CreateModel(
            name='VideoMedia',
            fields=[
                ('media_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='media.Media')),
                ('file', mtp.apps.media.fields.MimeTypeRestrictedFileField(help_text='Only MP4 files (max 200Mo) are allowed.', upload_to=mtp.apps.common.utils.upload_to_generator, verbose_name='Video file')),
            ],
            options={
                'abstract': False,
            },
            bases=('media.media',),
        ),
        migrations.AddField(
            model_name='video',
            name='media',
            field=models.OneToOneField(blank=True, on_delete=django.db.models.deletion.CASCADE, related_name='video', to='media.Media'),
        ),
        migrations.AddField(
            model_name='presentationmedia',
            name='media',
            field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='presentation_medias', to='media.Media', verbose_name='Medias'),
        ),
        migrations.AddField(
            model_name='presentationmedia',
            name='original_version',
            field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='all_duplicates', to='media.PresentationMedia'),
        ),
        migrations.AddField(
            model_name='presentationmedia',
            name='parent_version',
            field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='direct_duplicates', to='media.PresentationMedia'),
        ),
        migrations.AddField(
            model_name='presentationmedia',
            name='presentation',
            field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='presentation_medias', to='media.Presentation', verbose_name='Presentation'),
        ),
        migrations.AddField(
            model_name='presentation',
            name='medias',
            field=models.ManyToManyField(related_name='presentations', through='media.PresentationMedia', to='media.Media', verbose_name='Medias'),
        ),
        migrations.AddField(
            model_name='media',
            name='first_update',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='media_media_by_first', to='sync.Update'),
        ),
        migrations.AddField(
            model_name='media',
            name='last_update',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='media_media_by_last', to='sync.Update'),
        ),
        migrations.AddField(
            model_name='media',
            name='original_version',
            field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='all_duplicates', to='media.Media'),
        ),
        migrations.AddField(
            model_name='media',
            name='parent_version',
            field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='direct_duplicates', to='media.Media'),
        ),
        migrations.AddField(
            model_name='media',
            name='polymorphic_ctype',
            field=models.ForeignKey(editable=False, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='polymorphic_media.media_set+', to='contenttypes.ContentType'),
        ),
        migrations.AlterUniqueTogether(
            name='presentationmedia',
            unique_together=set([('media', 'presentation', 'is_restricted')]),
        ),
        migrations.CreateModel(
            name='Note',
            fields=[
                ('id', django_extensions.db.fields.UUIDField(blank=True, editable=False, primary_key=True, serialize=False)),
                ('is_deleted', models.BooleanField(default=False)),
                ('update_locally', models.BooleanField(default=False)),
                ('content', models.TextField(verbose_name='Content')),
                ('presentation_page', models.PositiveSmallIntegerField(blank=True, null=True, verbose_name='Presentation page')),
                ('first_update', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='media_note_by_first', to='sync.Update')),
                ('last_update', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='media_note_by_last', to='sync.Update')),
                ('presentation', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='notes', to='media.Presentation', verbose_name='Presentation')),
                ('training_session', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='notes', to='trainings.TrainingSession', verbose_name='Training session')),
                ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='notes', to=settings.AUTH_USER_MODEL, verbose_name='User')),
            ],
        ),
        migrations.AlterUniqueTogether(
            name='note',
            unique_together=set([('training_session', 'user', 'presentation', 'presentation_page')]),
        ),
        migrations.AlterField(
            model_name='presentation',
            name='medias',
            field=models.ManyToManyField(related_name='presentation', through='media.PresentationMedia', to='media.Media', verbose_name='Medias'),
        ),
        migrations.AlterField(
            model_name='presentationmedia',
            name='media',
            field=models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, related_name='presentation_media', to='media.Media', verbose_name='Medias'),
        ),
        migrations.AlterModelManagers(
            name='media',
            managers=[
                ('objects', mtp.apps.media.managers.DependencyMediaManager()),
            ],
        ),
        migrations.AlterField(
            model_name='presentation',
            name='is_accessible_by_trainees',
            field=models.BooleanField(default=True, verbose_name='Is available on trainee interface'),
        ),
        migrations.AddField(
            model_name='media',
            name='created_at',
            field=models.DateTimeField(blank=True, db_index=True, default=django.utils.timezone.now, editable=False, verbose_name='created at'),
        ),
        migrations.AddField(
            model_name='media',
            name='updated_at',
            field=models.DateTimeField(auto_now=True, db_index=True, verbose_name='last updated at'),
        ),
        migrations.AddField(
            model_name='presentationmedia',
            name='created_at',
            field=models.DateTimeField(blank=True, db_index=True, default=django.utils.timezone.now, editable=False, verbose_name='created at'),
        ),
        migrations.AddField(
            model_name='presentationmedia',
            name='updated_at',
            field=models.DateTimeField(auto_now=True, db_index=True, verbose_name='last updated at'),
        ),
        migrations.RunPython(
            code=force_create_content_types,
            reverse_code=noop_backward_func,
        ),
        migrations.RunPython(
            code=migrate_documents_to_media,
            reverse_code=noop_backward_func,
        ),
    ]
