# Generated by Django 5.2.9 on 2026-01-27 01:09

import django.db.models.deletion
import django.db.models.functions.text
import django.utils.timezone
from django.conf import settings
from django.contrib.auth.management import create_permissions
from django.contrib.auth.models import Group
from django.contrib.auth.models import Permission
from django.contrib.auth.models import User
from django.db import migrations
from django.db import models


def grant_share_link_bundle_permissions(apps, schema_editor):
    # Ensure newly introduced permissions are created for all apps
    for app_config in apps.get_app_configs():
        app_config.models_module = True
        create_permissions(app_config, apps=apps, verbosity=0)
        app_config.models_module = None

    add_document_perm = Permission.objects.filter(codename="add_document").first()
    share_bundle_permissions = Permission.objects.filter(
        codename__contains="sharelinkbundle",
    )

    users = User.objects.filter(user_permissions=add_document_perm).distinct()
    for user in users:
        user.user_permissions.add(*share_bundle_permissions)

    groups = Group.objects.filter(permissions=add_document_perm).distinct()
    for group in groups:
        group.permissions.add(*share_bundle_permissions)


def revoke_share_link_bundle_permissions(apps, schema_editor):
    share_bundle_permissions = Permission.objects.filter(
        codename__contains="sharelinkbundle",
    )
    for user in User.objects.all():
        user.user_permissions.remove(*share_bundle_permissions)
    for group in Group.objects.all():
        group.permissions.remove(*share_bundle_permissions)


class Migration(migrations.Migration):
    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ("documents", "0007_document_content_length"),
    ]

    operations = [
        migrations.CreateModel(
            name="ShareLinkBundle",
            fields=[
                (
                    "id",
                    models.AutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "created",
                    models.DateTimeField(
                        blank=True,
                        db_index=True,
                        default=django.utils.timezone.now,
                        editable=False,
                        verbose_name="created",
                    ),
                ),
                (
                    "expiration",
                    models.DateTimeField(
                        blank=True,
                        db_index=True,
                        null=True,
                        verbose_name="expiration",
                    ),
                ),
                (
                    "slug",
                    models.SlugField(
                        blank=True,
                        editable=False,
                        unique=True,
                        verbose_name="slug",
                    ),
                ),
                (
                    "file_version",
                    models.CharField(
                        choices=[("archive", "Archive"), ("original", "Original")],
                        default="archive",
                        max_length=50,
                    ),
                ),
                (
                    "status",
                    models.CharField(
                        choices=[
                            ("pending", "Pending"),
                            ("processing", "Processing"),
                            ("ready", "Ready"),
                            ("failed", "Failed"),
                        ],
                        default="pending",
                        max_length=50,
                    ),
                ),
                (
                    "size_bytes",
                    models.PositiveIntegerField(
                        blank=True,
                        null=True,
                        verbose_name="size (bytes)",
                    ),
                ),
                (
                    "last_error",
                    models.JSONField(
                        blank=True,
                        null=True,
                        default=None,
                        verbose_name="last error",
                    ),
                ),
                (
                    "file_path",
                    models.CharField(
                        blank=True,
                        max_length=512,
                        verbose_name="file path",
                    ),
                ),
                (
                    "built_at",
                    models.DateTimeField(
                        blank=True,
                        null=True,
                        verbose_name="built at",
                    ),
                ),
                (
                    "documents",
                    models.ManyToManyField(
                        related_name="share_link_bundles",
                        to="documents.document",
                        verbose_name="documents",
                    ),
                ),
                (
                    "owner",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="share_link_bundles",
                        to=settings.AUTH_USER_MODEL,
                        verbose_name="owner",
                    ),
                ),
            ],
            options={
                "ordering": ("-created",),
                "verbose_name": "share link bundle",
                "verbose_name_plural": "share link bundles",
            },
        ),
        migrations.RunPython(
            grant_share_link_bundle_permissions,
            reverse_code=revoke_share_link_bundle_permissions,
        ),
    ]
