# -*- coding: utf-8 -*-
# Generated by Django 1.11.24 on 2019-09-18 12:39
from __future__ import unicode_literals

from django.db import migrations


def create_pipeline_v5(apps, schema_editor):
    Pipeline = apps.get_model("emgapi", "Pipeline")

    obj, created = Pipeline.objects.get_or_create(pipeline_id=6,
                                                  description='Release of version 5.0',
                                                  changes='Major upgrade.',
                                                  release_version='5.0',
                                                  release_date='2019-11-06')


def create_group_types(apps, schema_editor):
    DownloadGroupType = apps.get_model("emgapi", "DownloadGroupType")
    group_types = (
        "Taxonomic analysis ITS",
        "Taxonomic analysis ITSoneDB",
        "Taxonomic analysis UNITE",
        "Pathways and Systems",
        # TODO: Do we need sub groups for the function and pathways
    )
    _groups = list()
    for group_type in group_types:
        _groups.append(
            DownloadGroupType(group_type=group_type)
        )
    DownloadGroupType.objects.bulk_create(_groups)


def create_subdirs(apps, schema_editor):
    DownloadSubdir = apps.get_model("emgapi", "DownloadSubdir")
    subdirs = (
        "version_5.0/project-summary",
        "taxonomy-summary/its",
        "taxonomy-summary/its/itsonedb",
        "taxonomy-summary/its/unite",
    )
    _subdirs = list()
    for subdir in subdirs:
        _subdirs.append(
            DownloadSubdir(
                subdir=subdir
            )
        )
    DownloadSubdir.objects.bulk_create(_subdirs)


def create_download_description(apps, schema_editor):
    DownloadDescriptionLabel = apps.get_model("emgapi", "DownloadDescriptionLabel")
    downloads = (
        ("All reads encoding 5.8S", "Reads encoding 5.8S"),
        ("All reads encoding 5S", "Reads encoding 5S"),
        ("All contigs encoding 5.8S", "Contigs encoding 5.8S"),
        ("All contigs encoding 5S", "Contigs encoding 5S"),
        ("All reads encoding ITS", "Reads encoding ITS (with SSU and LSU sequences masked)"),
        ("Taxonomic assignments (TSV)", "MAPseq ITSoneDB assignments"),
        ("Taxonomic assignments (TSV)", "MAPseq UNITE assignments"),
        ("OTUs and taxonomic assignments", "OTUs, counts and taxonomic assignments for ITSOneDB"),
        ("OTUs and taxonomic assignments", "OTUs, counts and taxonomic assignments for UNITE"),
        ("Complete Pfam annotation", "Pfam annotation"),
        ("KEGG orthologues annotation", "KEGG orthologues annotation"),
        ("Genome Properties annotation", "Genome Properties annotation"),
        ("antiSMASH annotation", "antiSMASH annotation"),
        ("Diamond annotation", "Diamond annotation"),
        ("KEGG pathway annotation", "KEGG pathway annotation"),
        ("All predicted ORF", "Predicted ORF"),
    )
    _downloads = list()
    for d in downloads:
        _downloads.append(
            DownloadDescriptionLabel(
                description=d[0],
                description_label=d[1]
            )
        )
    DownloadDescriptionLabel.objects.bulk_create(_downloads)
    DownloadDescriptionLabel.objects.filter(
        description="Protein sequence FASTA file of the species representative").delete()


def create_fileformats(apps, schema_editor):
    FileFormat = apps.get_model("emgapi", "FileFormat")
    file_formats = (
        ("EMBL", "embl", True),
        ("GenBank", "gbk", True),
    )
    _formats = list()
    for file_format in file_formats:
        _formats.append(
            FileFormat(
                format_name=file_format[0],
                format_extension=file_format[1],
                compression=file_format[2],
            )
        )
    FileFormat.objects.bulk_create(_formats)


class Migration(migrations.Migration):
    dependencies = [
        ('emgapi', '0016_auto_20190913_1512'),
    ]

    operations = [
        migrations.RunPython(create_pipeline_v5),
        migrations.RunPython(create_group_types),
        migrations.RunPython(create_subdirs),
        migrations.RunPython(create_download_description),
        migrations.RunPython(create_fileformats),
    ]
