"""
Integration tests for pagination
Tests paginated responses across all modules return unified format
"""
from rest_framework import status
from igny8_core.api.tests.test_integration_base import IntegrationTestBase
from igny8_core.modules.planner.models import Keywords
from igny8_core.auth.models import SeedKeyword, Industry, IndustrySector


class PaginationIntegrationTestCase(IntegrationTestBase):
    """Integration tests for pagination"""
    
    def setUp(self):
        """Set up test fixtures with multiple records"""
        super().setUp()
        
        # Create multiple keywords for pagination testing
        for i in range(15):
            Keywords.objects.create(
                seed_keyword=self.seed_keyword,
                site=self.site,
                sector=self.sector,
                account=self.account,
                status='active'
            )
    
    def test_pagination_default_page_size(self):
        """Test pagination with default page size"""
        response = self.client.get('/api/v1/planner/keywords/')
        
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assert_paginated_response(response)
        self.assertEqual(response.data['count'], 15)
        self.assertLessEqual(len(response.data['results']), 10)  # Default page size
        self.assertIsNotNone(response.data['next'])  # Should have next page
    
    def test_pagination_custom_page_size(self):
        """Test pagination with custom page size"""
        response = self.client.get('/api/v1/planner/keywords/?page_size=5')
        
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assert_paginated_response(response)
        self.assertEqual(response.data['count'], 15)
        self.assertEqual(len(response.data['results']), 5)
        self.assertIsNotNone(response.data['next'])
    
    def test_pagination_page_parameter(self):
        """Test pagination with page parameter"""
        response = self.client.get('/api/v1/planner/keywords/?page=2&page_size=5')
        
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assert_paginated_response(response)
        self.assertEqual(response.data['count'], 15)
        self.assertEqual(len(response.data['results']), 5)
        self.assertIsNotNone(response.data['previous'])
    
    def test_pagination_max_page_size(self):
        """Test pagination respects max page size"""
        response = self.client.get('/api/v1/planner/keywords/?page_size=200')  # Exceeds max of 100
        
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assert_paginated_response(response)
        self.assertLessEqual(len(response.data['results']), 100)  # Should be capped at 100
    
    def test_pagination_empty_results(self):
        """Test pagination with empty results"""
        # Use a filter that returns no results
        response = self.client.get('/api/v1/planner/keywords/?status=nonexistent')
        
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assert_paginated_response(response)
        self.assertEqual(response.data['count'], 0)
        self.assertEqual(len(response.data['results']), 0)
        self.assertIsNone(response.data['next'])
        self.assertIsNone(response.data['previous'])
    
    def test_pagination_includes_success_field(self):
        """Test paginated responses include success field"""
        response = self.client.get('/api/v1/planner/keywords/')
        
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertIn('success', response.data)
        self.assertTrue(response.data['success'])
    
    def test_pagination_clusters(self):
        """Test pagination works for clusters endpoint"""
        response = self.client.get('/api/v1/planner/clusters/')
        
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assert_paginated_response(response)
    
    def test_pagination_ideas(self):
        """Test pagination works for ideas endpoint"""
        response = self.client.get('/api/v1/planner/ideas/')
        
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assert_paginated_response(response)
    
    def test_pagination_tasks(self):
        """Test pagination works for tasks endpoint"""
        response = self.client.get('/api/v1/writer/tasks/')
        
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assert_paginated_response(response)
    
    def test_pagination_content(self):
        """Test pagination works for content endpoint"""
        response = self.client.get('/api/v1/writer/content/')
        
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assert_paginated_response(response)

