# Generated by Django 5.2.14 on 2026-05-18 14:16

import common.validators
import InvenTree.models
import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ("common", "0045_projectcode_active"),
        ("contenttypes", "0002_remove_content_type_name"),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name="Note",
            fields=[
                (
                    "id",
                    models.AutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "metadata",
                    models.JSONField(
                        blank=True,
                        help_text="JSON metadata field, for use by external plugins",
                        null=True,
                        verbose_name="Plugin Metadata",
                    ),
                ),
                (
                    "updated",
                    models.DateTimeField(
                        blank=True,
                        default=None,
                        help_text="Timestamp of last update",
                        null=True,
                        verbose_name="Updated",
                    ),
                ),
                ("model_id", models.PositiveIntegerField(
                    blank=True,
                    null=True,
                    help_text="Target model instance ID for this note",
                )),
                (
                    "title",
                    models.CharField(
                        help_text="Note title", max_length=100, verbose_name="Title",
                    ),
                ),
                (
                    "description",
                    models.CharField(
                        blank=True,
                        help_text="Optional description field",
                        max_length=250,
                        verbose_name="Description",
                    ),
                ),
                (
                    "content",
                    models.TextField(
                        blank=True, help_text="Note content", verbose_name="Content", max_length=50000
                    ),
                ),
                (
                    "model_type",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        to="contenttypes.contenttype",
                        help_text="Target model type for this note",
                        blank=True,
                        null=True,
                    ),
                ),
                (
                    "template",
                    models.BooleanField(
                        default=False,
                        help_text="Is this note a template (not linked to a specific model instance)?",
                        verbose_name="Template",
                    ),
                ),
                (
                    "updated_by",
                    models.ForeignKey(
                        blank=True,
                        help_text="User who last updated this object",
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="%(class)s_updated",
                        to=settings.AUTH_USER_MODEL,
                        verbose_name="Update By",
                    ),
                ),
                (
                    "primary",
                    models.BooleanField(
                        default=False,
                        help_text="Is this the primary note for the associated model?",
                        verbose_name="Primary",
                    ),
                )
            ],
            options={
                "verbose_name": "Note",
                "verbose_name_plural": "Notes",
            },
            bases=(
                InvenTree.models.ContentTypeMixin,
                InvenTree.models.PluginValidationMixin,
                models.Model,
            ),
        ),
        # Once the 'Note' model has been created, we can add the foreign key to the 'NotesImage' model
        # This will (initially) allow null values, so that existing images are not affected
        # After the data migration, we will come back and mark this field as non-nullable
        migrations.AddField(
            model_name="notesimage",
            name="note",
            field=models.ForeignKey(
                blank=True,
                null=True,
                on_delete=django.db.models.deletion.CASCADE,
                to="common.note",
                related_name='images',
            ),
        ),
        # Add constraint to ensure that only one 'primary' note exists per model instance
        migrations.AddConstraint(
            model_name="note",
            constraint=models.UniqueConstraint(
                condition=models.Q(("primary", True), ("template", False)),
                fields=("model_type", "model_id"),
                name="unique_primary_note_per_model",
            ),
        ),
    ]
