# Django
from django.apps import apps
from django.test import SimpleTestCase
from django.test.utils import override_settings

from incursions import __version__
from incursions.app_settings import EVECATEGORY_ALL, get_site_url, insert_sse_subdomain


class TestAppSettings(SimpleTestCase):
    def test_get_site_url_uses_callback_origin(self) -> None:
        with override_settings(ESI_SSO_CALLBACK_URL="https://auth.example.com/sso/callback"):
            self.assertEqual(get_site_url(), "https://auth.example.com")

    def test_get_site_url_falls_back_to_site_url(self) -> None:
        with override_settings(SITE_URL="https://alliance.example.com", ESI_SSO_CALLBACK_URL=None):
            self.assertEqual(get_site_url(), "https://alliance.example.com")

    def test_get_site_url_rejects_non_url_callback(self) -> None:
        with override_settings(
            SITE_URL="https://alliance.example.com",
            ESI_SSO_CALLBACK_URL="not-a-valid-url",
        ):
            self.assertEqual(get_site_url(), "https://alliance.example.com")

    def test_insert_sse_subdomain_preserves_port(self) -> None:
        self.assertEqual(
            insert_sse_subdomain("http://localhost:8000"),
            "http://sse.localhost:8000",
        )

    def test_insert_sse_subdomain_without_port(self) -> None:
        self.assertEqual(
            insert_sse_subdomain("https://auth.example.com"),
            "https://sse.auth.example.com",
        )

    def test_evecategory_all_contains_expected_categories(self) -> None:
        self.assertEqual(EVECATEGORY_ALL, (6, 7, 8, 18, 16, 20, 22))


class TestAppConfig(SimpleTestCase):
    def test_app_config_uses_expected_verbose_name(self) -> None:
        app_config = apps.get_app_config("incursions")

        self.assertEqual(app_config.name, "incursions")
        self.assertEqual(app_config.verbose_name, f"AA Incursions v{__version__}")
