# Generated by Django 5.2.4 on 2025-07-30 07:14

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


class Migration(migrations.Migration):

    initial = True

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

    operations = [
        migrations.CreateModel(
            name="ApprovalFlow",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("object_id", models.CharField(max_length=255)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                (
                    "content_type",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        to="contenttypes.contenttype",
                    ),
                ),
            ],
        ),
        migrations.CreateModel(
            name="ApprovalInstance",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("form_data", models.JSONField(blank=True, null=True)),
                (
                    "form_object_id",
                    models.CharField(blank=True, max_length=255, null=True),
                ),
                (
                    "step_number",
                    models.PositiveIntegerField(
                        default=1, help_text="The current step in the flow"
                    ),
                ),
                (
                    "status",
                    models.CharField(
                        choices=[
                            ("pending", "Pending"),
                            ("current", "Current"),
                            ("approved", "Approved"),
                            ("rejected", "Rejected"),
                            ("resubmission", "Needs Resubmission"),
                            ("delegated", "Delegated"),
                            ("escalated", "Escalated"),
                            ("cancelled", "Cancelled"),
                            ("completed", "Completed"),
                        ],
                        default="pending",
                        help_text="Current approval status",
                        max_length=30,
                    ),
                ),
                ("comment", models.TextField(blank=True)),
                (
                    "sla_duration",
                    models.DurationField(
                        blank=True,
                        help_text="SLA duration for this step (e.g., 2 days, 4 hours). Optional.",
                        null=True,
                    ),
                ),
                (
                    "allow_higher_level",
                    models.BooleanField(
                        default=False,
                        help_text="Allow users with higher roles to approve this step on behalf of assigned user",
                    ),
                ),
                (
                    "extra_fields",
                    models.JSONField(
                        blank=True,
                        help_text="Additional custom fields for extending functionality without package modifications",
                        null=True,
                    ),
                ),
                ("started_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
                (
                    "action_user",
                    models.ForeignKey(
                        blank=True,
                        help_text="User who actually performed the approve/reject action",
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="approval_actions",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
                (
                    "assigned_to",
                    models.ForeignKey(
                        blank=True,
                        help_text="User currently assigned to act on this step",
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
                (
                    "flow",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="instances",
                        to="approval_workflow.approvalflow",
                    ),
                ),
                (
                    "form_content_type",
                    models.ForeignKey(
                        blank=True,
                        help_text="Content type of the dynamic form model",
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="approval_forms",
                        to="contenttypes.contenttype",
                    ),
                ),
                (
                    "assigned_role_content_type",
                    models.ForeignKey(
                        blank=True,
                        help_text="Content type of the role model from settings",
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="approval_roles",
                        to="contenttypes.contenttype",
                    ),
                ),
                (
                    "assigned_role_object_id",
                    models.CharField(
                        blank=True,
                        help_text="ID of the role instance",
                        max_length=255,
                        null=True,
                    ),
                ),
                (
                    "role_selection_strategy",
                    models.CharField(
                        blank=True,
                        choices=[
                            ("anyone", "Anyone with role can approve"),
                            ("consensus", "All users with role must approve"),
                            ("round_robin", "Distribute approvals evenly among role users"),
                        ],
                        help_text="Strategy for selecting approvers when assigned to a role",
                        max_length=20,
                        null=True,
                    ),
                ),
            ],
            options={
                "ordering": ["-started_at"],
            },
        ),
        migrations.AddIndex(
            model_name="approvalflow",
            index=models.Index(
                fields=["content_type", "object_id"], name="approval_flow_object_idx"
            ),
        ),
        migrations.AlterUniqueTogether(
            name="approvalflow",
            unique_together={("content_type", "object_id")},
        ),
        migrations.AddIndex(
            model_name="approvalinstance",
            index=models.Index(
                fields=["flow", "status"], name="appinst_flow_status_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="approvalinstance",
            index=models.Index(
                fields=["assigned_to", "status"], name="appinst_assigned_status_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="approvalinstance",
            index=models.Index(fields=["started_at"], name="appinst_started_at_idx"),
        ),
        migrations.AddConstraint(
            model_name="approvalinstance",
            constraint=models.UniqueConstraint(
                condition=models.Q(("status", "current"), ("assigned_to__isnull", False), ("assigned_role_content_type__isnull", True)),
                fields=("flow",),
                name="unique_current_per_flow_user",
            ),
        ),
    ]
