# Generated by Django 4.2.23 on 2025-07-22 20:22

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
import django_chain.validators
import uuid


class Migration(migrations.Migration):
    initial = True

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name="Prompt",
            fields=[
                ("created_at", models.DateTimeField(auto_now_add=True, db_index=True)),
                ("updated_at", models.DateTimeField(auto_now=True, db_index=True)),
                (
                    "id",
                    models.UUIDField(
                        default=uuid.uuid4,
                        editable=False,
                        primary_key=True,
                        serialize=False,
                        unique=True,
                    ),
                ),
                ("created_by", models.UUIDField(blank=True, editable=False, null=True)),
                ("updated_by", models.UUIDField(blank=True, null=True)),
                ("version", models.PositiveIntegerField(default=1)),
                (
                    "is_active",
                    models.BooleanField(
                        default=False,
                        help_text="Activates/Deactivates the object, only one object of a given 'name' should be active at a given time",
                    ),
                ),
                ("name", models.CharField(max_length=255, unique=True)),
                (
                    "prompt_template",
                    models.JSONField(
                        default=dict,
                        help_text="JSON representation of the LangChain prompt. Must include 'langchain_type' (e.g., 'PromptTemplate', 'ChatPromptTemplate').",
                        validators=[django_chain.validators.validate_prompt],
                    ),
                ),
                (
                    "input_variables",
                    models.JSONField(
                        blank=True, help_text="Input variables to the prompt", null=True
                    ),
                ),
                (
                    "optional_variables",
                    models.JSONField(
                        blank=True, help_text="Input variables to the prompt", null=True
                    ),
                ),
            ],
            options={
                "verbose_name": "Prompt",
                "verbose_name_plural": "Prompts",
                "ordering": ("-updated_at", "-created_at", "name", "-version"),
            },
        ),
        migrations.CreateModel(
            name="Workflow",
            fields=[
                ("created_at", models.DateTimeField(auto_now_add=True, db_index=True)),
                ("updated_at", models.DateTimeField(auto_now=True, db_index=True)),
                (
                    "id",
                    models.UUIDField(
                        default=uuid.uuid4,
                        editable=False,
                        primary_key=True,
                        serialize=False,
                        unique=True,
                    ),
                ),
                ("created_by", models.UUIDField(blank=True, editable=False, null=True)),
                ("updated_by", models.UUIDField(blank=True, null=True)),
                ("version", models.PositiveIntegerField(default=1)),
                (
                    "is_active",
                    models.BooleanField(
                        default=False,
                        help_text="Activates/Deactivates the object, only one object of a given 'name' should be active at a given time",
                    ),
                ),
                (
                    "name",
                    models.CharField(
                        help_text="A unique name for this workflow (e.g., 'SummaryGenerator', 'CustomerServiceChatbot').",
                        max_length=255,
                        unique=True,
                    ),
                ),
                ("description", models.TextField(blank=True, null=True)),
                (
                    "workflow_definition",
                    models.JSONField(
                        default=dict,
                        help_text="JSON array defining the sequence of LangChain components (prompt, llm, parser).",
                        validators=[django_chain.validators.validate_workflow],
                    ),
                ),
                (
                    "prompt",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        to="django_chain.prompt",
                    ),
                ),
            ],
            options={
                "verbose_name": "Workflow",
                "verbose_name_plural": "Workflows",
                "ordering": ("-updated_at", "-created_at", "name", "-version"),
            },
        ),
        migrations.CreateModel(
            name="InteractionLog",
            fields=[
                ("created_at", models.DateTimeField(auto_now_add=True, db_index=True)),
                ("updated_at", models.DateTimeField(auto_now=True, db_index=True)),
                (
                    "id",
                    models.UUIDField(
                        default=uuid.uuid4,
                        editable=False,
                        primary_key=True,
                        serialize=False,
                    ),
                ),
                ("prompt_text", models.JSONField(blank=True, default=dict, null=True)),
                ("response_text", models.TextField(blank=True, null=True)),
                (
                    "model_name",
                    models.CharField(help_text="Name of the LLM model used", max_length=100),
                ),
                ("provider", models.CharField(max_length=50)),
                ("input_tokens", models.IntegerField(blank=True, null=True)),
                ("output_tokens", models.IntegerField(blank=True, null=True)),
                (
                    "model_parameters",
                    models.JSONField(blank=True, default=dict, null=True),
                ),
                ("latency_ms", models.IntegerField(blank=True, null=True)),
                (
                    "status",
                    models.CharField(
                        choices=[
                            ("PROCESSING", "Request is processing"),
                            ("SUCCESS", "Request has succeeded"),
                            ("FAILURE", "Request has failed"),
                        ],
                        default="PROCESSING",
                        max_length=20,
                    ),
                ),
                ("error_message", models.TextField(blank=True, null=True)),
                ("metadata", models.JSONField(blank=True, default=dict, null=True)),
                (
                    "user",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
                (
                    "workflow",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        to="django_chain.workflow",
                    ),
                ),
            ],
            options={
                "verbose_name": "LLM Interaction Log",
                "verbose_name_plural": "LLM Interaction Logs",
                "ordering": ["-created_at"],
            },
        ),
        migrations.CreateModel(
            name="ChatSession",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("created_at", models.DateTimeField(auto_now_add=True, db_index=True)),
                ("updated_at", models.DateTimeField(auto_now=True, db_index=True)),
                (
                    "session_id",
                    models.CharField(
                        help_text="UUID for anonymous sessions or custom session tracking",
                        max_length=100,
                        unique=True,
                    ),
                ),
                (
                    "title",
                    models.CharField(
                        blank=True,
                        help_text="A user-friendly title for the chat",
                        max_length=200,
                        null=True,
                    ),
                ),
                (
                    "is_active",
                    models.BooleanField(
                        db_index=True,
                        default=True,
                        help_text="Whether the session is currently active (for archiving)",
                    ),
                ),
                (
                    "user",
                    models.ForeignKey(
                        blank=True,
                        help_text="User associated with this chat session",
                        null=True,
                        on_delete=django.db.models.deletion.PROTECT,
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
                (
                    "workflow",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.PROTECT,
                        to="django_chain.workflow",
                    ),
                ),
            ],
            options={
                "verbose_name": "Chat Session",
                "verbose_name_plural": "Chat Sessions",
                "ordering": ("-updated_at", "-created_at"),
            },
        ),
        migrations.CreateModel(
            name="ChatHistory",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("content", models.TextField()),
                (
                    "role",
                    models.CharField(
                        choices=[
                            ("USER", "User template"),
                            ("ASSISTANT", "Assistant Template"),
                            ("SYSTEM", "System Template"),
                        ],
                        default="USER",
                        max_length=10,
                    ),
                ),
                ("timestamp", models.DateTimeField(auto_now_add=True)),
                ("token_count", models.IntegerField(blank=True, null=True)),
                (
                    "order",
                    models.IntegerField(
                        default=0,
                        help_text="For ordering in case of simultaneous writes",
                        verbose_name="order",
                    ),
                ),
                (
                    "is_deleted",
                    models.BooleanField(
                        db_index=True,
                        default=False,
                        help_text="Whether the message has been soft deleted for privacy compliance",
                    ),
                ),
                (
                    "session",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="messages",
                        to="django_chain.chatsession",
                    ),
                ),
            ],
            options={
                "verbose_name": "Chat Message",
                "verbose_name_plural": "Chat Messages",
                "ordering": ["timestamp"],
            },
        ),
        migrations.AddIndex(
            model_name="chatsession",
            index=models.Index(
                fields=["user", "created_at"], name="django_chai_user_id_855317_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="chatsession",
            index=models.Index(fields=["session_id"], name="django_chai_session_d72c30_idx"),
        ),
        migrations.AddIndex(
            model_name="chatsession",
            index=models.Index(fields=["is_active", "user"], name="django_chai_is_acti_b5db55_idx"),
        ),
        migrations.AddIndex(
            model_name="chatsession",
            index=models.Index(
                fields=["is_active", "created_at"],
                name="django_chai_is_acti_613284_idx",
            ),
        ),
        migrations.AddIndex(
            model_name="chathistory",
            index=models.Index(
                fields=["session", "timestamp"], name="django_chai_session_ae48ef_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="chathistory",
            index=models.Index(
                fields=["session", "is_deleted"], name="django_chai_session_61a981_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="chathistory",
            index=models.Index(
                fields=["is_deleted", "timestamp"],
                name="django_chai_is_dele_115554_idx",
            ),
        ),
    ]
