# Generated by Django 6.0 on 2025-12-25 09:34

import archivebox.base_models.models
import django.db.models.deletion
import django.utils.timezone
from archivebox import uuid_compat
from django.conf import settings
from django.db import migrations, models


def populate_archiveresult_uuids(apps, schema_editor):
    """Generate unique UUIDs for ArchiveResults that don't have one."""
    ArchiveResult = apps.get_model('core', 'ArchiveResult')
    for result in ArchiveResult.objects.filter(uuid__isnull=True):
        result.uuid = uuid_compat.uuid7()
        result.save(update_fields=['uuid'])


def reverse_populate_uuids(apps, schema_editor):
    """Reverse migration - do nothing, UUIDs can stay."""
    pass


class Migration(migrations.Migration):

    dependencies = [
        ('core', '0025_allow_duplicate_urls_per_crawl'),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        # FIRST: Populate UUIDs for existing NULL rows BEFORE any schema changes
        migrations.RunPython(populate_archiveresult_uuids, reverse_populate_uuids),

        # Remove output_dir fields (not needed, computed from snapshot)
        migrations.RemoveField(
            model_name='archiveresult',
            name='output_dir',
        ),
        migrations.RemoveField(
            model_name='snapshot',
            name='output_dir',
        ),

        # Archiveresult field alterations
        migrations.AlterField(
            model_name='archiveresult',
            name='created_at',
            field=models.DateTimeField(db_index=True, default=django.utils.timezone.now),
        ),
        migrations.AlterField(
            model_name='archiveresult',
            name='created_by',
            field=models.ForeignKey(default=None, on_delete=django.db.models.deletion.CASCADE, related_name='archiveresult_set', to=settings.AUTH_USER_MODEL),
        ),
        migrations.AlterField(
            model_name='archiveresult',
            name='extractor',
            field=models.CharField(db_index=True, max_length=32),
        ),
        migrations.AlterField(
            model_name='archiveresult',
            name='id',
            field=models.AutoField(editable=False, primary_key=True, serialize=False),
        ),
        migrations.AlterField(
            model_name='archiveresult',
            name='status',
            field=models.CharField(choices=[('queued', 'Queued'), ('started', 'Started'), ('backoff', 'Waiting to retry'), ('succeeded', 'Succeeded'), ('failed', 'Failed'), ('skipped', 'Skipped')], db_index=True, default='queued', max_length=15),
        ),

        # Snapshot field alterations
        migrations.AlterField(
            model_name='snapshot',
            name='bookmarked_at',
            field=models.DateTimeField(db_index=True, default=django.utils.timezone.now),
        ),
        migrations.AlterField(
            model_name='snapshot',
            name='created_at',
            field=models.DateTimeField(db_index=True, default=django.utils.timezone.now),
        ),
        migrations.AlterField(
            model_name='snapshot',
            name='created_by',
            field=models.ForeignKey(default=None, on_delete=django.db.models.deletion.CASCADE, related_name='snapshot_set', to=settings.AUTH_USER_MODEL),
        ),
        migrations.AlterField(
            model_name='snapshot',
            name='downloaded_at',
            field=models.DateTimeField(blank=True, db_index=True, default=None, editable=False, null=True),
        ),
        migrations.AlterField(
            model_name='snapshot',
            name='id',
            field=models.UUIDField(default=uuid_compat.uuid7, editable=False, primary_key=True, serialize=False, unique=True),
        ),

        # SnapshotTag and Tag alterations
        migrations.AlterField(
            model_name='snapshottag',
            name='id',
            field=models.AutoField(primary_key=True, serialize=False),
        ),
        migrations.AlterField(
            model_name='tag',
            name='created_by',
            field=models.ForeignKey(default=archivebox.base_models.models.get_or_create_system_user_pk, on_delete=django.db.models.deletion.CASCADE, related_name='tag_set', to=settings.AUTH_USER_MODEL),
        ),
        migrations.AlterUniqueTogether(
            name='snapshottag',
            unique_together={('snapshot', 'tag')},
        ),
    ]
