# -*- coding: utf-8 -*-
# Generated by Django 1.10.2 on 2018-07-06 23:23
from __future__ import unicode_literals

from django.db import migrations, models

from django.db import transaction


def extract_reward_data_migration(apps, schema_editor):
    ActionLog = apps.get_model('nurture', 'ActionLog')

    with transaction.atomic():
        for alog in ActionLog.objects.all():
            try:
                reward = float(alog.reward_state_message.split(';')[1:-1])
                alog.reward = reward
                alog.processing_status = 4  # ActionLog.STATUS_OKAY
            except:
                alog.processing_status = 1  # ActionLog.STATUS_INVALID_REWARD
            
            alog.save()

class Migration(migrations.Migration):

    dependencies = [
        ('nurture', '0005_filelog_uploaded_time'),
    ]

    operations = [
        migrations.AddField(
            model_name='actionlog',
            name='processing_status',
            field=models.IntegerField(choices=[(0, 'Request received'), (1, 'Invalid reward'), (2, 'Invalid state'), (3, 'Policy execution failure'), (4, 'Okay')], default=0),
            preserve_default=False,
        ),
        migrations.AddField(
            model_name='actionlog',
            name='reward',
            field=models.FloatField(default=0),
            preserve_default=False,
        ),
        migrations.RunPython(extract_reward_data_migration),
    ]
