"""
Tests for Universal Content Types (Phase 8)
Tests for product, service, and taxonomy content generation
"""
from unittest.mock import patch, MagicMock
from django.test import TestCase
from igny8_core.business.content.models import Content
from igny8_core.business.content.services.content_generation_service import ContentGenerationService
from igny8_core.api.tests.test_integration_base import IntegrationTestBase


class UniversalContentTypesTests(IntegrationTestBase):
    """Tests for Phase 8: Universal Content Types"""
    
    def setUp(self):
        super().setUp()
        # Add credits to account for testing
        self.account.credits = 10000
        self.account.save()
        self.service = ContentGenerationService()
    
    @patch('igny8_core.ai.tasks.run_ai_task')
    def test_product_content_generates_correctly(self, mock_run_ai_task):
        """
        Test: Product content generates correctly
        Task 17: Verify product generation creates content with correct entity_type and structure
        """
        # Mock AI task response
        mock_task = MagicMock()
        mock_task.id = 'test-task-123'
        mock_run_ai_task.delay.return_value = mock_task
        
        product_data = {
            'name': 'Test Product',
            'description': 'A test product description',
            'features': ['Feature 1', 'Feature 2', 'Feature 3'],
            'target_audience': 'Small businesses',
            'primary_keyword': 'test product',
            'word_count': 1500
        }
        
        # Generate product content
        result = self.service.generate_product_content(
            product_data=product_data,
            account=self.account,
            site=self.site,
            sector=self.sector
        )
        
        # Verify result
        self.assertTrue(result.get('success'))
        self.assertIsNotNone(result.get('task_id'))
        self.assertEqual(result.get('message'), 'Product content generation started')
        
        # Verify AI task was called with correct function name
        mock_run_ai_task.delay.assert_called_once()
        call_args = mock_run_ai_task.delay.call_args
        self.assertEqual(call_args[1]['function_name'], 'generate_product_content')
        self.assertEqual(call_args[1]['payload']['product_name'], 'Test Product')
    
    @patch('igny8_core.ai.tasks.run_ai_task')
    def test_service_pages_work_correctly(self, mock_run_ai_task):
        """
        Test: Service pages work correctly
        Task 18: Verify service page generation creates content with correct entity_type
        """
        # Mock AI task response
        mock_task = MagicMock()
        mock_task.id = 'test-task-456'
        mock_run_ai_task.delay.return_value = mock_task
        
        service_data = {
            'name': 'Test Service',
            'description': 'A test service description',
            'benefits': ['Benefit 1', 'Benefit 2', 'Benefit 3'],
            'target_audience': 'Enterprise clients',
            'primary_keyword': 'test service',
            'word_count': 1800
        }
        
        # Generate service page
        result = self.service.generate_service_page(
            service_data=service_data,
            account=self.account,
            site=self.site,
            sector=self.sector
        )
        
        # Verify result
        self.assertTrue(result.get('success'))
        self.assertIsNotNone(result.get('task_id'))
        self.assertEqual(result.get('message'), 'Service page generation started')
        
        # Verify AI task was called with correct function name
        mock_run_ai_task.delay.assert_called_once()
        call_args = mock_run_ai_task.delay.call_args
        self.assertEqual(call_args[1]['function_name'], 'generate_service_page')
        self.assertEqual(call_args[1]['payload']['service_name'], 'Test Service')
    
    @patch('igny8_core.ai.tasks.run_ai_task')
    def test_taxonomy_pages_work_correctly(self, mock_run_ai_task):
        """
        Test: Taxonomy pages work correctly
        Task 19: Verify taxonomy generation creates content with correct entity_type
        """
        # Mock AI task response
        mock_task = MagicMock()
        mock_task.id = 'test-task-789'
        mock_run_ai_task.delay.return_value = mock_task
        
        taxonomy_data = {
            'name': 'Test Taxonomy',
            'description': 'A test taxonomy description',
            'items': ['Category 1', 'Category 2', 'Category 3'],
            'primary_keyword': 'test taxonomy',
            'word_count': 1200
        }
        
        # Generate taxonomy
        result = self.service.generate_taxonomy(
            taxonomy_data=taxonomy_data,
            account=self.account,
            site=self.site,
            sector=self.sector
        )
        
        # Verify result
        self.assertTrue(result.get('success'))
        self.assertIsNotNone(result.get('task_id'))
        self.assertEqual(result.get('message'), 'Taxonomy generation started')
        
        # Verify AI task was called with correct function name
        mock_run_ai_task.delay.assert_called_once()
        call_args = mock_run_ai_task.delay.call_args
        self.assertEqual(call_args[1]['function_name'], 'generate_taxonomy')
        self.assertEqual(call_args[1]['payload']['taxonomy_name'], 'Test Taxonomy')
    
    def test_product_content_has_correct_structure(self):
        """
        Test: Product content generates correctly
        Task 17: Verify product content has correct entity_type, json_blocks, and structure_data
        """
        # Create product content manually to test structure
        product_content = Content.objects.create(
            account=self.account,
            site=self.site,
            sector=self.sector,
            title='Test Product',
            html_content='<p>Product content</p>',
            entity_type='product',
            json_blocks=[
                {
                    'type': 'product_overview',
                    'heading': 'Product Overview',
                    'content': 'Product description'
                },
                {
                    'type': 'features',
                    'heading': 'Key Features',
                    'items': ['Feature 1', 'Feature 2']
                },
                {
                    'type': 'specifications',
                    'heading': 'Specifications',
                    'data': {'Spec 1': 'Value 1'}
                }
            ],
            structure_data={
                'product_type': 'software',
                'price_range': '$99-$199',
                'target_market': 'SMB'
            },
            word_count=1500,
            status='draft'
        )
        
        # Verify structure
        self.assertEqual(product_content.entity_type, 'product')
        self.assertIsNotNone(product_content.json_blocks)
        self.assertEqual(len(product_content.json_blocks), 3)
        self.assertEqual(product_content.json_blocks[0]['type'], 'product_overview')
        self.assertIsNotNone(product_content.structure_data)
        self.assertEqual(product_content.structure_data['product_type'], 'software')
    
    def test_service_content_has_correct_structure(self):
        """
        Test: Service pages work correctly
        Task 18: Verify service content has correct entity_type and json_blocks
        """
        # Create service content manually to test structure
        service_content = Content.objects.create(
            account=self.account,
            site=self.site,
            sector=self.sector,
            title='Test Service',
            html_content='<p>Service content</p>',
            entity_type='service',
            json_blocks=[
                {
                    'type': 'service_overview',
                    'heading': 'Service Overview',
                    'content': 'Service description'
                },
                {
                    'type': 'benefits',
                    'heading': 'Benefits',
                    'items': ['Benefit 1', 'Benefit 2']
                },
                {
                    'type': 'process',
                    'heading': 'Our Process',
                    'steps': ['Step 1', 'Step 2']
                }
            ],
            structure_data={
                'service_type': 'consulting',
                'duration': '3-6 months',
                'target_market': 'Enterprise'
            },
            word_count=1800,
            status='draft'
        )
        
        # Verify structure
        self.assertEqual(service_content.entity_type, 'service')
        self.assertIsNotNone(service_content.json_blocks)
        self.assertEqual(len(service_content.json_blocks), 3)
        self.assertEqual(service_content.json_blocks[0]['type'], 'service_overview')
        self.assertIsNotNone(service_content.structure_data)
        self.assertEqual(service_content.structure_data['service_type'], 'consulting')
    
    def test_taxonomy_content_has_correct_structure(self):
        """
        Test: Taxonomy pages work correctly
        Task 19: Verify taxonomy content has correct entity_type and json_blocks
        """
        # Create taxonomy content manually to test structure
        taxonomy_content = Content.objects.create(
            account=self.account,
            site=self.site,
            sector=self.sector,
            title='Test Taxonomy',
            html_content='<p>Taxonomy content</p>',
            entity_type='taxonomy',
            json_blocks=[
                {
                    'type': 'taxonomy_overview',
                    'heading': 'Taxonomy Overview',
                    'content': 'Taxonomy description'
                },
                {
                    'type': 'categories',
                    'heading': 'Categories',
                    'items': [
                        {
                            'name': 'Category 1',
                            'description': 'Category description',
                            'subcategories': ['Subcat 1', 'Subcat 2']
                        }
                    ]
                },
                {
                    'type': 'tags',
                    'heading': 'Tags',
                    'items': ['Tag 1', 'Tag 2', 'Tag 3']
                }
            ],
            structure_data={
                'taxonomy_type': 'product_categories',
                'item_count': 10,
                'hierarchy_levels': 3
            },
            word_count=1200,
            status='draft'
        )
        
        # Verify structure
        self.assertEqual(taxonomy_content.entity_type, 'taxonomy')
        self.assertIsNotNone(taxonomy_content.json_blocks)
        self.assertEqual(len(taxonomy_content.json_blocks), 3)
        self.assertEqual(taxonomy_content.json_blocks[0]['type'], 'taxonomy_overview')
        self.assertIsNotNone(taxonomy_content.structure_data)
        self.assertEqual(taxonomy_content.structure_data['taxonomy_type'], 'product_categories')
