"""Unit tests for the Jellyseerr client."""

from __future__ import annotations

import unittest
from unittest.mock import MagicMock

from media_library_viewer_api.clients.jellyseerr import JellyseerrClient


class JellyseerrClientTests(unittest.TestCase):
    def setUp(self) -> None:
        self.client = JellyseerrClient("https://requests.example.com/api/v1", "api-key")
        self.session = MagicMock()
        self.client.session = self.session

    def test_jellyfin_users_accepts_wrapped_payload(self) -> None:
        response = MagicMock()
        response.raise_for_status.return_value = None
        response.json.return_value = {
            "users": [
                {"id": "jf1", "username": "alex", "email": "alex@example.com", "thumb": "/avatar"},
                {"id": "jf2", "username": "sam", "email": "sam@example.com", "thumb": "/avatar2"},
            ]
        }
        self.session.get.return_value = response

        users = self.client.jellyfin_users()

        self.assertEqual(len(users), 2)
        self.assertEqual(users[0]["email"], "alex@example.com")
        self.assertEqual(users[1]["username"], "sam")
        self.session.get.assert_called_once()

    def test_jellyfin_users_accepts_list_payload(self) -> None:
        response = MagicMock()
        response.raise_for_status.return_value = None
        response.json.return_value = [
            {"id": "jf1", "username": "alex"},
            {"id": "jf2", "username": "sam"},
        ]
        self.session.get.return_value = response

        users = self.client.jellyfin_users()

        self.assertEqual([u["username"] for u in users], ["alex", "sam"])

    def test_users_uses_take_and_skip(self) -> None:
        first = MagicMock()
        first.raise_for_status.return_value = None
        first.json.return_value = {
            "pageInfo": {"results": 3, "pages": 2, "pageSize": 2, "page": 1},
            "results": [
                {"id": 1, "username": "alex", "email": "alex@example.com"},
                {"id": 2, "username": "sam", "email": "sam@example.com"},
            ],
        }
        second = MagicMock()
        second.raise_for_status.return_value = None
        second.json.return_value = {
            "pageInfo": {"results": 3, "pages": 2, "pageSize": 2, "page": 2},
            "results": [
                {"id": 3, "username": "max", "email": "max@example.com"},
            ],
        }
        self.session.get.side_effect = [first, second]

        users = self.client.users(page_size=2)

        self.assertEqual([u["username"] for u in users], ["alex", "sam", "max"])
        self.assertEqual(self.session.get.call_args_list[0].kwargs["params"], {"take": 2, "skip": 0})
        self.assertEqual(self.session.get.call_args_list[1].kwargs["params"], {"take": 2, "skip": 2})

    def test_absolute_url_normalizes_relative_paths(self) -> None:
        self.assertEqual(self.client.absolute_url("/avatar.png"), "https://requests.example.com/avatar.png")
        self.assertEqual(self.client.absolute_url("avatar.png"), "https://requests.example.com/avatar.png")
        self.assertEqual(self.client.absolute_url("https://cdn.example.com/x.png"), "https://cdn.example.com/x.png")


if __name__ == "__main__":
    unittest.main()
