# Copyright 2025 Laurent Defert
#
#  This file is part of Sosse.
#
# Sosse is free software: you can redistribute it and/or modify it under the terms of the GNU Affero
# General Public License as published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# Sosse is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
# the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License along with Sosse.
# If not, see <https://www.gnu.org/licenses/>.

# Generated by Django 3.2.19 on 2025-06-20 06:55

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

import se.utils


def truncate_mimetype_reverse(apps, schema_editor):
    """Truncate mimetype field values to 64 chars for reverse migration."""
    Document = apps.get_model("se", "Document")
    Document.objects.filter(mimetype__isnull=False).update(mimetype=functions.Left("mimetype", 64))


class Migration(migrations.Migration):
    dependencies = [
        ("se", "0016_sosse_1_13_0_pre"),
    ]

    operations = [
        migrations.CreateModel(
            name="MimeHandler",
            fields=[
                ("id", models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
                ("name", models.CharField(max_length=255, unique=True)),
                ("description", models.TextField(blank=True)),
                (
                    "license",
                    models.CharField(blank=True, help_text="License of the script, e.g. GPL-3.0", max_length=255),
                ),
                ("script", models.TextField(help_text="Shell script which receives a file path as first argument")),
                (
                    "mimetype_re",
                    models.TextField(
                        help_text="One regex per line to match MIME types (e.g. ^application/pdf$)",
                        validators=[se.utils.validate_multiline_re],
                    ),
                ),
                (
                    "timeout",
                    models.PositiveIntegerField(default=30, help_text="Timeout in seconds for the script execution"),
                ),
                ("builtin", models.BooleanField(default=False, verbose_name="Built-in")),
                ("enabled", models.BooleanField(default=True)),
            ],
        ),
        migrations.RenameModel(
            old_name="DomainSetting",
            new_name="Domain",
        ),
        migrations.AddField(
            model_name="document",
            name="mime_handlers_result",
            field=models.TextField(blank=True, default=""),
        ),
        migrations.AddField(
            model_name="document",
            name="retries",
            field=models.PositiveIntegerField(default=0, verbose_name="Crawl retries"),
        ),
        migrations.AlterField(
            model_name="document",
            name="mimetype",
            field=models.CharField(blank=True, max_length=128, null=True),
        ),
        migrations.RunPython(
            migrations.RunPython.noop,
            truncate_mimetype_reverse,
        ),
    ]
