"""Tests for alumni signals."""
from unittest.mock import MagicMock, patch

from django.test import TestCase

from allianceauth.eveonline.models import EveCharacter

from alumni.models import AlumniSetup


class AlumniSetupSignalTest(TestCase):
    """Test signals triggered by AlumniSetup changes."""

    @patch('alumni.signals.run_alumni_check_all')
    def test_alumni_setup_update_triggers_check_all(self, mock_task: MagicMock) -> None:
        """Test that updating AlumniSetup triggers run_alumni_check_all."""
        setup = AlumniSetup.get_solo()
        # Trigger a save which should fire the signal
        setup.save()

        # The signal handler should have called apply_async
        mock_task.apply_async.assert_called()


class CharacterAddedSignalTest(TestCase):
    """Test signals triggered by EveCharacter creation."""

    @patch('alumni.signals.alumni_check_character')
    def test_new_character_triggers_alumni_check(self, mock_task: MagicMock) -> None:
        """Test that creating a new EveCharacter triggers alumni_check_character."""
        # Create a new character
        character = EveCharacter.objects.create(
            character_id=12345678,
            character_name="Test Character",
            corporation_id=98765432,
            corporation_name="Test Corp",
        )

        # The signal handler should have called apply_async with the character_id
        mock_task.apply_async.assert_called()
        call_args = mock_task.apply_async.call_args
        # Check that the first argument is the character_id
        self.assertIn(character.character_id, call_args[1]['args'])

    @patch('alumni.signals.alumni_check_character')
    def test_character_update_does_not_trigger_alumni_check(self, mock_task: MagicMock) -> None:
        """Test that updating an existing EveCharacter does not trigger alumni_check_character."""
        # Create a character
        character = EveCharacter.objects.create(
            character_id=12345678,
            character_name="Test Character",
            corporation_id=98765432,
            corporation_name="Test Corp",
        )
        # Reset the mock to clear the create call
        mock_task.apply_async.reset_mock()

        # Update the character
        character.character_name = "Updated Character"
        character.save()

        # The signal handler should NOT have been called for this update
        mock_task.apply_async.assert_not_called()


class SignalPriorityTest(TestCase):
    """Test that signals use correct task priorities."""

    @patch('alumni.signals.alumni_check_character')
    def test_alumni_check_character_priority(self, mock_task: MagicMock) -> None:
        """Test that alumni_check_character is called with correct priority."""
        from alumni.app_settings import ALUMNI_TASK_PRIORITY

        # Create a new character
        EveCharacter.objects.create(
            character_id=12345678,
            character_name="Test Character",
            corporation_id=98765432,
            corporation_name="Test Corp",
        )

        # Check that the task was called with ALUMNI_TASK_PRIORITY - 1
        mock_task.apply_async.assert_called()
        call_kwargs = mock_task.apply_async.call_args[1]
        self.assertEqual(call_kwargs['priority'], ALUMNI_TASK_PRIORITY - 1)

    @patch('alumni.signals.run_alumni_check_all')
    def test_run_alumni_check_all_priority(self, mock_task: MagicMock) -> None:
        """Test that run_alumni_check_all is called with correct priority."""
        from alumni.app_settings import ALUMNI_TASK_PRIORITY

        setup = AlumniSetup.get_solo()
        setup.save()

        # Check that the task was called with ALUMNI_TASK_PRIORITY
        mock_task.apply_async.assert_called()
        call_kwargs = mock_task.apply_async.call_args[1]
        self.assertEqual(call_kwargs['priority'], ALUMNI_TASK_PRIORITY)
