# Generated by Django 3.0.2 on 2020-01-19 22:47
import csv
import os

from django.db import migrations


COURSE_DATA_PATH = os.path.join(os.path.dirname(os.path.dirname(
    os.path.abspath(__file__)
)), 'course_data')

CATEGORIES = (
    ('1', 'Geology'),
    ('2', 'Geophysics'),
    ('3', 'Others'),
)


def add_course_data(apps, schema_editor):
    Course = apps.get_model('courses', 'Course')
    Lecturer = apps.get_model('people', 'Lecturer')

    for category, name in CATEGORIES:
        with open(os.path.join(COURSE_DATA_PATH, '{}.csv')
                  .format(name.lower())) as csv_file:
            csv_reader = csv.reader(csv_file)
            for row in csv_reader:
                code, title, desc, unit, lecturers = row

                new_lecturers = []
                if lecturers:
                    lecturers = lecturers.split(' & ')
                    lecturers = \
                        [lecturer.split('. ') for lecturer in lecturers]
                    lecturers = [[n[0], *n[1].split(' ')] for n in lecturers]

                    for lecturer in lecturers:
                        lect_title = lecturer[0]
                        last_name = lecturer[1]
                        try:
                            first_name = lecturer[2]
                        except IndexError:
                            first_name = ''

                        new_lecturer, _ = Lecturer.objects.get_or_create(
                            title=lect_title,
                            last_name=last_name,
                            first_name=first_name,
                        )

                        new_lecturers.append(new_lecturer)

                new_course = Course.objects.create(
                    code=code,
                    title=title,
                    description=desc,
                    units=unit,
                    category=category
                )

                if new_lecturers:
                    new_course.lecturers.add(*new_lecturers)


def remove_course_data(apps, schema_editor):
    Course = apps.get_model('courses', 'Course')
    Lecturer = apps.get_model('people', 'Lecturer')

    for category, name in CATEGORIES:
        with open(os.path.join(COURSE_DATA_PATH, '{}.csv')
                  .format(name.lower())) as csv_file:
            csv_reader = csv.reader(csv_file)
            for row in csv_reader:
                code = row[0]
                Course.objects.get(code=code).delete()

                lecturers = row[4]
                if lecturers:
                    lecturers = lecturers.split(' & ')
                    lecturers =\
                        [lecturer.split('. ') for lecturer in lecturers]
                    lecturers = [[n[0], *n[1].split(' ')] for n in lecturers]

                for lecturer in lecturers:
                    title = lecturer[0]
                    last_name = lecturer[1]
                    try:
                        first_name = lecturer[2]
                    except IndexError:
                        first_name = ''
                    lect = Lecturer.objects.filter(
                        title=title,
                        first_name=first_name,
                        last_name=last_name,
                    )
                    lect.delete()


class Migration(migrations.Migration):

    dependencies = [
        ('courses', '0004_auto_20200118_1957'),
        ('people', '0003_remove_lecturer_course'),
    ]

    operations = [
        migrations.RunPython(
            add_course_data,
            remove_course_data
        )
    ]
