# Generated by Django 2.2 on 2020-02-09 16:44

from django.db import migrations, models
import django.db.models.deletion
import django.utils.timezone
from django.utils.dateparse import parse_time, parse_datetime
from django.utils.timezone import get_current_timezone

def populate(apps, schema_editor):
    tz = get_current_timezone()

    output_device_model = apps.get_model('crop', 'OutputDevice')
    output_device_task_model = apps.get_model('crop', 'OutputDeviceScheduledTask')
    crop_model = apps.get_model('crop', 'Crop')
    plan_model = apps.get_model('crop', 'GrowthPlan')
    tray_model = apps.get_model('crop', 'Tray')

    device1 = output_device_model.objects.create(
        name='LED Light',
        device_type=1,
        units=2,
        units_per_second=1
    )

    device2 = output_device_model.objects.create(
        name='Water sprayer',
        device_type=2,
        units=1,
        units_per_second=1
    )

    device_task = output_device_task_model.objects.create(
        output_device=device1,
        start_time=parse_time('16:00:00'),
        end_time=parse_time('18:00:00')
    )

    device_task = output_device_task_model.objects.create(
        output_device=device2,
        start_time=parse_time('12:00:00'),
        end_time=parse_time('18:00:00')
    )

    crop = crop_model.objects.create(
        name='New Basil', family='Basil'
    )

    plan = plan_model.objects.create(
        crop=crop, name='Micro', growth_duration=8, est_yield=80
    )

    plan.output_devices.set([device1, device2])

    tray_model.objects.create(
        crop=crop,
        growth_plan=plan,
        sow_date=tz.localize(parse_datetime('2020-1-22T12:00:00')),
        harvest_date=tz.localize(parse_datetime('2020-1-29T12:00:00'))
    )

class Migration(migrations.Migration):
    dependencies = [
        ('crop', '0009_auto_20200209_1644'),
    ]

    operations = [
        migrations.RunPython(populate),
    ]
