"""
Integration tests for Writer module endpoints
Tests CRUD operations and AI actions return unified format
"""
from rest_framework import status
from igny8_core.api.tests.test_integration_base import IntegrationTestBase
from igny8_core.modules.writer.models import Tasks, Content, Images


class WriterIntegrationTestCase(IntegrationTestBase):
    """Integration tests for Writer module"""
    
    def test_list_tasks_returns_unified_format(self):
        """Test GET /api/v1/writer/tasks/ returns unified format"""
        response = self.client.get('/api/v1/writer/tasks/')
        
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assert_paginated_response(response)
    
    def test_create_task_returns_unified_format(self):
        """Test POST /api/v1/writer/tasks/ returns unified format"""
        data = {
            'title': 'Test Task',
            'site_id': self.site.id,
            'sector_id': self.sector.id,
            'status': 'pending'
        }
        response = self.client.post('/api/v1/writer/tasks/', data, format='json')
        
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assert_unified_response_format(response, expected_success=True)
        self.assertIn('data', response.data)
    
    def test_list_content_returns_unified_format(self):
        """Test GET /api/v1/writer/content/ returns unified format"""
        response = self.client.get('/api/v1/writer/content/')
        
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assert_paginated_response(response)
    
    def test_list_images_returns_unified_format(self):
        """Test GET /api/v1/writer/images/ returns unified format"""
        response = self.client.get('/api/v1/writer/images/')
        
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assert_paginated_response(response)
    
    def test_create_image_returns_unified_format(self):
        """Test POST /api/v1/writer/images/ returns unified format"""
        data = {
            'image_type': 'featured',
            'site_id': self.site.id,
            'sector_id': self.sector.id,
            'status': 'pending'
        }
        response = self.client.post('/api/v1/writer/images/', data, format='json')
        
        # May return 400 if site/sector validation fails, but should still be unified format
        self.assertIn(response.status_code, [status.HTTP_201_CREATED, status.HTTP_400_BAD_REQUEST])
        self.assert_unified_response_format(response)
    
    def test_task_validation_error_returns_unified_format(self):
        """Test validation errors return unified format"""
        response = self.client.post('/api/v1/writer/tasks/', {}, format='json')
        
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
        self.assert_unified_response_format(response, expected_success=False)
        self.assertIn('error', response.data)
        self.assertIn('errors', response.data)

