# Generated by Django 5.2.5 on 2025-08-16 20:02

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):
    initial = True

    dependencies = []

    operations = [
        migrations.CreateModel(
            name="IndexedFile",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("size", models.IntegerField()),
                ("sha1", models.CharField(db_index=True, max_length=255, null=True)),
                (
                    "sha512",
                    models.CharField(db_index=True, max_length=255, null=True, unique=True),
                ),
                (
                    "mime_type",
                    models.CharField(db_index=True, max_length=255, null=True),
                ),
                ("file", models.FileField(max_length=2048, upload_to="")),
                ("first_seen", models.DateTimeField()),
                ("corrupt", models.BooleanField(default=None, null=True)),
                (
                    "derived_for",
                    models.CharField(
                        blank=True,
                        choices=[
                            (None, "Original file"),
                            ("thumbnail", "Video thumbnail"),
                            ("compression", "Compressed version (e.g., AVIF from GIF)"),
                        ],
                        db_index=True,
                        help_text="Type of derived file (e.g., 'thumbnail' for thumbnails)",
                        max_length=50,
                        null=True,
                    ),
                ),
                (
                    "metadata",
                    models.JSONField(
                        blank=True,
                        default=dict,
                        help_text="JSON field for type-specific metadata (dimensions, duration, etc.)",
                    ),
                ),
                (
                    "derived_from",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="derived_files",
                        to="fileindex.indexedfile",
                    ),
                ),
            ],
            options={
                "get_latest_by": "first_seen",
            },
        ),
        migrations.CreateModel(
            name="FilePath",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("mtime", models.DateTimeField()),
                ("ctime", models.DateTimeField()),
                ("hostname", models.CharField(max_length=1024, null=True)),
                ("path", models.CharField(db_index=True, max_length=2048)),
                ("created_at", models.DateTimeField()),
                (
                    "indexedfile",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        to="fileindex.indexedfile",
                    ),
                ),
            ],
        ),
        migrations.AddIndex(
            model_name="indexedfile",
            index=models.Index(
                fields=["derived_from", "derived_for"],
                name="fileindex_i_derived_a408eb_idx",
            ),
        ),
        migrations.AddConstraint(
            model_name="indexedfile",
            constraint=models.CheckConstraint(
                condition=models.Q(
                    models.Q(("corrupt", True), ("corrupt__isnull", False)),
                    models.Q(
                        ("mime_type__startswith", "image/"),
                        ("mime_type__startswith", "video/"),
                        _connector="OR",
                        _negated=True,
                    ),
                    models.Q(("metadata__has_key", "width"), ("metadata__has_key", "height")),
                    _connector="OR",
                ),
                name="visual_media_requires_dimensions",
            ),
        ),
        migrations.AddConstraint(
            model_name="indexedfile",
            constraint=models.CheckConstraint(
                condition=models.Q(
                    models.Q(("corrupt", True), ("corrupt__isnull", False)),
                    models.Q(
                        ("mime_type__startswith", "audio/"),
                        ("mime_type__startswith", "video/"),
                        _connector="OR",
                        _negated=True,
                    ),
                    ("metadata__has_key", "duration"),
                    _connector="OR",
                ),
                name="media_requires_duration",
            ),
        ),
        migrations.AddConstraint(
            model_name="indexedfile",
            constraint=models.CheckConstraint(
                condition=models.Q(
                    models.Q(("corrupt", True), ("corrupt__isnull", False)),
                    models.Q(("mime_type__startswith", "video/"), _negated=True),
                    ("metadata__has_key", "frame_rate"),
                    _connector="OR",
                ),
                name="video_requires_frame_rate",
            ),
        ),
        migrations.AddConstraint(
            model_name="indexedfile",
            constraint=models.CheckConstraint(
                condition=models.Q(
                    models.Q(("corrupt", True), ("corrupt__isnull", False)),
                    models.Q(("metadata__animated", True), _negated=True),
                    ("metadata__has_key", "duration"),
                    _connector="OR",
                ),
                name="animated_requires_duration",
            ),
        ),
        migrations.AlterUniqueTogether(
            name="filepath",
            unique_together={("indexedfile", "path")},
        ),
    ]
