"""
Migration 0056: AI Agent Models (AgentConfig, ChatSession, ChatMessage)
+ can_use_agent Permission
"""
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
import uuid


class Migration(migrations.Migration):

    dependencies = [
        ("stiftung", "0055_add_import_types_for_unified_import_export"),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name="AgentConfig",
            fields=[
                (
                    "id",
                    models.AutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "provider",
                    models.CharField(
                        choices=[
                            ("ollama", "Ollama (lokal)"),
                            ("openai", "OpenAI"),
                            ("anthropic", "Anthropic"),
                        ],
                        default="ollama",
                        max_length=20,
                        verbose_name="LLM-Provider",
                    ),
                ),
                (
                    "model_name",
                    models.CharField(
                        default="qwen2.5:3b",
                        max_length=100,
                        verbose_name="Modell-Name",
                    ),
                ),
                (
                    "ollama_url",
                    models.CharField(
                        default="http://ollama:11434",
                        max_length=255,
                        verbose_name="Ollama-URL",
                    ),
                ),
                (
                    "openai_api_key",
                    models.CharField(
                        blank=True,
                        max_length=255,
                        verbose_name="OpenAI API-Key",
                    ),
                ),
                (
                    "anthropic_api_key",
                    models.CharField(
                        blank=True,
                        max_length=255,
                        verbose_name="Anthropic API-Key",
                    ),
                ),
                (
                    "system_prompt",
                    models.TextField(verbose_name="System-Prompt"),
                ),
                (
                    "allow_write",
                    models.BooleanField(
                        default=False,
                        verbose_name="Schreib-Tools erlaubt",
                    ),
                ),
                (
                    "chat_retention_days",
                    models.IntegerField(
                        default=30,
                        verbose_name="Chat-Verlauf Aufbewahrung (Tage)",
                    ),
                ),
            ],
            options={
                "verbose_name": "Agent-Konfiguration",
                "verbose_name_plural": "Agent-Konfiguration",
            },
        ),
        migrations.CreateModel(
            name="ChatSession",
            fields=[
                (
                    "id",
                    models.UUIDField(
                        default=uuid.uuid4,
                        editable=False,
                        primary_key=True,
                        serialize=False,
                    ),
                ),
                (
                    "title",
                    models.CharField(
                        blank=True,
                        max_length=200,
                        verbose_name="Titel",
                    ),
                ),
                (
                    "created_at",
                    models.DateTimeField(auto_now_add=True, verbose_name="Erstellt"),
                ),
                (
                    "updated_at",
                    models.DateTimeField(auto_now=True, verbose_name="Zuletzt aktiv"),
                ),
                (
                    "user",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="agent_sessions",
                        to=settings.AUTH_USER_MODEL,
                        verbose_name="Benutzer",
                    ),
                ),
            ],
            options={
                "verbose_name": "Chat-Sitzung",
                "verbose_name_plural": "Chat-Sitzungen",
                "ordering": ["-updated_at"],
            },
        ),
        migrations.CreateModel(
            name="ChatMessage",
            fields=[
                (
                    "id",
                    models.UUIDField(
                        default=uuid.uuid4,
                        editable=False,
                        primary_key=True,
                        serialize=False,
                    ),
                ),
                (
                    "role",
                    models.CharField(
                        choices=[
                            ("user", "Benutzer"),
                            ("assistant", "Assistent"),
                            ("tool", "Tool-Ergebnis"),
                        ],
                        max_length=20,
                        verbose_name="Rolle",
                    ),
                ),
                (
                    "content",
                    models.TextField(verbose_name="Inhalt"),
                ),
                (
                    "tool_name",
                    models.CharField(
                        blank=True,
                        max_length=100,
                        verbose_name="Tool-Name",
                    ),
                ),
                (
                    "tool_call_id",
                    models.CharField(
                        blank=True,
                        max_length=100,
                        verbose_name="Tool-Call-ID",
                    ),
                ),
                (
                    "created_at",
                    models.DateTimeField(auto_now_add=True, verbose_name="Erstellt"),
                ),
                (
                    "session",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="messages",
                        to="stiftung.chatsession",
                        verbose_name="Sitzung",
                    ),
                ),
            ],
            options={
                "verbose_name": "Chat-Nachricht",
                "verbose_name_plural": "Chat-Nachrichten",
                "ordering": ["created_at"],
            },
        ),
        # Update ApplicationPermission to add can_use_agent
        # (No DB table change needed — this is a managed=False model)
        # The permission is added via the Meta.permissions list in system.py
    ]
