"""Tests for alumni providers."""
from unittest.mock import Mock, patch

from django.test import SimpleTestCase


class AllianceHistoryProviderTest(SimpleTestCase):
    """Test corporation alliance history provider."""

    @patch('alumni.providers.esi')
    def test_get_corporations_corporation_id_alliancehistory(self, mock_esi: Mock) -> None:
        """Test getting corporation alliance history from ESI."""
        from alumni.providers import (
            get_corporations_corporation_id_alliancehistory,
        )

        # Mock the ESI response
        mock_response = Mock()
        mock_response.results.return_value = [
            Mock(
                record_id=1,
                alliance_id=123456789,
                is_deleted='false',
                start_date='2020-01-01T00:00:00Z',
            )
        ]
        mock_esi.client.Corporation.GetCorporationsCorporationIdAlliancehistory.return_value = mock_response

        result = get_corporations_corporation_id_alliancehistory(98765432)

        self.assertEqual(len(result), 1)
        self.assertEqual(result[0].record_id, 1)
        self.assertEqual(result[0].alliance_id, 123456789)

    @patch('alumni.providers.esi')
    def test_get_corporations_corporation_id_alliancehistory_multiple_records(self, mock_esi: Mock) -> None:
        """Test getting multiple corporation alliance history records."""
        from alumni.providers import (
            get_corporations_corporation_id_alliancehistory,
        )

        # Mock the ESI response with multiple records
        mock_response = Mock()
        mock_response.results.return_value = [
            Mock(record_id=1, alliance_id=123456789, is_deleted='false', start_date='2020-01-01T00:00:00Z'),
            Mock(record_id=2, alliance_id=987654321, is_deleted='false', start_date='2021-01-01T00:00:00Z'),
        ]
        mock_esi.client.Corporation.GetCorporationsCorporationIdAlliancehistory.return_value = mock_response

        result = get_corporations_corporation_id_alliancehistory(98765432)

        self.assertEqual(len(result), 2)

    @patch('alumni.providers.esi')
    def test_get_corporations_corporation_id_alliancehistory_empty(self, mock_esi: Mock) -> None:
        """Test getting empty corporation alliance history."""
        from alumni.providers import (
            get_corporations_corporation_id_alliancehistory,
        )

        mock_response = Mock()
        mock_response.results.return_value = []
        mock_esi.client.Corporation.GetCorporationsCorporationIdAlliancehistory.return_value = mock_response

        result = get_corporations_corporation_id_alliancehistory(98765432)

        self.assertEqual(len(result), 0)


class CharacterCorporationHistoryProviderTest(SimpleTestCase):
    """Test character corporation history provider."""

    @patch('alumni.providers.esi')
    def test_get_characters_character_id_corporationhistory(self, mock_esi: Mock) -> None:
        """Test getting character corporation history from ESI."""
        from alumni.providers import (
            get_characters_character_id_corporationhistory,
        )

        mock_response = Mock()
        mock_response.results.return_value = [
            Mock(
                record_id=1,
                corporation_id=98765432,
                is_deleted='false',
                start_date='2020-01-01T00:00:00Z',
            )
        ]
        mock_esi.client.Character.GetCharactersCharacterIdCorporationhistory.return_value = mock_response

        result = get_characters_character_id_corporationhistory(12345678)

        self.assertEqual(len(result), 1)
        self.assertEqual(result[0].record_id, 1)
        self.assertEqual(result[0].corporation_id, 98765432)

    @patch('alumni.providers.esi')
    def test_get_characters_character_id_corporationhistory_multiple_records(self, mock_esi: Mock) -> None:
        """Test getting multiple character corporation history records."""
        from alumni.providers import (
            get_characters_character_id_corporationhistory,
        )

        mock_response = Mock()
        mock_response.results.return_value = [
            Mock(record_id=1, corporation_id=98765432, is_deleted='false', start_date='2020-01-01T00:00:00Z'),
            Mock(record_id=2, corporation_id=11111111, is_deleted='false', start_date='2021-01-01T00:00:00Z'),
        ]
        mock_esi.client.Character.GetCharactersCharacterIdCorporationhistory.return_value = mock_response

        result = get_characters_character_id_corporationhistory(12345678)

        self.assertEqual(len(result), 2)

    @patch('alumni.providers.esi')
    def test_get_characters_character_id_corporationhistory_empty(self, mock_esi: Mock) -> None:
        """Test getting empty character corporation history."""
        from alumni.providers import (
            get_characters_character_id_corporationhistory,
        )

        mock_response = Mock()
        mock_response.results.return_value = []
        mock_esi.client.Character.GetCharactersCharacterIdCorporationhistory.return_value = mock_response

        result = get_characters_character_id_corporationhistory(12345678)

        self.assertEqual(len(result), 0)


class ProviderReturnTypeTest(SimpleTestCase):
    """Test that providers return list types."""

    @patch('alumni.providers.esi')
    def test_corporations_returns_list(self, mock_esi: Mock) -> None:
        """Verify get_corporations_corporation_id_alliancehistory returns a list."""
        from alumni.providers import (
            get_corporations_corporation_id_alliancehistory,
        )

        mock_response = Mock()
        mock_response.results.return_value = []
        mock_esi.client.Corporation.GetCorporationsCorporationIdAlliancehistory.return_value = mock_response

        result = get_corporations_corporation_id_alliancehistory(98765432)

        self.assertIsInstance(result, list)

    @patch('alumni.providers.esi')
    def test_characters_returns_list(self, mock_esi: Mock) -> None:
        """Verify get_characters_character_id_corporationhistory returns a list."""
        from alumni.providers import (
            get_characters_character_id_corporationhistory,
        )

        mock_response = Mock()
        mock_response.results.return_value = []
        mock_esi.client.Character.GetCharactersCharacterIdCorporationhistory.return_value = mock_response

        result = get_characters_character_id_corporationhistory(12345678)

        self.assertIsInstance(result, list)
