# Generated by Django 6.0.2 on 2026-03-07 15:26

import uuid

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


class Migration(migrations.Migration):
    initial = True

    dependencies = []

    operations = [
        migrations.CreateModel(
            name="SearchIndex",
            fields=[
                (
                    "id",
                    models.UUIDField(
                        default=uuid.uuid4,
                        editable=False,
                        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"),
                ),
                (
                    "name",
                    models.CharField(
                        db_index=True,
                        help_text="Logical index name (e.g. 'products').",
                        max_length=100,
                    ),
                ),
                (
                    "tenant_id",
                    models.CharField(
                        blank=True,
                        db_index=True,
                        default="",
                        help_text="Tenant identifier for multi-tenant setups. Blank in single-tenant mode.",
                        max_length=100,
                    ),
                ),
                (
                    "engine_uid",
                    models.CharField(
                        editable=False,
                        help_text="Computed index name sent to the search engine.",
                        max_length=255,
                        unique=True,
                    ),
                ),
                (
                    "primary_key_field",
                    models.CharField(
                        default="id",
                        help_text="Document field used as the primary key in the search engine.",
                        max_length=100,
                    ),
                ),
                (
                    "settings",
                    models.JSONField(
                        blank=True,
                        default=dict,
                        help_text="Engine settings: searchable_attributes, filterable_attributes, sortable_attributes, synonyms, stop_words, ranking_rules.",
                    ),
                ),
                (
                    "document_count",
                    models.PositiveIntegerField(
                        default=0,
                        help_text="Cached document count, updated by refresh task.",
                    ),
                ),
                (
                    "is_synced",
                    models.BooleanField(
                        default=False,
                        help_text="Whether current settings have been pushed to the engine.",
                    ),
                ),
                (
                    "last_synced_at",
                    models.DateTimeField(
                        blank=True,
                        help_text="Timestamp of last successful settings sync.",
                        null=True,
                    ),
                ),
                (
                    "is_active",
                    models.BooleanField(
                        db_index=True,
                        default=True,
                        help_text="Deactivated indexes are not queried or synced.",
                    ),
                ),
            ],
            options={
                "verbose_name": "search index",
                "verbose_name_plural": "search indexes",
                "ordering": ["name"],
                "unique_together": {("tenant_id", "name")},
            },
        ),
        migrations.CreateModel(
            name="IndexSyncLog",
            fields=[
                (
                    "id",
                    models.UUIDField(
                        default=uuid.uuid4,
                        editable=False,
                        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"),
                ),
                (
                    "action",
                    models.CharField(
                        choices=[
                            ("created", "Created"),
                            ("settings_updated", "Settings Updated"),
                            ("deleted", "Deleted"),
                            ("reindexed", "Reindexed"),
                            ("documents_added", "Documents Added"),
                            ("documents_deleted", "Documents Deleted"),
                        ],
                        max_length=50,
                    ),
                ),
                (
                    "status",
                    models.CharField(
                        choices=[
                            ("pending", "Pending"),
                            ("success", "Success"),
                            ("failed", "Failed"),
                        ],
                        default="pending",
                        max_length=20,
                    ),
                ),
                (
                    "detail",
                    models.TextField(blank=True, help_text="Error messages or sync metadata."),
                ),
                (
                    "task_uid",
                    models.CharField(
                        blank=True,
                        help_text="Engine-side task ID for async operations.",
                        max_length=100,
                    ),
                ),
                ("completed_at", models.DateTimeField(blank=True, null=True)),
                (
                    "index",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="sync_logs",
                        to="icv_search.searchindex",
                    ),
                ),
            ],
            options={
                "verbose_name": "index sync log",
                "verbose_name_plural": "index sync logs",
                "ordering": ["-created_at"],
            },
        ),
    ]
