# Standard Library
from unittest.mock import Mock, patch

# Django
from django.http import Http404
from django.test import RequestFactory

# Alliance Auth (Core)
from allianceauth.eveonline.models import EveCharacter
from allianceauth.utils.testing import NoSocketsTestCase

from incursions.api import implants as implants_api


class FakeAPI:
    def __init__(self) -> None:
        self.routes: dict[str, object] = {}

    def get(self, path: str, **kwargs: object):
        def decorator(func):
            self.routes[path] = func
            return func
        return decorator


def _routes() -> dict[str, object]:
    api = FakeAPI()
    implants_api.setup(api)
    return api.routes


def _user(**perms: bool) -> Mock:
    return Mock(has_perm=Mock(side_effect=lambda perm: perms.get(perm, False)))


class TestListImplants(NoSocketsTestCase):
    def setUp(self) -> None:
        self.factory = RequestFactory()
        self.routes = _routes()
        self.view = self.routes["/implants"]
        self.character = EveCharacter.objects.create(character_id=1001, character_name="Pilot One")

    @patch("incursions.api.implants.get_all_characters_from_user", return_value=[])
    def test_denies_without_permission_or_ownership(self, mock_alts: Mock) -> None:
        request = self.factory.get("/implants")
        request.user = _user()

        response = self.view(request, character_id=self.character.character_id)

        self.assertEqual(response, (403, {"error": "Permission denied"}))

    @patch("incursions.api.implants.get_all_characters_from_user", return_value=[])
    def test_raises_404_when_character_missing(self, mock_alts: Mock) -> None:
        request = self.factory.get("/implants")
        request.user = _user(**{"incursions.waitlist_implants_view": True})

        with self.assertRaises(Http404):
            self.view(request, character_id=999999)

    @patch("incursions.api.implants.get_character_implants")
    @patch("incursions.api.implants.get_all_characters_from_user", return_value=[])
    def test_returns_implants(self, mock_alts: Mock, mock_get_implants: Mock) -> None:
        mock_get_implants.return_value = ([1, 2, 3], Mock())
        request = self.factory.get("/implants")
        request.user = _user(**{"incursions.waitlist_implants_view": True})

        response = self.view(request, character_id=self.character.character_id)

        self.assertEqual(response.implants, [1, 2, 3])
        mock_get_implants.assert_called_once_with(self.character.character_id)

    @patch("incursions.api.implants.get_character_implants")
    @patch("incursions.api.implants.get_all_characters_from_user")
    def test_allows_own_character_without_permission(self, mock_alts: Mock, mock_get_implants: Mock) -> None:
        mock_alts.return_value = [self.character]
        mock_get_implants.return_value = ([], Mock())
        request = self.factory.get("/implants")
        request.user = _user()

        response = self.view(request, character_id=self.character.character_id)

        self.assertEqual(response.implants, [])
