# Generated by Django 5.1.10 on 2025-08-27 22:19

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


class Migration(migrations.Migration):
    atomic = False

    initial = True

    dependencies = [
        ("projects", "0030_project_search_vector_index"),
        ("tasks", "0057_annotation_proj_result_octlen_idx_async"),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name="AnnotationState",
            fields=[
                (
                    "id",
                    models.UUIDField(
                        default=fsm.utils.generate_uuid7,
                        editable=False,
                        help_text="UUID7 provides natural time ordering and global uniqueness",
                        primary_key=True,
                        serialize=False,
                    ),
                ),
                (
                    "organization_id",
                    models.PositiveIntegerField(
                        blank=True,
                        db_index=True,
                        help_text="Organization ID that owns this state record (for multi-tenant applications)",
                        null=True,
                    ),
                ),
                (
                    "previous_state",
                    models.CharField(
                        blank=True,
                        help_text="Previous state before this transition",
                        max_length=50,
                        null=True,
                    ),
                ),
                (
                    "transition_name",
                    models.CharField(
                        blank=True,
                        help_text="Name of the transition method that triggered this state change",
                        max_length=100,
                        null=True,
                    ),
                ),
                (
                    "context_data",
                    models.JSONField(
                        default=dict,
                        help_text="Additional context data for this transition (e.g., validation results, external IDs)",
                    ),
                ),
                (
                    "reason",
                    models.TextField(
                        blank=True,
                        help_text="Human-readable reason for this state transition",
                    ),
                ),
                (
                    "created_at",
                    models.DateTimeField(
                        auto_now_add=True,
                        help_text="Human-readable timestamp for debugging (UUID7 id contains precise timestamp)",
                    ),
                ),
                (
                    "state",
                    models.CharField(
                        choices=[
                            ("SUBMITTED", "Submitted"),
                            ("COMPLETED", "Completed"),
                        ],
                        db_index=True,
                        max_length=50,
                    ),
                ),
                (
                    "task_id",
                    models.PositiveIntegerField(
                        db_index=True,
                        help_text="From annotation.task_id - denormalized for performance",
                    ),
                ),
                (
                    "project_id",
                    models.PositiveIntegerField(
                        db_index=True,
                        help_text="From annotation.task.project_id - denormalized for performance",
                    ),
                ),
                (
                    "completed_by_id",
                    models.PositiveIntegerField(
                        db_index=True,
                        help_text="From annotation.completed_by_id - denormalized for performance",
                        null=True,
                    ),
                ),
                (
                    "annotation",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="fsm_states",
                        to="tasks.annotation",
                    ),
                ),
                (
                    "triggered_by",
                    models.ForeignKey(
                        help_text="User who triggered this state transition",
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "ordering": ["-id"],
                "indexes": [
                    models.Index(
                        fields=["annotation_id", "-id"], name="anno_current_state_idx"
                    ),
                    models.Index(
                        fields=["task_id", "state", "-id"], name="anno_task_state_idx"
                    ),
                    models.Index(
                        fields=["completed_by_id", "state", "-id"],
                        name="anno_user_report_idx",
                    ),
                    models.Index(
                        fields=["project_id", "state", "-id"],
                        name="anno_project_report_idx",
                    ),
                ],
            },
        ),
        migrations.CreateModel(
            name="ProjectState",
            fields=[
                (
                    "id",
                    models.UUIDField(
                        default=fsm.utils.generate_uuid7,
                        editable=False,
                        help_text="UUID7 provides natural time ordering and global uniqueness",
                        primary_key=True,
                        serialize=False,
                    ),
                ),
                (
                    "organization_id",
                    models.PositiveIntegerField(
                        blank=True,
                        db_index=True,
                        help_text="Organization ID that owns this state record (for multi-tenant applications)",
                        null=True,
                    ),
                ),
                (
                    "previous_state",
                    models.CharField(
                        blank=True,
                        help_text="Previous state before this transition",
                        max_length=50,
                        null=True,
                    ),
                ),
                (
                    "transition_name",
                    models.CharField(
                        blank=True,
                        help_text="Name of the transition method that triggered this state change",
                        max_length=100,
                        null=True,
                    ),
                ),
                (
                    "context_data",
                    models.JSONField(
                        default=dict,
                        help_text="Additional context data for this transition (e.g., validation results, external IDs)",
                    ),
                ),
                (
                    "reason",
                    models.TextField(
                        blank=True,
                        help_text="Human-readable reason for this state transition",
                    ),
                ),
                (
                    "created_at",
                    models.DateTimeField(
                        auto_now_add=True,
                        help_text="Human-readable timestamp for debugging (UUID7 id contains precise timestamp)",
                    ),
                ),
                (
                    "state",
                    models.CharField(
                        choices=[
                            ("CREATED", "Created"),
                            ("IN_PROGRESS", "In Progress"),
                            ("COMPLETED", "Completed"),
                        ],
                        db_index=True,
                        max_length=50,
                    ),
                ),
                (
                    "created_by_id",
                    models.PositiveIntegerField(
                        db_index=True,
                        help_text="From project.created_by_id - denormalized for performance",
                        null=True,
                    ),
                ),
                (
                    "project",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="fsm_states",
                        to="projects.project",
                    ),
                ),
                (
                    "triggered_by",
                    models.ForeignKey(
                        help_text="User who triggered this state transition",
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "ordering": ["-id"],
                "indexes": [
                    models.Index(
                        fields=["project_id", "-id"], name="project_current_state_idx"
                    ),
                    models.Index(
                        fields=["organization_id", "state", "-id"],
                        name="project_org_state_idx",
                    ),
                    models.Index(
                        fields=["organization_id", "-id"],
                        name="project_org_reporting_idx",
                    ),
                ],
            },
        ),
        migrations.CreateModel(
            name="TaskState",
            fields=[
                (
                    "id",
                    models.UUIDField(
                        default=fsm.utils.generate_uuid7,
                        editable=False,
                        help_text="UUID7 provides natural time ordering and global uniqueness",
                        primary_key=True,
                        serialize=False,
                    ),
                ),
                (
                    "organization_id",
                    models.PositiveIntegerField(
                        blank=True,
                        db_index=True,
                        help_text="Organization ID that owns this state record (for multi-tenant applications)",
                        null=True,
                    ),
                ),
                (
                    "previous_state",
                    models.CharField(
                        blank=True,
                        help_text="Previous state before this transition",
                        max_length=50,
                        null=True,
                    ),
                ),
                (
                    "transition_name",
                    models.CharField(
                        blank=True,
                        help_text="Name of the transition method that triggered this state change",
                        max_length=100,
                        null=True,
                    ),
                ),
                (
                    "context_data",
                    models.JSONField(
                        default=dict,
                        help_text="Additional context data for this transition (e.g., validation results, external IDs)",
                    ),
                ),
                (
                    "reason",
                    models.TextField(
                        blank=True,
                        help_text="Human-readable reason for this state transition",
                    ),
                ),
                (
                    "created_at",
                    models.DateTimeField(
                        auto_now_add=True,
                        help_text="Human-readable timestamp for debugging (UUID7 id contains precise timestamp)",
                    ),
                ),
                (
                    "state",
                    models.CharField(
                        choices=[
                            ("CREATED", "Created"),
                            ("IN_PROGRESS", "In Progress"),
                            ("COMPLETED", "Completed"),
                        ],
                        db_index=True,
                        max_length=50,
                    ),
                ),
                (
                    "project_id",
                    models.PositiveIntegerField(
                        db_index=True,
                        help_text="From task.project_id - denormalized for performance",
                    ),
                ),
                (
                    "task",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="fsm_states",
                        to="tasks.task",
                    ),
                ),
                (
                    "triggered_by",
                    models.ForeignKey(
                        help_text="User who triggered this state transition",
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "ordering": ["-id"],
                "indexes": [
                    models.Index(
                        fields=["task_id", "-id"], name="task_current_state_idx"
                    ),
                    models.Index(
                        fields=["project_id", "state", "-id"],
                        name="task_project_state_idx",
                    ),
                    models.Index(
                        fields=["organization_id", "state", "-id"],
                        name="task_org_reporting_idx",
                    ),
                    models.Index(fields=["task_id", "id"], name="task_history_idx"),
                ],
            },
        ),
    ]
