"""
TIIA SearchImage simplified API tests.
"""

from django.contrib.auth import get_user_model
from django.test import TestCase
from rest_framework import status
from rest_framework.test import APIClient
from unittest.mock import patch


User = get_user_model()


class TiiaSearchImageAPITestCase(TestCase):
    def setUp(self):
        self.client = APIClient()
        self.user = User.objects.create_user(
            username='test_tiia_search_user',
            password='testpass123',
            email='test_tiia_search@example.com',
        )

    def test_search_image_unauthenticated(self):
        resp = self.client.post(
            '/api/v1/tiia/search-image/',
            data={'imageUrl': 'https://example.com/a.png'},
            format='json',
        )
        self.assertEqual(resp.status_code, status.HTTP_401_UNAUTHORIZED)

    @patch('api_v1.views.tiia.search_image_url_in_tencent_tiia')
    def test_search_image_success(self, mock_search):
        self.client.force_authenticate(self.user)
        mock_search.return_value = {'Candidates': [{'EntityId': '1', 'Score': 99}]}

        resp = self.client.post(
            '/api/v1/tiia/search-image/',
            data={'imageUrl': 'https://example.com/a.png'},
            format='json',
        )
        self.assertEqual(resp.status_code, status.HTTP_200_OK)
        self.assertEqual(resp.json(), {'Candidates': [{'EntityId': '1', 'Score': 99}]})
        mock_search.assert_called()

    @patch('api_v1.views.tiia.search_image_url_in_tencent_tiia')
    def test_search_image_pass_limit_offset(self, mock_search):
        self.client.force_authenticate(self.user)
        mock_search.return_value = {'Candidates': []}

        resp = self.client.post(
            '/api/v1/tiia/search-image/',
            data={
                'imageUrl': 'https://example.com/a.png',
                'limit': 50,
                'offset': 10,
                'matchThreshold': 80,
            },
            format='json',
        )
        self.assertEqual(resp.status_code, status.HTTP_200_OK)
        _, kwargs = mock_search.call_args
        self.assertEqual(kwargs.get('limit'), 50)
        self.assertEqual(kwargs.get('offset'), 10)
        self.assertEqual(kwargs.get('match_threshold'), 80)

    def test_search_image_limit_too_large(self):
        self.client.force_authenticate(self.user)
        resp = self.client.post(
            '/api/v1/tiia/search-image/',
            data={'imageUrl': 'https://example.com/a.png', 'limit': 101},
            format='json',
        )
        self.assertEqual(resp.status_code, status.HTTP_400_BAD_REQUEST)
        self.assertIn('error', resp.json())

    def test_search_image_offset_negative(self):
        self.client.force_authenticate(self.user)
        resp = self.client.post(
            '/api/v1/tiia/search-image/',
            data={'imageUrl': 'https://example.com/a.png', 'offset': -1},
            format='json',
        )
        self.assertEqual(resp.status_code, status.HTTP_400_BAD_REQUEST)
        self.assertIn('error', resp.json())

    @patch('api_v1.views.tiia.search_image_url_in_tencent_tiia')
    def test_search_image_success_base64(self, mock_search):
        self.client.force_authenticate(self.user)
        mock_search.return_value = {'Candidates': []}

        resp = self.client.post(
            '/api/v1/tiia/search-image/',
            data={'imageBase64': 'iVBORw0KGgoAAAANSUhEUgAAAAUA'},  # dummy
            format='json',
        )
        self.assertEqual(resp.status_code, status.HTTP_200_OK)
        mock_search.assert_called()

    @patch('api_v1.views.tiia.search_image_url_in_tencent_tiia')
    def test_search_image_both_params_url_wins(self, mock_search):
        self.client.force_authenticate(self.user)
        mock_search.return_value = {'Candidates': []}

        resp = self.client.post(
            '/api/v1/tiia/search-image/',
            data={
                'imageUrl': 'https://example.com/a.png',
                'imageBase64': 'iVBORw0KGgoAAAANSUhEUgAAAAUA',
            },
            format='json',
        )
        self.assertEqual(resp.status_code, status.HTTP_200_OK)
        _, kwargs = mock_search.call_args
        self.assertEqual(kwargs.get('image_url'), 'https://example.com/a.png')

    def test_search_image_missing_image_url(self):
        self.client.force_authenticate(self.user)
        resp = self.client.post('/api/v1/tiia/search-image/', data={}, format='json')
        self.assertEqual(resp.status_code, status.HTTP_400_BAD_REQUEST)
        self.assertIn('error', resp.json())

