# Generated by Django 5.1.7 on 2025-03-26 16:04

import django.db.models.deletion
from django.db import migrations, models


def migrate_layout(apps, schema_editor):
    LibraryItem = apps.get_model('library', 'LibraryItem')
    Operation = apps.get_model('oss', 'Operation')
    Layout = apps.get_model('oss', 'Layout')

    for library_item in LibraryItem.objects.filter(item_type='oss'):
        layout_data = {'operations': [], 'blocks': []}

        operations = Operation.objects.filter(oss=library_item)
        for operation in operations:
            layout_data['operations'].append({
                'id': operation.id,
                'x': operation.position_x,
                'y': operation.position_y
            })

        Layout.objects.create(oss=library_item, data=layout_data)


class Migration(migrations.Migration):

    dependencies = [
        ('library', '0007_rename_libraryitem_comment_libraryitem_description'),
        ('oss', '0010_rename_comment_operation_description'),
    ]

    operations = [
        migrations.CreateModel(
            name='Layout',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('data', models.JSONField(default=dict, verbose_name='Расположение')),
                ('oss', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE,
                 related_name='layout', to='library.libraryitem', verbose_name='Схема синтеза')),
            ],
            options={
                'verbose_name': 'Схема расположения',
                'verbose_name_plural': 'Схемы расположения',
            },
        ),
        migrations.RunPython(migrate_layout),
        migrations.RemoveField(
            model_name='operation',
            name='position_x',
        ),
        migrations.RemoveField(
            model_name='operation',
            name='position_y',
        ),

        migrations.CreateModel(
            name='Block',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('title', models.TextField(blank=True, verbose_name='Название')),
                ('description', models.TextField(blank=True, verbose_name='Описание')),
                ('oss', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE,
                 related_name='blocks', to='library.libraryitem', verbose_name='Схема синтеза')),
                ('parent', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL,
                 related_name='as_child_block', to='oss.block', verbose_name='Содержащий блок')),
            ],
            options={
                'verbose_name': 'Блок',
                'verbose_name_plural': 'Блоки',
            },
        ),
        migrations.AddField(
            model_name='operation',
            name='parent',
            field=models.ForeignKey(
                blank=True,
                null=True,
                on_delete=django.db.models.deletion.SET_NULL,
                related_name='as_child_operation',
                to='oss.block',
                verbose_name='Содержащий блок'),
        ),
    ]
