# Generated by Django 4.2.24 on 2026-01-24 19:46

from django.db import migrations, models


def backfill_processing_status(apps, schema_editor):
    """
    Backfill processing_status based on existing document state.

    Logic:
    - Documents with processing_finished + parsed content -> COMPLETED
    - Documents with processing_finished + no content -> FAILED
    - Documents with processing_started only (no finished) -> PROCESSING
    - Rest -> PENDING (default, no update needed)
    """
    Document = apps.get_model("documents", "Document")

    # STEP 1: Mark PDF documents with PAWLS content as COMPLETED
    # FileFields can be either NULL or empty string ("") when empty, so we check both.
    # This query catches documents that have actual PAWLS parse data (PDFs).
    Document.objects.filter(
        processing_finished__isnull=False, pawls_parse_file__isnull=False
    ).exclude(pawls_parse_file="").update(processing_status="completed")

    # STEP 2: Mark text-only documents as COMPLETED
    # Text files (txt, md, etc.) don't produce PAWLS data - they only have txt_extract_file.
    # This catches documents where pawls_parse_file is empty string but txt_extract exists.
    # Note: Documents with actual PAWLS content were already marked COMPLETED in step 1,
    # so this won't create duplicates.
    Document.objects.filter(
        processing_finished__isnull=False,
        pawls_parse_file="",  # Empty string means no PAWLS (text file)
        txt_extract_file__isnull=False,
    ).exclude(txt_extract_file="").update(processing_status="completed")

    # STEP 3: Mark documents with processing_finished but no content as FAILED
    # These are legacy documents where processing "completed" but produced no output.
    # We check processing_status="pending" to avoid overwriting documents already
    # marked as COMPLETED in steps 1-2.
    Document.objects.filter(
        processing_finished__isnull=False,
        processing_status="pending",  # Not yet marked as completed
    ).filter(
        models.Q(pawls_parse_file__isnull=True) | models.Q(pawls_parse_file=""),
        models.Q(txt_extract_file__isnull=True) | models.Q(txt_extract_file=""),
    ).update(
        processing_status="failed",
        processing_error="Processing completed but produced no output (legacy document)",
    )

    # STEP 4: Mark documents currently being processed as PROCESSING
    # These have processing_started but no processing_finished timestamp.
    Document.objects.filter(
        processing_started__isnull=False,
        processing_finished__isnull=True,
        processing_status="pending",  # Still pending
    ).update(processing_status="processing")


def reverse_backfill(apps, schema_editor):
    """
    Reverse the backfill - reset all to pending.
    This is a data-lossy operation but allows rollback.
    """
    Document = apps.get_model("documents", "Document")
    Document.objects.update(processing_status="pending")


class Migration(migrations.Migration):

    dependencies = [
        (
            "documents",
            "0029_alter_documentrelationshipuserobjectpermission_unique_together_and_more",
        ),
    ]

    operations = [
        migrations.AddField(
            model_name="document",
            name="processing_error",
            field=models.TextField(
                blank=True, default="", help_text="Error message if processing failed"
            ),
        ),
        migrations.AddField(
            model_name="document",
            name="processing_error_traceback",
            field=models.TextField(
                blank=True, default="", help_text="Full traceback if processing failed"
            ),
        ),
        migrations.AddField(
            model_name="document",
            name="processing_status",
            field=models.CharField(
                choices=[
                    ("pending", "Pending"),
                    ("processing", "Processing"),
                    ("completed", "Completed"),
                    ("failed", "Failed"),
                ],
                db_index=True,
                default="pending",
                help_text="Current processing status of the document in the parsing pipeline",
                max_length=20,
            ),
        ),
        # Data migration: backfill processing_status based on existing state
        migrations.RunPython(backfill_processing_status, reverse_backfill),
    ]
