# Generated by Django 5.1.5 on 2025-02-07 10:07

from django.db import migrations, transaction


@transaction.atomic
def merge_notes_to_comments(apps, schema_editor):
    try:
        Segment = apps.get_model("cesnet_service_path_plugin", "Segment")
        for segment in Segment.objects.all():
            notes = []

            if segment.comments:
                notes.append(segment.comments)

            if segment.note_a:
                notes.append(f"**Note A:** {segment.note_a}")

            if segment.note_b:
                notes.append(f"**Note B:** {segment.note_b}")

            segment.comments = "\n\n___\n\n".join(notes)
            segment.save()
    except Exception as e:
        raise Exception(f"Failed to merge notes to comments: {str(e)}")


def reverse_merge_notes_to_comments(apps, schema_editor):
    # No reverse operation needed, but added for completeness
    pass


class Migration(migrations.Migration):
    dependencies = [
        ("cesnet_service_path_plugin", "0014_remove_segment_imported_data_and_more"),
    ]

    operations = [
        migrations.RunPython(
            merge_notes_to_comments,
            reverse_code=reverse_merge_notes_to_comments,
        ),
    ]
