# -*- coding: utf-8 -*-
# Generated by Django 1.11.5 on 2017-10-09 03:20
from __future__ import unicode_literals
import numpy as np

from django.db import migrations, models


def populate_avg_times(apps, schema_editor):
    UserInfo = apps.get_model("examples", "UserInfo")
    # Use a fixed seed for generate content
    np.random.seed(123456)
    # Generate the clusters (from previous migrations total_size is 200)
    y0 = np.random.multivariate_normal([20, 20], [[2, 0], [0, 0.1]],
                                       size=50)
    y1 = np.random.multivariate_normal([20, 20], [[0.1, 0], [0, 2]],
                                       size=50)
    y2 = np.random.multivariate_normal([25, 25], [[2, -1.5], [-1.5, 2]],
                                       size=50)
    y3 = np.random.multivariate_normal([16, 16], [[0.5, 0], [0, 0.5]],
                                       size=50)
    y = np.vstack([y0, y1, y2, y3])
    uis = UserInfo.objects.all()
    # Update the objects in the Model
    for index, ui in enumerate(uis):
        ui.avg_time_pages_a = y[index][0]
        ui.avg_time_logged = y[index][1]
        ui.save(update_fields=['avg_time_logged', 'avg_time_pages_a'])


def unpopulate_avg_times(apps, schema_editor):
    """
    This is for making the migration reversible, it doesn't do anything
    because the fields will be removed after by the reverse of AddField.
    """
    pass


class Migration(migrations.Migration):

    dependencies = [
        ('examples', '0004_bn_example'),
    ]

    operations = [
        migrations.AddField(
            model_name='userinfo',
            name='avg_time_pages_a',
            field=models.FloatField(blank=True, null=True,
                            verbose_name='Average Time spent on Pages A'),
        ),
        migrations.AddField(
            model_name='userinfo',
            name='avg_time_logged',
            field=models.FloatField(blank=True, null=True,
                            verbose_name='Average Weekly Time Logged In'),
        ),
        migrations.RunPython(populate_avg_times, unpopulate_avg_times),
    ]
