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


class Migration(migrations.Migration):

    dependencies = [
        ("core", "0032_commandvariantpolicy_store_document"),
    ]

    operations = [
        migrations.CreateModel(
            name="ContactAvailabilitySettings",
            fields=[
                ("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
                ("enabled", models.BooleanField(default=True)),
                ("show_in_chat", models.BooleanField(default=True)),
                ("show_in_groups", models.BooleanField(default=True)),
                ("inference_enabled", models.BooleanField(default=True)),
                ("retention_days", models.PositiveIntegerField(default=90)),
                ("fade_threshold_seconds", models.PositiveIntegerField(default=900)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
                (
                    "user",
                    models.OneToOneField(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="contact_availability_settings",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
        ),
        migrations.CreateModel(
            name="ContactAvailabilityEvent",
            fields=[
                ("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
                (
                    "service",
                    models.CharField(
                        choices=[("signal", "Signal"), ("whatsapp", "WhatsApp"), ("xmpp", "XMPP"), ("instagram", "Instagram"), ("web", "Web")],
                        max_length=255,
                    ),
                ),
                (
                    "source_kind",
                    models.CharField(
                        choices=[
                            ("native_presence", "Native Presence"),
                            ("read_receipt", "Read Receipt"),
                            ("typing_start", "Typing Start"),
                            ("typing_stop", "Typing Stop"),
                            ("message_in", "Message In"),
                            ("message_out", "Message Out"),
                            ("inferred_timeout", "Inferred Timeout"),
                        ],
                        max_length=32,
                    ),
                ),
                (
                    "availability_state",
                    models.CharField(
                        choices=[("available", "Available"), ("unavailable", "Unavailable"), ("unknown", "Unknown"), ("fading", "Fading")],
                        max_length=32,
                    ),
                ),
                ("confidence", models.FloatField(default=0.0)),
                ("ts", models.BigIntegerField(db_index=True)),
                ("payload", models.JSONField(blank=True, default=dict)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                (
                    "person",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="availability_events",
                        to="core.person",
                    ),
                ),
                (
                    "person_identifier",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="availability_events",
                        to="core.personidentifier",
                    ),
                ),
                (
                    "user",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="contact_availability_events",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "indexes": [
                    models.Index(fields=["user", "person", "ts"], name="core_contac_user_id_0da9b2_idx"),
                    models.Index(fields=["user", "service", "ts"], name="core_contac_user_id_bce271_idx"),
                    models.Index(fields=["user", "availability_state", "ts"], name="core_contac_user_id_1b50b3_idx"),
                ],
            },
        ),
        migrations.CreateModel(
            name="ExternalChatLink",
            fields=[
                ("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
                ("provider", models.CharField(default="codex_cli", max_length=64)),
                ("external_chat_id", models.CharField(max_length=255)),
                ("metadata", models.JSONField(blank=True, default=dict)),
                ("enabled", models.BooleanField(default=True)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
                (
                    "person",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="external_chat_links",
                        to="core.person",
                    ),
                ),
                (
                    "person_identifier",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="external_chat_links",
                        to="core.personidentifier",
                    ),
                ),
                (
                    "user",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="external_chat_links",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "indexes": [
                    models.Index(fields=["user", "provider", "external_chat_id"], name="core_extern_user_id_f4a7b0_idx"),
                    models.Index(fields=["user", "provider", "enabled"], name="core_extern_user_id_7d2295_idx"),
                ],
                "constraints": [
                    models.UniqueConstraint(fields=("user", "provider", "external_chat_id"), name="unique_external_chat_link_per_provider"),
                ],
            },
        ),
        migrations.CreateModel(
            name="ContactAvailabilitySpan",
            fields=[
                ("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
                (
                    "service",
                    models.CharField(
                        choices=[("signal", "Signal"), ("whatsapp", "WhatsApp"), ("xmpp", "XMPP"), ("instagram", "Instagram"), ("web", "Web")],
                        max_length=255,
                    ),
                ),
                (
                    "state",
                    models.CharField(
                        choices=[("available", "Available"), ("unavailable", "Unavailable"), ("unknown", "Unknown"), ("fading", "Fading")],
                        max_length=32,
                    ),
                ),
                ("start_ts", models.BigIntegerField(db_index=True)),
                ("end_ts", models.BigIntegerField(db_index=True)),
                ("confidence_start", models.FloatField(default=0.0)),
                ("confidence_end", models.FloatField(default=0.0)),
                ("payload", models.JSONField(blank=True, default=dict)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
                (
                    "closing_event",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="closing_spans",
                        to="core.contactavailabilityevent",
                    ),
                ),
                (
                    "opening_event",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="opening_spans",
                        to="core.contactavailabilityevent",
                    ),
                ),
                (
                    "person",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="availability_spans",
                        to="core.person",
                    ),
                ),
                (
                    "person_identifier",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="availability_spans",
                        to="core.personidentifier",
                    ),
                ),
                (
                    "user",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="contact_availability_spans",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "indexes": [
                    models.Index(fields=["user", "person", "start_ts"], name="core_contac_user_id_9cd15a_idx"),
                    models.Index(fields=["user", "person", "end_ts"], name="core_contac_user_id_88584a_idx"),
                    models.Index(fields=["user", "service", "start_ts"], name="core_contac_user_id_182ffb_idx"),
                ],
            },
        ),
    ]
