import uuid

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


class Migration(migrations.Migration):
    dependencies = [
        ("core", "0034_codexrun_codexpermissionrequest_and_more"),
    ]

    operations = [
        migrations.CreateModel(
            name="ConversationEvent",
            fields=[
                ("id", models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ("ts", models.BigIntegerField(db_index=True, help_text="Event timestamp (unix ms).")),
                (
                    "event_type",
                    models.CharField(
                        choices=[
                            ("message_created", "Message Created"),
                            ("message_edited", "Message Edited"),
                            ("message_deleted", "Message Deleted"),
                            ("reaction_added", "Reaction Added"),
                            ("reaction_removed", "Reaction Removed"),
                            ("read_receipt", "Read Receipt"),
                            ("typing_started", "Typing Started"),
                            ("typing_stopped", "Typing Stopped"),
                            ("participant_added", "Participant Added"),
                            ("participant_removed", "Participant Removed"),
                            ("delivery_receipt", "Delivery Receipt"),
                        ],
                        max_length=64,
                    ),
                ),
                (
                    "direction",
                    models.CharField(
                        choices=[("in", "Inbound"), ("out", "Outbound"), ("system", "System")],
                        max_length=16,
                    ),
                ),
                ("actor_identifier", models.CharField(blank=True, default="", max_length=255)),
                (
                    "origin_transport",
                    models.CharField(
                        blank=True,
                        choices=[
                            ("signal", "Signal"),
                            ("whatsapp", "WhatsApp"),
                            ("xmpp", "XMPP"),
                            ("instagram", "Instagram"),
                            ("web", "Web"),
                        ],
                        default="",
                        max_length=32,
                    ),
                ),
                ("origin_message_id", models.CharField(blank=True, default="", max_length=255)),
                ("origin_chat_id", models.CharField(blank=True, default="", max_length=255)),
                ("payload", models.JSONField(blank=True, default=dict)),
                ("raw_payload", models.JSONField(blank=True, default=dict)),
                ("trace_id", models.CharField(blank=True, default="", max_length=64)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                (
                    "session",
                    models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to="core.chatsession"),
                ),
                (
                    "user",
                    models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
                ),
            ],
            options={
                "ordering": ["ts", "created_at"],
            },
        ),
        migrations.CreateModel(
            name="AdapterHealthEvent",
            fields=[
                ("id", models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                (
                    "service",
                    models.CharField(
                        choices=[
                            ("signal", "Signal"),
                            ("whatsapp", "WhatsApp"),
                            ("xmpp", "XMPP"),
                            ("instagram", "Instagram"),
                        ],
                        max_length=32,
                    ),
                ),
                (
                    "status",
                    models.CharField(
                        choices=[
                            ("ok", "OK"),
                            ("degraded", "Degraded"),
                            ("down", "Down"),
                            ("recovering", "Recovering"),
                        ],
                        max_length=32,
                    ),
                ),
                ("reason", models.TextField(blank=True, default="")),
                ("retry_meta", models.JSONField(blank=True, default=dict)),
                (
                    "ts",
                    models.BigIntegerField(
                        db_index=True,
                        help_text="Health transition timestamp (unix ms).",
                    ),
                ),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                (
                    "user",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "ordering": ["-ts", "-created_at"],
            },
        ),
        migrations.AddIndex(
            model_name="conversationevent",
            index=models.Index(fields=["user", "session", "ts"], name="core_conver_user_id_96580b_idx"),
        ),
        migrations.AddIndex(
            model_name="conversationevent",
            index=models.Index(
                fields=["origin_transport", "origin_message_id"],
                name="core_conver_origin__283534_idx",
            ),
        ),
        migrations.AddIndex(
            model_name="conversationevent",
            index=models.Index(fields=["trace_id"], name="core_conver_trace_i_4ed2ec_idx"),
        ),
        migrations.AddIndex(
            model_name="conversationevent",
            index=models.Index(
                fields=["event_type", "created_at"],
                name="core_conver_event_t_a16f3e_idx",
            ),
        ),
        migrations.AddIndex(
            model_name="adapterhealthevent",
            index=models.Index(fields=["service", "ts"], name="core_adapte_service_f6e8d4_idx"),
        ),
        migrations.AddIndex(
            model_name="adapterhealthevent",
            index=models.Index(
                fields=["status", "created_at"],
                name="core_adapte_status_6529f5_idx",
            ),
        ),
    ]
