# Generated by Django 2.2.28 on 2023-12-05 11:46
from django.db import (
    migrations,
)

from m3_db_utils.consts import (
    DEFAULT_ORDER_NUMBER,
)


def init_request_statuses(apps, schema_editor):
    """Заполнение первоначальными данными DataMartRequestStatus."""
    DataMartRequestStatus = apps.get_model('edu_rdm_integration', 'DataMartRequestStatus')  # noqa: N806

    DataMartRequestStatus.objects.bulk_create([
        DataMartRequestStatus(
            key='UPLOAD_TO_BUFFER',
            value=-1,
            title='Загрузка данных в буффер',
            order_number=DEFAULT_ORDER_NUMBER,
        ),
        DataMartRequestStatus(
            key='BUFFERED',
            value=0,
            title='Запрос буфферизирован',
            order_number=DEFAULT_ORDER_NUMBER,
        ),
        DataMartRequestStatus(
            key='WAIT_FOR_OPEN_DELTA',
            value=1,
            title='Ожидает открытия дельты',
            order_number=DEFAULT_ORDER_NUMBER,
        ),
        DataMartRequestStatus(
            key='IN_PROCESSING',
            value=2,
            title='В обработке',
            order_number=DEFAULT_ORDER_NUMBER,
        ),
        DataMartRequestStatus(
            key='SUCCESSFULLY_PROCESSED',
            value=3,
            title='Успешно обработан',
            order_number=DEFAULT_ORDER_NUMBER,
        ),
        DataMartRequestStatus(
            key='FAILED_PROCESSING',
            value=4,
            title='Ошибка обработки запроса',
            order_number=DEFAULT_ORDER_NUMBER,
        ),
        DataMartRequestStatus(
            key='REQUEST_ID_NOT_FOUND',
            value=5,
            title='Идентификатор запроса не обнаружен',
            order_number=DEFAULT_ORDER_NUMBER,
        ),
        DataMartRequestStatus(
            key='FORMAT_LOGICAL_CONTROL',
            value=6,
            title='Форматно-логический контроль',
            order_number=DEFAULT_ORDER_NUMBER,
        ),
        DataMartRequestStatus(
            key='FLC_ERROR',
            value=7,
            title='Ошибки ФЛК',
            order_number=DEFAULT_ORDER_NUMBER,
        ),
    ], ignore_conflicts=True)


def migrate_upload_status_to_request_status(apps, schema_editor):
    """Переносит значения из поля status в request_status модели UploadStatusRequestLog."""
    UploadStatusRequestLog = apps.get_model('edu_rdm_integration', 'UploadStatusRequestLog')  # noqa: N806
    DataMartRequestStatus = apps.get_model('edu_rdm_integration', 'DataMartRequestStatus')  # noqa: N806

    request_status_value_to_enum_key = dict(DataMartRequestStatus.objects.values_list('value', 'key'))

    for value, enum_key in request_status_value_to_enum_key.items():
        UploadStatusRequestLog.objects.filter(
            status__code=value,
        ).update(
            request_status=enum_key,
        )


class Migration(migrations.Migration):

    dependencies = [
        ('edu_rdm_integration', '0005_auto_20231204_1224'),
    ]

    operations = [
        migrations.RunPython(init_request_statuses, reverse_code=migrations.RunPython.noop),
        migrations.RunPython(migrate_upload_status_to_request_status, reverse_code=migrations.RunPython.noop)
    ]
