# Generated by Django 4.2.8 on 2025-11-08 17:51

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
import django.utils.timezone
import django_lifecycle.mixins
import model_utils.fields
import uuid


class Migration(migrations.Migration):

    initial = True

    dependencies = [
        ("contenttypes", "0002_remove_content_type_name"),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ("moderation", "0001_initial"),
    ]

    operations = [
        migrations.CreateModel(
            name="EntityVersion",
            fields=[
                (
                    "created",
                    model_utils.fields.AutoCreatedField(
                        default=django.utils.timezone.now,
                        editable=False,
                        verbose_name="created",
                    ),
                ),
                (
                    "modified",
                    model_utils.fields.AutoLastModifiedField(
                        default=django.utils.timezone.now,
                        editable=False,
                        verbose_name="modified",
                    ),
                ),
                (
                    "id",
                    models.UUIDField(
                        default=uuid.uuid4,
                        editable=False,
                        primary_key=True,
                        serialize=False,
                    ),
                ),
                (
                    "entity_id",
                    models.UUIDField(db_index=True, help_text="ID of the entity"),
                ),
                (
                    "version_number",
                    models.PositiveIntegerField(
                        default=1, help_text="Sequential version number for this entity"
                    ),
                ),
                (
                    "change_type",
                    models.CharField(
                        choices=[
                            ("created", "Created"),
                            ("updated", "Updated"),
                            ("deleted", "Deleted"),
                            ("restored", "Restored"),
                        ],
                        db_index=True,
                        help_text="Type of change",
                        max_length=20,
                    ),
                ),
                (
                    "snapshot",
                    models.JSONField(
                        help_text="Complete snapshot of entity state as JSON"
                    ),
                ),
                (
                    "changed_fields",
                    models.JSONField(
                        default=dict,
                        help_text="Dict of changed fields with old/new values: {'field': {'old': ..., 'new': ...}}",
                    ),
                ),
                (
                    "comment",
                    models.TextField(
                        blank=True, help_text="Optional comment about this version"
                    ),
                ),
                (
                    "ip_address",
                    models.GenericIPAddressField(
                        blank=True, help_text="IP address of change origin", null=True
                    ),
                ),
                (
                    "user_agent",
                    models.CharField(
                        blank=True, help_text="User agent string", max_length=500
                    ),
                ),
                (
                    "changed_by",
                    models.ForeignKey(
                        blank=True,
                        help_text="User who made the change",
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="entity_versions",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
                (
                    "entity_type",
                    models.ForeignKey(
                        help_text="Type of entity (Park, Ride, Company, etc.)",
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="entity_versions",
                        to="contenttypes.contenttype",
                    ),
                ),
                (
                    "submission",
                    models.ForeignKey(
                        blank=True,
                        help_text="Submission that caused this version (if applicable)",
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="versions",
                        to="moderation.contentsubmission",
                    ),
                ),
            ],
            options={
                "verbose_name": "Entity Version",
                "verbose_name_plural": "Entity Versions",
                "ordering": ["-created"],
                "indexes": [
                    models.Index(
                        fields=["entity_type", "entity_id", "-created"],
                        name="versioning__entity__8eabd9_idx",
                    ),
                    models.Index(
                        fields=["entity_type", "entity_id", "-version_number"],
                        name="versioning__entity__fe6f1b_idx",
                    ),
                    models.Index(
                        fields=["change_type"], name="versioning__change__17de57_idx"
                    ),
                    models.Index(
                        fields=["changed_by"], name="versioning__changed_39d5fd_idx"
                    ),
                    models.Index(
                        fields=["submission"], name="versioning__submiss_345f6b_idx"
                    ),
                ],
                "unique_together": {("entity_type", "entity_id", "version_number")},
            },
            bases=(django_lifecycle.mixins.LifecycleModelMixin, models.Model),
        ),
    ]
