# Generated by Django 4.2.20 on 2025-06-10 05:57

import uuid

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

from opencontractserver.shared.utils import calc_oc_file_path


def calculate_description_filepath(instance, filename):
    """Inlined upload_to callable for the historical md_description FileField.

    Originally lived as ``opencontractserver.corpuses.models.calculate_description_filepath``;
    the helper and the field it served were removed in migration 0053
    (Canonical-CAML Description Refactor). We inline the callable here so this
    historical migration remains importable on fresh installs even after the
    runtime model code drops it.
    """
    return calc_oc_file_path(
        instance,
        filename,
        f"user_{instance.creator.id}/{instance.__class__.__name__}/md_descriptions/{uuid.uuid4()}",
    )


class Migration(migrations.Migration):

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ("corpuses", "0017_alter_corpus_preferred_embedder"),
    ]

    operations = [
        migrations.AddField(
            model_name="corpus",
            name="md_description",
            field=models.FileField(
                blank=True,
                help_text="Markdown description file for this corpus.",
                null=True,
                upload_to=calculate_description_filepath,
            ),
        ),
        migrations.CreateModel(
            name="CorpusDescriptionRevision",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("version", models.PositiveIntegerField()),
                ("diff", models.TextField(blank=True)),
                ("snapshot", models.TextField(blank=True, null=True)),
                ("checksum_base", models.CharField(blank=True, max_length=64)),
                ("checksum_full", models.CharField(blank=True, max_length=64)),
                (
                    "created",
                    models.DateTimeField(
                        db_index=True, default=django.utils.timezone.now
                    ),
                ),
                (
                    "author",
                    models.ForeignKey(
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="corpus_revisions",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
                (
                    "corpus",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="revisions",
                        to="corpuses.corpus",
                    ),
                ),
            ],
            options={
                "ordering": ("corpus_id", "version"),
                "indexes": [
                    models.Index(
                        fields=["corpus"], name="corpuses_co_corpus__dc1049_idx"
                    ),
                    models.Index(
                        fields=["author"], name="corpuses_co_author__fe4c23_idx"
                    ),
                    models.Index(
                        fields=["created"], name="corpuses_co_created_881ee0_idx"
                    ),
                ],
                "unique_together": {("corpus", "version")},
            },
        ),
    ]
