# Generated by Django 5.0.14 on 2026-02-16 11:02

import uuid

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


class Migration(migrations.Migration):

    initial = True

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

    operations = [
        migrations.CreateModel(
            name="ChatConversation",
            fields=[
                (
                    "id",
                    models.UUIDField(
                        default=uuid.uuid4,
                        editable=False,
                        primary_key=True,
                        serialize=False,
                    ),
                ),
                (
                    "session_id",
                    models.CharField(
                        db_index=True,
                        help_text="ChatAgent session ID",
                        max_length=255,
                    ),
                ),
                (
                    "app_name",
                    models.CharField(
                        db_index=True,
                        help_text="e.g. drifttales, bfagent, weltenhub",
                        max_length=50,
                    ),
                ),
                (
                    "tenant_id",
                    models.UUIDField(
                        blank=True,
                        db_index=True,
                        null=True,
                    ),
                ),
                (
                    "goal_type",
                    models.CharField(
                        choices=[
                            ("trip_planning", "Reiseplanung"),
                            ("story_config", "Story-Konfiguration"),
                            ("enrichment", "Anreicherung"),
                            ("chapter_writing", "Kapitel-Schreiben"),
                            ("research", "Recherche"),
                            ("general", "Allgemein"),
                        ],
                        default="general",
                        max_length=30,
                    ),
                ),
                (
                    "goal_summary",
                    models.TextField(
                        blank=True,
                        help_text="Auto-extracted or manually set conversation goal",
                    ),
                ),
                (
                    "outcome_status",
                    models.CharField(
                        choices=[
                            ("completed", "Erfolgreich"),
                            ("partial", "Teilweise"),
                            ("abandoned", "Abgebrochen"),
                            ("error", "Fehlgeschlagen"),
                        ],
                        default="partial",
                        max_length=20,
                    ),
                ),
                ("outcome_summary", models.TextField(blank=True)),
                (
                    "outcome_artifacts",
                    models.JSONField(
                        blank=True,
                        default=dict,
                        help_text='e.g. {"trip_id": 42, "stops_created": 3}',
                    ),
                ),
                (
                    "satisfaction_rating",
                    models.PositiveSmallIntegerField(
                        blank=True,
                        help_text="User rating 1-5 (if collected)",
                        null=True,
                    ),
                ),
                (
                    "review_status",
                    models.CharField(
                        choices=[
                            ("pending", "Offen"),
                            ("reviewed", "Gepr\u00fcft"),
                            ("action_taken", "Ma\u00dfnahme eingeleitet"),
                        ],
                        default="pending",
                        max_length=20,
                    ),
                ),
                ("review_notes", models.TextField(blank=True)),
                (
                    "improvement_tags",
                    models.JSONField(
                        blank=True,
                        default=list,
                        help_text='e.g. ["prompt_unclear", "wrong_tool", "slow"]',
                    ),
                ),
                ("message_count", models.PositiveIntegerField(default=0)),
                ("total_tokens", models.PositiveIntegerField(default=0)),
                ("total_tool_calls", models.PositiveIntegerField(default=0)),
                ("total_latency_ms", models.PositiveIntegerField(default=0)),
                (
                    "models_used",
                    models.JSONField(
                        blank=True,
                        default=list,
                        help_text="List of LLM model names used",
                    ),
                ),
                ("started_at", models.DateTimeField(auto_now_add=True)),
                ("ended_at", models.DateTimeField(blank=True, null=True)),
                (
                    "user",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="chat_conversations",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "ordering": ["-started_at"],
            },
        ),
        migrations.CreateModel(
            name="ChatMessage",
            fields=[
                (
                    "id",
                    models.UUIDField(
                        default=uuid.uuid4,
                        editable=False,
                        primary_key=True,
                        serialize=False,
                    ),
                ),
                (
                    "role",
                    models.CharField(
                        choices=[
                            ("system", "System"),
                            ("user", "User"),
                            ("assistant", "Assistant"),
                            ("tool", "Tool"),
                        ],
                        max_length=10,
                    ),
                ),
                ("content", models.TextField(blank=True, default="")),
                ("model", models.CharField(blank=True, max_length=100)),
                (
                    "tool_calls",
                    models.JSONField(
                        blank=True,
                        default=list,
                        help_text="Tool calls made in this message",
                    ),
                ),
                (
                    "tool_call_id",
                    models.CharField(
                        blank=True,
                        help_text="For tool-result messages",
                        max_length=100,
                    ),
                ),
                (
                    "name",
                    models.CharField(
                        blank=True,
                        help_text="Tool name (tool role only)",
                        max_length=100,
                    ),
                ),
                ("tokens_used", models.PositiveIntegerField(default=0)),
                ("latency_ms", models.PositiveIntegerField(default=0)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                (
                    "conversation",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="messages",
                        to="chat_logging.chatconversation",
                    ),
                ),
            ],
            options={
                "ordering": ["created_at"],
            },
        ),
        migrations.CreateModel(
            name="EvaluationScore",
            fields=[
                (
                    "id",
                    models.UUIDField(
                        default=uuid.uuid4,
                        editable=False,
                        primary_key=True,
                        serialize=False,
                    ),
                ),
                (
                    "evaluator",
                    models.CharField(
                        choices=[
                            ("deepeval", "DeepEval"),
                            ("langfuse", "LangFuse"),
                            ("custom", "Custom"),
                        ],
                        max_length=20,
                    ),
                ),
                (
                    "metric_name",
                    models.CharField(
                        help_text="e.g. answer_relevancy, tool_correctness",
                        max_length=100,
                    ),
                ),
                (
                    "score",
                    models.FloatField(
                        help_text="Score value (0.0-1.0 for most metrics)",
                    ),
                ),
                (
                    "reason",
                    models.TextField(
                        blank=True,
                        help_text="LLM-generated explanation for the score",
                    ),
                ),
                ("metadata", models.JSONField(blank=True, default=dict)),
                ("evaluated_at", models.DateTimeField(auto_now_add=True)),
                (
                    "conversation",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="evaluation_scores",
                        to="chat_logging.chatconversation",
                    ),
                ),
            ],
            options={
                "ordering": ["-evaluated_at"],
            },
        ),
        migrations.CreateModel(
            name="UseCaseCandidate",
            fields=[
                (
                    "id",
                    models.UUIDField(
                        default=uuid.uuid4,
                        editable=False,
                        primary_key=True,
                        serialize=False,
                    ),
                ),
                (
                    "detection_method",
                    models.CharField(
                        choices=[
                            ("no_tool_match", "Kein passendes Tool"),
                            ("explicit_decline", "Agent lehnt ab"),
                            ("repeated_rephrase", "User wiederholt sich"),
                            ("session_abandoned", "Session abgebrochen"),
                            ("tool_error", "Tool-Fehler"),
                            ("manual", "Manuell markiert"),
                        ],
                        max_length=30,
                    ),
                ),
                (
                    "user_intent",
                    models.TextField(
                        help_text="What the user wanted to achieve",
                    ),
                ),
                (
                    "app_name",
                    models.CharField(db_index=True, max_length=50),
                ),
                (
                    "frequency",
                    models.PositiveIntegerField(
                        default=1,
                        help_text="How often this use case was detected",
                    ),
                ),
                (
                    "status",
                    models.CharField(
                        choices=[
                            ("new", "Neu"),
                            ("confirmed", "Best\u00e4tigt"),
                            ("planned", "Geplant"),
                            ("implemented", "Umgesetzt"),
                            ("rejected", "Abgelehnt"),
                        ],
                        default="new",
                        max_length=20,
                    ),
                ),
                (
                    "priority",
                    models.CharField(
                        choices=[
                            ("low", "Niedrig"),
                            ("medium", "Mittel"),
                            ("high", "Hoch"),
                        ],
                        default="medium",
                        max_length=10,
                    ),
                ),
                ("notes", models.TextField(blank=True)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
                (
                    "conversation",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="use_case_candidates",
                        to="chat_logging.chatconversation",
                    ),
                ),
                (
                    "trigger_message",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        to="chat_logging.chatmessage",
                    ),
                ),
            ],
            options={
                "ordering": ["-frequency", "-created_at"],
            },
        ),
        migrations.AddIndex(
            model_name="chatconversation",
            index=models.Index(
                fields=["app_name", "started_at"],
                name="chat_loggin_app_nam_a2d251_idx",
            ),
        ),
        migrations.AddIndex(
            model_name="chatconversation",
            index=models.Index(
                fields=["outcome_status", "app_name"],
                name="chat_loggin_outcome_ce5ca4_idx",
            ),
        ),
        migrations.AddIndex(
            model_name="chatconversation",
            index=models.Index(
                fields=["review_status"],
                name="chat_loggin_review__811cc8_idx",
            ),
        ),
        migrations.AddIndex(
            model_name="chatconversation",
            index=models.Index(
                fields=["goal_type", "app_name"],
                name="chat_loggin_goal_ty_caab03_idx",
            ),
        ),
        migrations.AddIndex(
            model_name="chatmessage",
            index=models.Index(
                fields=["conversation", "created_at"],
                name="chat_loggin_convers_ce06dc_idx",
            ),
        ),
        migrations.AddIndex(
            model_name="chatmessage",
            index=models.Index(
                fields=["role"],
                name="chat_loggin_role_7f41f9_idx",
            ),
        ),
        migrations.AddIndex(
            model_name="evaluationscore",
            index=models.Index(
                fields=["conversation", "metric_name"],
                name="chat_loggin_convers_c0a262_idx",
            ),
        ),
        migrations.AddIndex(
            model_name="evaluationscore",
            index=models.Index(
                fields=["evaluator", "metric_name"],
                name="chat_loggin_evaluat_17e27c_idx",
            ),
        ),
        migrations.AddConstraint(
            model_name="evaluationscore",
            constraint=models.UniqueConstraint(
                fields=("conversation", "evaluator", "metric_name"),
                name="unique_eval_per_conversation",
            ),
        ),
        migrations.AddIndex(
            model_name="usecasecandidate",
            index=models.Index(
                fields=["status", "app_name"],
                name="chat_loggin_status_8d5d1f_idx",
            ),
        ),
        migrations.AddIndex(
            model_name="usecasecandidate",
            index=models.Index(
                fields=["detection_method"],
                name="chat_loggin_detecti_2f0981_idx",
            ),
        ),
    ]
