# 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-08-19 13:52

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

import se.collection
import se.url
import se.utils


class Migration(migrations.Migration):
    dependencies = [
        ("se", "0019_crawl_policy_backup_run"),
    ]

    operations = [
        # Create backup of CrawlPolicy table before rename
        migrations.RenameModel(
            old_name="CrawlPolicy",
            new_name="Collection",
        ),
        migrations.RenameField(
            model_name="authfield",
            old_name="crawl_policy",
            new_name="collection",
        ),
        migrations.AddField(
            model_name="collection",
            name="name",
            field=models.CharField(max_length=256, unique=True, blank=True, null=True),
        ),
        migrations.RenameField(
            model_name="collection",
            old_name="url_regex",
            new_name="unlimited_regex",
        ),
        migrations.RenameField(
            model_name="collection",
            old_name="url_regex_pg",
            new_name="unlimited_regex_pg",
        ),
        migrations.AlterField(
            model_name="collection",
            name="unlimited_regex",
            field=models.TextField(
                blank=True,
                default="",
                help_text="URL regular expressions. Matching URLs will have unlimited crawling recursion depth (one per line; lines starting with # are ignored)",
                validators=[se.collection.validate_url_regexp],
                verbose_name="Unlimited depth URL regex",
            ),
        ),
        migrations.AlterField(
            model_name="collection",
            name="unlimited_regex_pg",
            field=models.TextField(default=""),
        ),
        migrations.AddField(
            model_name="collection",
            name="excluded_regex",
            field=models.TextField(
                blank=True,
                default="",
                help_text="URL regular expressions to exclude from this collection. (one by line, lines starting with # are ignored)",
                validators=[se.collection.validate_url_regexp],
                verbose_name="Excluded URL regex",
            ),
        ),
        migrations.AddField(
            model_name="collection",
            name="excluded_regex_pg",
            field=models.TextField(default=""),
        ),
        migrations.AddField(
            model_name="collection",
            name="limited_regex",
            field=models.TextField(
                blank=True,
                default="",
                help_text="URL regular expressions. Matching URLs will have limited crawling recursion depth (one per line; lines starting with # are ignored)",
                validators=[se.collection.validate_url_regexp],
                verbose_name="Limited depth URL regex",
            ),
        ),
        migrations.AddField(
            model_name="collection",
            name="limited_regex_pg",
            field=models.TextField(default=""),
        ),
        migrations.AddField(
            model_name="collection",
            name="combined_regex_pg",
            field=models.TextField(default=""),
        ),
        migrations.AlterField(
            model_name="collection",
            name="recursion_depth",
            field=models.PositiveIntegerField(
                default=1,
                help_text="Level of external links (links that don't match the regex) to recurse into",
                verbose_name="Limited recursion depth",
            ),
        ),
        migrations.AddField(
            model_name="document",
            name="collection",
            field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to="se.collection"),
        ),
        migrations.AlterField(
            model_name="document",
            name="url",
            field=models.TextField(validators=[se.url.validate_url]),
        ),
        migrations.AlterUniqueTogether(
            name="document",
            unique_together={("url", "collection")},
        ),
        migrations.AddIndex(
            model_name="document",
            index=models.Index(fields=["collection"], name="se_document_collect_f51e37_idx"),
        ),
        migrations.AlterField(
            model_name="collection",
            name="recursion_depth",
            field=models.PositiveIntegerField(
                default=1,
                help_text="Level of external links (links that don't match the regex) to recurse into",
                verbose_name="Limited recursion depth",
            ),
        ),
        migrations.AddField(
            model_name="webhook",
            name="url_re",
            field=models.TextField(
                blank=True,
                default=".*",
                help_text="Run the webhook on pages with URL matching this regex (one per line; lines starting with # are ignored)",
                verbose_name="URL regex",
                validators=[se.utils.validate_multiline_re],
            ),
        ),
        migrations.AlterModelOptions(
            name="collection",
            options={"verbose_name": "Collection", "verbose_name_plural": "Collections"},
        ),
        migrations.AlterModelOptions(
            name="collection",
            options={"ordering": ["name"]},
        ),
    ]
