# Generated by Django 5.1.3 on 2026-06-01 08:29

import django.db.models.deletion
import django.utils.timezone
import uuid
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ("bpm", "0006_kpi_models"),
        ("platform_workspaces", "0004_workspacerolekgrant"),
        ("tenants", "0001_initial"),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name="ProcessInstance",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                (
                    "created_at",
                    models.DateTimeField(
                        auto_now_add=True, db_index=True, verbose_name="created at"
                    ),
                ),
                ("updated_at", models.DateTimeField(auto_now=True, verbose_name="updated at")),
                (
                    "public_id",
                    models.UUIDField(
                        db_index=True,
                        default=uuid.uuid4,
                        editable=False,
                        unique=True,
                        verbose_name="public id",
                    ),
                ),
                (
                    "instance_number",
                    models.CharField(
                        blank=True,
                        help_text="Auto-generated: <process_code>-<year>-<seq>.",
                        max_length=50,
                        verbose_name="instance number",
                    ),
                ),
                ("title", models.CharField(max_length=255, verbose_name="title")),
                (
                    "triggered_by",
                    models.CharField(
                        blank=True,
                        help_text="Human-readable trigger description, e.g. 'شروع دوره Q2 1405'.",
                        max_length=255,
                        verbose_name="triggered by",
                    ),
                ),
                (
                    "status",
                    models.CharField(
                        choices=[
                            ("planned", "Planned / برنامه\u200cریزی\u200cشده"),
                            ("in_progress", "In Progress / در حال اجرا"),
                            ("completed", "Completed / تکمیل\u200cشده"),
                            ("cancelled", "Cancelled / لغو\u200cشده"),
                        ],
                        default="planned",
                        max_length=20,
                        verbose_name="status",
                    ),
                ),
                (
                    "start_date",
                    models.DateTimeField(blank=True, null=True, verbose_name="start date"),
                ),
                (
                    "end_date",
                    models.DateTimeField(blank=True, null=True, verbose_name="planned end date"),
                ),
                (
                    "actual_end_date",
                    models.DateTimeField(blank=True, null=True, verbose_name="actual end date"),
                ),
                ("notes", models.TextField(blank=True, verbose_name="notes")),
                (
                    "created_by",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="created_process_instances",
                        to=settings.AUTH_USER_MODEL,
                        verbose_name="created by",
                    ),
                ),
                (
                    "process",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="instances",
                        to="bpm.processdefinition",
                        verbose_name="process",
                    ),
                ),
                (
                    "tenant",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="process_instances",
                        to="tenants.tenant",
                        verbose_name="tenant",
                    ),
                ),
                (
                    "workspace",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="process_instances",
                        to="platform_workspaces.workspace",
                        verbose_name="workspace",
                    ),
                ),
            ],
            options={
                "verbose_name": "process instance",
                "verbose_name_plural": "process instances",
                "ordering": ("-created_at",),
            },
        ),
        migrations.CreateModel(
            name="ControlPointExecution",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                (
                    "created_at",
                    models.DateTimeField(
                        auto_now_add=True, db_index=True, verbose_name="created at"
                    ),
                ),
                ("updated_at", models.DateTimeField(auto_now=True, verbose_name="updated at")),
                (
                    "outcome",
                    models.CharField(
                        choices=[
                            ("passed", "Passed / تأیید شد"),
                            ("failed", "Failed / رد شد"),
                            ("deferred", "Deferred / به بعد موکول شد"),
                        ],
                        default="passed",
                        max_length=20,
                        verbose_name="outcome",
                    ),
                ),
                (
                    "executed_at",
                    models.DateTimeField(
                        default=django.utils.timezone.now, verbose_name="executed at"
                    ),
                ),
                ("notes", models.TextField(blank=True, verbose_name="notes")),
                (
                    "control_point",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="executions",
                        to="bpm.processcontrolpoint",
                        verbose_name="control point",
                    ),
                ),
                (
                    "executed_by",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="bpm_cp_executions",
                        to=settings.AUTH_USER_MODEL,
                        verbose_name="executed by",
                    ),
                ),
                (
                    "instance",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="cp_executions",
                        to="bpm.processinstance",
                        verbose_name="process instance",
                    ),
                ),
            ],
            options={
                "verbose_name": "control point execution",
                "verbose_name_plural": "control point executions",
                "ordering": ("-executed_at",),
                "constraints": [
                    models.UniqueConstraint(
                        fields=("instance", "control_point"),
                        name="bpm_cpexecution_instance_cp_uniq",
                    )
                ],
            },
        ),
        migrations.CreateModel(
            name="ProcessInstanceRuleCheck",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                (
                    "created_at",
                    models.DateTimeField(
                        auto_now_add=True, db_index=True, verbose_name="created at"
                    ),
                ),
                ("updated_at", models.DateTimeField(auto_now=True, verbose_name="updated at")),
                ("is_compliant", models.BooleanField(default=True, verbose_name="compliant")),
                (
                    "checked_at",
                    models.DateTimeField(
                        default=django.utils.timezone.now, verbose_name="checked at"
                    ),
                ),
                ("notes", models.TextField(blank=True, verbose_name="notes")),
                (
                    "checked_by",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="bpm_rule_checks",
                        to=settings.AUTH_USER_MODEL,
                        verbose_name="checked by",
                    ),
                ),
                (
                    "instance",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="rule_checks",
                        to="bpm.processinstance",
                        verbose_name="process instance",
                    ),
                ),
                (
                    "rule",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="instance_checks",
                        to="bpm.processrule",
                        verbose_name="business rule",
                    ),
                ),
            ],
            options={
                "verbose_name": "rule check",
                "verbose_name_plural": "rule checks",
                "ordering": ("-checked_at",),
                "constraints": [
                    models.UniqueConstraint(
                        fields=("instance", "rule"), name="bpm_instancerulecheck_instance_rule_uniq"
                    )
                ],
            },
        ),
        migrations.CreateModel(
            name="ProcessInstanceStep",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                (
                    "created_at",
                    models.DateTimeField(
                        auto_now_add=True, db_index=True, verbose_name="created at"
                    ),
                ),
                ("updated_at", models.DateTimeField(auto_now=True, verbose_name="updated at")),
                (
                    "status",
                    models.CharField(
                        choices=[
                            ("pending", "Pending / در انتظار"),
                            ("in_progress", "In Progress / در حال اجرا"),
                            ("done", "Done / انجام\u200cشده"),
                            ("skipped", "Skipped / رد شده"),
                        ],
                        default="pending",
                        max_length=20,
                        verbose_name="status",
                    ),
                ),
                (
                    "started_at",
                    models.DateTimeField(blank=True, null=True, verbose_name="started at"),
                ),
                (
                    "completed_at",
                    models.DateTimeField(blank=True, null=True, verbose_name="completed at"),
                ),
                ("notes", models.TextField(blank=True, verbose_name="notes")),
                (
                    "output_document_ids",
                    models.JSONField(
                        blank=True,
                        default=list,
                        help_text="List of DMS document UUIDs produced by this step.",
                        verbose_name="output document IDs",
                    ),
                ),
                (
                    "assigned_to",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="assigned_instance_steps",
                        to=settings.AUTH_USER_MODEL,
                        verbose_name="assigned to",
                    ),
                ),
                (
                    "instance",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="instance_steps",
                        to="bpm.processinstance",
                        verbose_name="process instance",
                    ),
                ),
                (
                    "step",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="instance_steps",
                        to="bpm.processoperationalstep",
                        verbose_name="operational step",
                    ),
                ),
            ],
            options={
                "verbose_name": "instance step",
                "verbose_name_plural": "instance steps",
                "ordering": ("step__order", "step__step_number"),
                "constraints": [
                    models.UniqueConstraint(
                        fields=("instance", "step"), name="bpm_instancestep_instance_step_uniq"
                    )
                ],
            },
        ),
    ]
