import uuid

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


class Migration(migrations.Migration):

    dependencies = [
        ("core", "0033_contactavailability_and_externalchatlink"),
    ]

    operations = [
        migrations.AlterField(
            model_name="externalsyncevent",
            name="status",
            field=models.CharField(
                choices=[
                    ("pending", "Pending"),
                    ("ok", "OK"),
                    ("failed", "Failed"),
                    ("retrying", "Retrying"),
                    ("waiting_approval", "Waiting Approval"),
                ],
                default="pending",
                max_length=32,
            ),
        ),
        migrations.CreateModel(
            name="CodexRun",
            fields=[
                ("id", models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ("source_service", models.CharField(blank=True, default="", max_length=255)),
                ("source_channel", models.CharField(blank=True, default="", max_length=255)),
                ("external_chat_id", models.CharField(blank=True, default="", max_length=255)),
                (
                    "status",
                    models.CharField(
                        choices=[
                            ("queued", "Queued"),
                            ("running", "Running"),
                            ("waiting_approval", "Waiting Approval"),
                            ("approved_waiting_resume", "Approved Waiting Resume"),
                            ("denied", "Denied"),
                            ("ok", "OK"),
                            ("failed", "Failed"),
                            ("cancelled", "Cancelled"),
                        ],
                        default="queued",
                        max_length=32,
                    ),
                ),
                ("request_payload", models.JSONField(blank=True, default=dict)),
                ("result_payload", models.JSONField(blank=True, default=dict)),
                ("error", models.TextField(blank=True, default="")),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
                (
                    "derived_task_event",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="codex_runs",
                        to="core.derivedtaskevent",
                    ),
                ),
                (
                    "epic",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="codex_runs",
                        to="core.taskepic",
                    ),
                ),
                (
                    "project",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="codex_runs",
                        to="core.taskproject",
                    ),
                ),
                (
                    "source_message",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="codex_runs",
                        to="core.message",
                    ),
                ),
                (
                    "task",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="codex_runs",
                        to="core.derivedtask",
                    ),
                ),
                (
                    "user",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="codex_runs",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "indexes": [
                    models.Index(fields=["user", "status", "updated_at"], name="core_codexr_user_id_dddd7f_idx"),
                    models.Index(
                        fields=["user", "source_service", "source_channel", "created_at"],
                        name="core_codexr_user_id_a70a53_idx",
                    ),
                ],
            },
        ),
        migrations.CreateModel(
            name="CodexPermissionRequest",
            fields=[
                ("id", models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ("approval_key", models.CharField(max_length=255, unique=True)),
                ("summary", models.TextField(blank=True, default="")),
                ("requested_permissions", models.JSONField(blank=True, default=dict)),
                ("resume_payload", models.JSONField(blank=True, default=dict)),
                (
                    "status",
                    models.CharField(
                        choices=[
                            ("pending", "Pending"),
                            ("approved", "Approved"),
                            ("denied", "Denied"),
                            ("expired", "Expired"),
                        ],
                        default="pending",
                        max_length=16,
                    ),
                ),
                ("requested_at", models.DateTimeField(auto_now_add=True)),
                ("resolved_at", models.DateTimeField(blank=True, null=True)),
                ("resolved_by_identifier", models.CharField(blank=True, default="", max_length=255)),
                ("resolution_note", models.TextField(blank=True, default="")),
                (
                    "codex_run",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="permission_requests",
                        to="core.codexrun",
                    ),
                ),
                (
                    "external_sync_event",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="codex_permission_requests",
                        to="core.externalsyncevent",
                    ),
                ),
                (
                    "user",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="codex_permission_requests",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "indexes": [
                    models.Index(fields=["user", "status", "requested_at"], name="core_codexp_user_id_ba71e9_idx"),
                    models.Index(fields=["approval_key"], name="core_codexp_approva_83035d_idx"),
                ],
            },
        ),
    ]
