"""
Tests for LLM Manager views — FBV-based.
"""

from django.contrib.auth import get_user_model
from django.test import TestCase
from django.urls import reverse

from llm_manager.models import LLMApi, LLMModel

User = get_user_model()


class LLMDashboardViewTest(TestCase):
    """Tests for the LLM Manager dashboard."""

    def setUp(self):
        self.user = User.objects.create_user(username="testuser", password="testpass123")
        self.client.login(username="testuser", password="testpass123")

    def test_dashboard_requires_login(self):
        self.client.logout()
        resp = self.client.get(reverse("llm_manager:dashboard"))
        self.assertEqual(resp.status_code, 302)

    def test_dashboard_renders(self):
        resp = self.client.get(reverse("llm_manager:dashboard"))
        self.assertEqual(resp.status_code, 200)
        self.assertContains(resp, "LLM Manager")


class LLMApiViewTest(TestCase):
    """Tests for LLMApi CRUD views."""

    def setUp(self):
        self.user = User.objects.create_user(username="testuser", password="testpass123")
        self.client.login(username="testuser", password="testpass123")
        self.api = LLMApi.objects.create(
            name="Test API",
            api_type="openai",
            base_url="https://api.example.com/v1",
            created_by=self.user,
        )

    def test_api_list(self):
        resp = self.client.get(reverse("llm_manager:api_list"))
        self.assertEqual(resp.status_code, 200)
        self.assertContains(resp, "Test API")

    def test_api_detail(self):
        resp = self.client.get(reverse("llm_manager:api_detail", kwargs={"pk": self.api.pk}))
        self.assertEqual(resp.status_code, 200)
        self.assertContains(resp, "Test API")

    def test_api_create_get(self):
        resp = self.client.get(reverse("llm_manager:api_create"))
        self.assertEqual(resp.status_code, 200)

    def test_api_create_post(self):
        resp = self.client.post(
            reverse("llm_manager:api_create"),
            {
                "name": "New API",
                "api_type": "ollama",
                "base_url": "http://localhost:11434",
                "is_active": True,
                "supports_streaming": True,
                "timeout_seconds": 60,
                "max_retries": 3,
            },
        )
        self.assertEqual(resp.status_code, 302)
        self.assertTrue(LLMApi.objects.filter(name="New API").exists())

    def test_api_edit(self):
        resp = self.client.post(
            reverse("llm_manager:api_edit", kwargs={"pk": self.api.pk}),
            {
                "name": "Updated API",
                "api_type": "openai",
                "base_url": "https://api.example.com/v2",
                "is_active": True,
                "supports_streaming": True,
                "timeout_seconds": 30,
                "max_retries": 5,
            },
        )
        self.assertEqual(resp.status_code, 302)
        self.api.refresh_from_db()
        self.assertEqual(self.api.name, "Updated API")

    def test_api_delete(self):
        resp = self.client.post(reverse("llm_manager:api_delete", kwargs={"pk": self.api.pk}))
        self.assertEqual(resp.status_code, 302)
        self.assertFalse(LLMApi.objects.filter(pk=self.api.pk).exists())


class LLMModelViewTest(TestCase):
    """Tests for LLMModel CRUD views."""

    def setUp(self):
        self.user = User.objects.create_user(username="testuser", password="testpass123")
        self.client.login(username="testuser", password="testpass123")
        self.api = LLMApi.objects.create(
            name="Test API",
            api_type="openai",
            base_url="https://api.example.com/v1",
        )
        self.model = LLMModel.objects.create(
            api=self.api,
            name="gpt-4o",
            model_type="chat",
            context_window=128000,
        )

    def test_model_list(self):
        resp = self.client.get(reverse("llm_manager:model_list"))
        self.assertEqual(resp.status_code, 200)
        self.assertContains(resp, "gpt-4o")

    def test_model_list_filter_by_api(self):
        resp = self.client.get(reverse("llm_manager:model_list") + f"?api={self.api.pk}")
        self.assertEqual(resp.status_code, 200)
        self.assertContains(resp, "gpt-4o")

    def test_model_detail(self):
        resp = self.client.get(reverse("llm_manager:model_detail", kwargs={"pk": self.model.pk}))
        self.assertEqual(resp.status_code, 200)
        self.assertContains(resp, "gpt-4o")

    def test_model_create(self):
        resp = self.client.post(
            reverse("llm_manager:model_create"),
            {
                "api": str(self.api.pk),
                "name": "gpt-4o-mini",
                "model_type": "chat",
                "context_window": 128000,
                "input_cost_per_1k": "0.000150",
                "output_cost_per_1k": "0.000600",
                "is_active": True,
            },
        )
        self.assertEqual(resp.status_code, 302)
        self.assertTrue(LLMModel.objects.filter(name="gpt-4o-mini").exists())

    def test_model_delete(self):
        resp = self.client.post(reverse("llm_manager:model_delete", kwargs={"pk": self.model.pk}))
        self.assertEqual(resp.status_code, 302)
        self.assertFalse(LLMModel.objects.filter(pk=self.model.pk).exists())


class UsageListViewTest(TestCase):
    """Tests for the usage list view."""

    def setUp(self):
        self.user = User.objects.create_user(username="testuser", password="testpass123")
        self.client.login(username="testuser", password="testpass123")

    def test_usage_list(self):
        resp = self.client.get(reverse("llm_manager:usage_list"))
        self.assertEqual(resp.status_code, 200)
