# Generated by Django 4.2.23 on 2025-06-24 13:09

import uuid

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


class Migration(migrations.Migration):
    initial = True

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

    operations = [
        migrations.CreateModel(
            name="Workflow",
            fields=[
                (
                    "id",
                    models.UUIDField(
                        default=uuid.uuid4,
                        editable=False,
                        primary_key=True,
                        serialize=False,
                    ),
                ),
                (
                    "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,
                        help_text="A brief description of what this workflow does.",
                    ),
                ),
                (
                    "workflow_definition",
                    models.JSONField(
                        default=dict,
                        help_text="JSON array defining the sequence of LangChain components (prompt, llm, parser).",
                    ),
                ),
                (
                    "is_active",
                    models.BooleanField(
                        default=False,
                        help_text="Only one workflow with a given 'name' should be active at any time.",
                    ),
                ),
            ],
            options={
                "verbose_name": "Workflow",
                "verbose_name_plural": "Workflows",
                "ordering": ["name"],
                "unique_together": {("name", "is_active")},
            },
        ),
        migrations.CreateModel(
            name="UserInteraction",
            fields=[
                (
                    "id",
                    models.UUIDField(
                        default=uuid.uuid4,
                        editable=False,
                        primary_key=True,
                        serialize=False,
                    ),
                ),
                (
                    "user_identifier",
                    models.CharField(
                        blank=True,
                        help_text="Identifier for the user (e.g., session ID, user ID if authenticated).",
                        max_length=255,
                        null=True,
                    ),
                ),
                (
                    "session_id",
                    models.UUIDField(
                        blank=True,
                        default=uuid.uuid4,
                        help_text="A unique ID to group related user interactions across a session.",
                        null=True,
                    ),
                ),
                (
                    "input_data",
                    models.JSONField(
                        help_text="The full input JSON received from the user for this interaction."
                    ),
                ),
                (
                    "llm_output",
                    models.JSONField(
                        blank=True,
                        help_text="The final output JSON/text from the LLM workflow.",
                        null=True,
                    ),
                ),
                (
                    "total_cost_estimate",
                    models.DecimalField(
                        decimal_places=8,
                        default=0.0,
                        help_text="Estimated total cost for this interaction (e.g., based on token usage).",
                        max_digits=10,
                    ),
                ),
                (
                    "total_duration_ms",
                    models.PositiveIntegerField(
                        default=0,
                        help_text="Total execution time for the workflow in milliseconds.",
                    ),
                ),
                (
                    "status",
                    models.CharField(
                        default="success",
                        help_text="Overall status of the interaction (e.g., 'success', 'failure').",
                        max_length=50,
                    ),
                ),
                (
                    "error_message",
                    models.TextField(
                        blank=True,
                        help_text="Detailed error message if the interaction failed.",
                        null=True,
                    ),
                ),
                (
                    "workflow",
                    models.ForeignKey(
                        blank=True,
                        help_text="The workflow that was executed for this interaction.",
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        to="django_chain.workflow",
                    ),
                ),
            ],
            options={
                "verbose_name": "User Interaction",
                "verbose_name_plural": "User Interactions",
                "ordering": ["-id"],
            },
        ),
        migrations.CreateModel(
            name="Prompt",
            fields=[
                (
                    "id",
                    models.UUIDField(
                        default=uuid.uuid4,
                        editable=False,
                        primary_key=True,
                        serialize=False,
                        unique=True,
                    ),
                ),
                ("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').",
                    ),
                ),
                ("version", models.PositiveIntegerField(default=1)),
                ("is_active", models.BooleanField(default=False)),
                (
                    "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": ["name", "-version"],
                "unique_together": {("name", "is_active"), ("name", "version")},
            },
        ),
        migrations.CreateModel(
            name="ChatSession",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "session_id",
                    models.CharField(
                        help_text="UUID for anonymous sessions or custom session tracking",
                        max_length=100,
                        unique=True,
                        verbose_name="session id",
                    ),
                ),
                (
                    "title",
                    models.CharField(
                        blank=True,
                        help_text="A user-friendly title for the chat",
                        max_length=200,
                        null=True,
                        verbose_name="title",
                    ),
                ),
                (
                    "llm_config",
                    models.JSONField(
                        default=dict,
                        help_text="Specific LLM configuration for this session",
                        verbose_name="LLM config",
                    ),
                ),
                (
                    "created_at",
                    models.DateTimeField(auto_now_add=True, verbose_name="created at"),
                ),
                (
                    "updated_at",
                    models.DateTimeField(auto_now=True, verbose_name="updated at"),
                ),
                (
                    "user",
                    models.ForeignKey(
                        blank=True,
                        help_text="User associated with this chat session",
                        null=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "verbose_name": "Chat Session",
                "verbose_name_plural": "Chat Sessions",
                "ordering": ["-updated_at"],
            },
        ),
        migrations.CreateModel(
            name="ChatMessage",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("content", models.TextField(verbose_name="content")),
                (
                    "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, verbose_name="timestamp"),
                ),
                (
                    "token_count",
                    models.IntegerField(blank=True, null=True, verbose_name="token count"),
                ),
                (
                    "order",
                    models.IntegerField(
                        default=0,
                        help_text="For ordering in case of simultaneous writes",
                        verbose_name="order",
                    ),
                ),
                (
                    "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.CreateModel(
            name="LLMInteractionLog",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("prompt_text", models.TextField(verbose_name="prompt text")),
                ("response_text", models.TextField(verbose_name="response text")),
                (
                    "model_name",
                    models.CharField(
                        help_text="Name of the LLM model used",
                        max_length=100,
                        verbose_name="model name",
                    ),
                ),
                (
                    "provider",
                    models.CharField(
                        help_text="LLM provider (e.g., openai, google)",
                        max_length=50,
                        verbose_name="provider",
                    ),
                ),
                (
                    "input_tokens",
                    models.IntegerField(blank=True, null=True, verbose_name="input tokens"),
                ),
                (
                    "output_tokens",
                    models.IntegerField(blank=True, null=True, verbose_name="output tokens"),
                ),
                (
                    "total_cost",
                    models.DecimalField(
                        blank=True,
                        decimal_places=8,
                        help_text="Estimated cost of the interaction in USD",
                        max_digits=10,
                        null=True,
                        verbose_name="total cost",
                    ),
                ),
                (
                    "latency_ms",
                    models.IntegerField(
                        blank=True,
                        help_text="Latency of the LLM API call in milliseconds",
                        null=True,
                        verbose_name="latency ms",
                    ),
                ),
                (
                    "status",
                    models.CharField(
                        choices=[("success", "Success"), ("error", "Error")],
                        default="success",
                        max_length=20,
                        verbose_name="status",
                    ),
                ),
                (
                    "error_message",
                    models.TextField(
                        blank=True,
                        help_text="Error message if the interaction failed",
                        null=True,
                        verbose_name="error message",
                    ),
                ),
                (
                    "created_at",
                    models.DateTimeField(auto_now_add=True, verbose_name="created at"),
                ),
                (
                    "metadata",
                    models.JSONField(
                        blank=True,
                        default=dict,
                        help_text="Additional arbitrary metadata",
                        verbose_name="metadata",
                    ),
                ),
                (
                    "user",
                    models.ForeignKey(
                        blank=True,
                        help_text="User who initiated the interaction",
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "verbose_name": "LLM Interaction Log",
                "verbose_name_plural": "LLM Interaction Logs",
                "ordering": ["-created_at"],
                "indexes": [
                    models.Index(fields=["created_at"], name="django_chai_created_f55512_idx"),
                    models.Index(
                        fields=["user", "created_at"],
                        name="django_chai_user_id_127566_idx",
                    ),
                ],
            },
        ),
        migrations.CreateModel(
            name="InteractionLog",
            fields=[
                (
                    "id",
                    models.UUIDField(
                        default=uuid.uuid4,
                        editable=False,
                        primary_key=True,
                        serialize=False,
                    ),
                ),
                (
                    "step_order",
                    models.PositiveIntegerField(
                        help_text="The sequential order of this step within the workflow execution."
                    ),
                ),
                (
                    "step_type",
                    models.CharField(
                        help_text="Type of the LangChain component (e.g., 'prompt', 'llm', 'parser').",
                        max_length=50,
                    ),
                ),
                (
                    "component_name",
                    models.CharField(
                        blank=True,
                        help_text="Name of the specific component used (e.g., Prompt name, LLM model name).",
                        max_length=255,
                        null=True,
                    ),
                ),
                (
                    "input_to_step",
                    models.JSONField(
                        blank=True,
                        help_text="The input payload received by this specific component in the chain.",
                        null=True,
                    ),
                ),
                (
                    "output_from_step",
                    models.JSONField(
                        blank=True,
                        help_text="The output payload generated by this specific component in the chain.",
                        null=True,
                    ),
                ),
                (
                    "metadata",
                    models.JSONField(
                        blank=True,
                        help_text="Additional step-specific metadata (e.g., token usage for LLM, finish reason).",
                        null=True,
                    ),
                ),
                (
                    "duration_ms",
                    models.PositiveIntegerField(
                        default=0,
                        help_text="Time taken for this specific step in milliseconds.",
                    ),
                ),
                (
                    "status",
                    models.CharField(
                        default="success",
                        help_text="Status of this individual step ('success', 'failure').",
                        max_length=50,
                    ),
                ),
                (
                    "error_message",
                    models.TextField(
                        blank=True,
                        help_text="Error message if this step failed.",
                        null=True,
                    ),
                ),
                (
                    "user_interaction",
                    models.ForeignKey(
                        help_text="The parent user interaction this log entry belongs to.",
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="logs",
                        to="django_chain.userinteraction",
                    ),
                ),
            ],
            options={
                "verbose_name": "Interaction Log",
                "verbose_name_plural": "Interaction Logs",
                "ordering": ["user_interaction", "step_order"],
                "unique_together": {("user_interaction", "step_order")},
            },
        ),
        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="chatmessage",
            index=models.Index(
                fields=["session", "timestamp"], name="django_chai_session_44c56e_idx"
            ),
        ),
    ]
