"""Tests for PI FYAD views."""

from django.contrib.auth.models import Permission, User
from django.test import TestCase, RequestFactory
from django.urls import reverse

from allianceauth.eveonline.models import EveCharacter

from aa_pi_fyad.models import CharacterOwner, CharacterPlanet, PlanetTemplate


class ViewTestBase(TestCase):
    """Base class for view tests with common setup."""

    @classmethod
    def setUpTestData(cls):
        cls.user = User.objects.create_user(
            username="testuser",
            password="testpass123",
        )
        cls.character = EveCharacter.objects.create(
            character_id=90000010,
            character_name="View Pilot",
            corporation_id=98000010,
            corporation_name="View Corp",
        )

        # Grant basic PI permission
        perm = Permission.objects.get(codename="view_pi")
        cls.user.user_permissions.add(perm)

        cls.owner = CharacterOwner.objects.create(character=cls.character)
        cls.planet = CharacterPlanet.objects.create(
            owner=cls.owner,
            planet_id=40000010,
            planet_type_id=2014,
            planet_name="Temperate X",
            solar_system_id=30000010,
            solar_system_name="View System",
        )


class TestDashboardView(ViewTestBase):
    """Tests for the dashboard view."""

    def test_dashboard_requires_login(self):
        """Dashboard redirects anonymous users."""
        response = self.client.get(reverse("pi_fyad:dashboard"))
        self.assertEqual(response.status_code, 302)

    def test_dashboard_requires_permission(self):
        """Dashboard requires view_pi permission."""
        user_no_perm = User.objects.create_user(
            username="noperm", password="testpass123"
        )
        self.client.force_login(user_no_perm)
        response = self.client.get(reverse("pi_fyad:dashboard"))
        self.assertEqual(response.status_code, 403)

    def test_dashboard_accessible(self):
        """Dashboard loads for authorized user."""
        self.client.force_login(self.user)
        response = self.client.get(reverse("pi_fyad:dashboard"))
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, "pi_fyad/dashboard.html")


class TestTemplateViews(ViewTestBase):
    """Tests for template views."""

    @classmethod
    def setUpTestData(cls):
        super().setUpTestData()
        perm = Permission.objects.get(codename="manage_templates")
        cls.user.user_permissions.add(perm)

    def test_template_list(self):
        """Template list loads for authorized user."""
        self.client.force_login(self.user)
        response = self.client.get(reverse("pi_fyad:template_list"))
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, "pi_fyad/template_list.html")

    def test_template_create_get(self):
        """Template create form loads."""
        self.client.force_login(self.user)
        response = self.client.get(reverse("pi_fyad:template_create"))
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, "pi_fyad/template_create.html")
        self.assertIn("form", response.context)
