# Generated by Django 5.0.10 on 2025-01-10 18:28

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


def migrate_entry_objects(apps, schema_editor):
    Entry = apps.get_model("database", "Entry")
    FileObject = apps.get_model("database", "FileObject")
    db_alias = schema_editor.connection.alias

    # Create lookup dictionary of all file objects
    file_objects_map = {(fo.user_id, fo.file_name): fo for fo in FileObject.objects.using(db_alias).all()}

    # Process entries in chunks of 1000
    chunk_size = 1000
    processed = 0

    processed_entry_ids = set()

    while True:
        entries = list(
            Entry.objects.using(db_alias)
            .select_related("user")
            .filter(file_object__isnull=True)
            .exclude(id__in=processed_entry_ids)
            .only("id", "user", "file_path")[:chunk_size]
        )

        if not entries:
            break

        processed_entry_ids.update([entry.id for entry in entries])

        entries_to_update = []
        for entry in entries:
            try:
                file_object = file_objects_map.get((entry.user_id, entry.file_path))
                if file_object:
                    entry.file_object = file_object
                    entries_to_update.append(entry)
            except Exception as e:
                print(f"Error processing entry {entry.id}: {str(e)}")
                continue

        if entries_to_update:
            Entry.objects.using(db_alias).bulk_update(entries_to_update, ["file_object"], batch_size=chunk_size)

        processed += len(entries)
        print(f"Processed {processed} entries")


def reverse_migration(apps, schema_editor):
    pass


class Migration(migrations.Migration):
    dependencies = [
        ("database", "0078_khojuser_email_verification_code_expiry"),
    ]

    operations = [
        migrations.AddField(
            model_name="entry",
            name="file_object",
            field=models.ForeignKey(
                blank=True,
                default=None,
                null=True,
                on_delete=django.db.models.deletion.CASCADE,
                to="database.fileobject",
            ),
        ),
        migrations.RunPython(migrate_entry_objects, reverse_migration),
    ]
