# -*- coding: utf-8 -*-
# Generated by Django 1.10.5 on 2017-03-21 11:33
from __future__ import unicode_literals

from django.contrib.auth import get_user_model
from django.db import migrations
from django.db.models import Value

from pootle_statistics.models import SubmissionTypes


def _add_comment_attrib(values, unit_changes, units_with_change, no_user=False):
    system_user = get_user_model().objects.get_system_user()
    existing_change = values.filter(id__in=units_with_change)
    no_existing_change = values.exclude(id__in=units_with_change)
    updated = 0
    for res in existing_change.iterator():
        if no_user:
            unit, timestamp = res
            user = system_user.id
        else:
            unit, timestamp, user = res
        updated = unit_changes.filter(unit_id=unit).update(
            commented_by_id=user, commented_on=timestamp)
    if no_user:
        return (
            updated,
            len(unit_changes.bulk_create(
                [unit_changes.model(
                    unit_id=unit,
                    changed_with=SubmissionTypes.SYSTEM,
                    commented_by_id=system_user.id,
                    commented_on=timestamp)
                 for unit, timestamp
                 in no_existing_change.iterator()])))
    else:
        return (
            updated,
            len(unit_changes.bulk_create(
                [unit_changes.model(
                    unit_id=unit,
                    changed_with=(
                        SubmissionTypes.SYSTEM
                        if user == system_user
                        else SubmissionTypes.WEB),
                    commented_by_id=user,
                    commented_on=timestamp)
                 for unit, timestamp, user
                 in no_existing_change.iterator()])))


def set_change_commented(apps, schema_editor):
    system_user = get_user_model().objects.get_system_user()
    units = apps.get_model("pootle_store.Unit").objects.all()
    unit_changes = apps.get_model("pootle_store.UnitChange").objects.all()
    units_with_comments = units.filter(translator_comment__gt="")
    # units with an existing change obj - not so many at this stage
    units_with_change = dict(units.filter(change__isnull=False).values_list("id", "change"))

    # units that have commented_by and commented_on
    commented = units_with_comments.filter(
        commented_on__isnull=False).filter(commented_by__isnull=False)
    _add_comment_attrib(
        commented.values_list("id", "commented_on", "commented_by"),
        unit_changes,
        units_with_change)

    # units that have comments and no attribution at all - use system/mtime
    no_attrib = units_with_comments.filter(
        commented_on__isnull=True).filter(commented_by__isnull=True)
    _add_comment_attrib(
        no_attrib.values_list("id", "mtime"),
        unit_changes,
        units_with_change,
        no_user=True)

    # units that have commented_by but no comment time - use commenter/mtime
    no_commented_on = units_with_comments.filter(
        commented_on__isnull=True).filter(commented_by__isnull=False)
    _add_comment_attrib(
        no_commented_on.values_list("id", "mtime", "commented_by"),
        unit_changes,
        units_with_change)

    # units that have commented_on but no commenter - use system/comment_time
    no_commented_by = units_with_comments.filter(
        commented_on__isnull=False).filter(commented_by__isnull=True)
    _add_comment_attrib(
        no_commented_by.values_list("id", "commented_on"),
        unit_changes,
        units_with_change,
        no_user=True)



class Migration(migrations.Migration):

    dependencies = [
        ('pootle_store', '0047_remove_old_unit_fields'),
    ]

    operations = [
        migrations.RunPython(set_change_commented),
    ]
