"""
Tests for ContentSyncService
Phase 6: Site Integration & Multi-Destination Publishing
"""
from django.test import TestCase
from unittest.mock import patch, Mock

from igny8_core.auth.models import Account, Site, Sector, User, Plan, Industry, IndustrySector
from igny8_core.business.integration.models import SiteIntegration
from igny8_core.business.integration.services.content_sync_service import ContentSyncService
from igny8_core.business.content.models import Content


class ContentSyncServiceTestCase(TestCase):
    """Test cases for ContentSyncService"""
    
    def setUp(self):
        """Set up test data"""
        # Create plan first
        self.plan = Plan.objects.create(
            name="Test Plan",
            slug="test-plan",
            price=0,
            credits_per_month=1000
        )
        
        # Create user first (Account needs owner)
        self.user = User.objects.create_user(
            username='testuser',
            email='test@test.com',
            password='testpass123',
            role='owner'
        )
        
        # Create account with owner
        self.account = Account.objects.create(
            name="Test Account",
            slug="test-account",
            plan=self.plan,
            owner=self.user
        )
        
        # Update user to have account
        self.user.account = self.account
        self.user.save()
        
        # Create industry and sector
        self.industry = Industry.objects.create(
            name="Test Industry",
            slug="test-industry"
        )
        
        self.industry_sector = IndustrySector.objects.create(
            industry=self.industry,
            name="Test Sector",
            slug="test-sector"
        )
        
        self.site = Site.objects.create(
            account=self.account,
            name="Test Site",
            slug="test-site",
            industry=self.industry
        )
        self.sector = Sector.objects.create(
            account=self.account,
            site=self.site,
            industry_sector=self.industry_sector,
            name="Test Sector",
            slug="test-sector"
        )
        self.integration = SiteIntegration.objects.create(
            account=self.account,
            site=self.site,
            platform='wordpress',
            platform_type='cms',
            sync_enabled=True
        )
        self.service = ContentSyncService()
    
    def test_sync_content_from_wordpress_creates_content(self):
        """Test: WordPress sync works (when plugin connected)"""
        mock_posts = [
            {
                'id': 1,
                'title': 'Test Post',
                'content': '<p>Test content</p>',
                'status': 'publish',
            }
        ]
        
        with patch.object(self.service, '_fetch_wordpress_posts') as mock_fetch:
            mock_fetch.return_value = mock_posts
            
            result = self.service.sync_from_wordpress(self.integration)
            
            self.assertTrue(result.get('success'))
            self.assertEqual(result.get('synced_count'), 1)
            
            # Verify content was created
            content = Content.objects.filter(site=self.site).first()
            self.assertIsNotNone(content)
            self.assertEqual(content.title, 'Test Post')
            self.assertEqual(content.source, 'wordpress')
    
    def test_sync_content_from_shopify_creates_content(self):
        """Test: Content sync works"""
        mock_products = [
            {
                'id': 1,
                'title': 'Test Product',
                'body_html': '<p>Product description</p>',
            }
        ]
        
        with patch.object(self.service, '_fetch_shopify_products') as mock_fetch:
            mock_fetch.return_value = mock_products
            
            result = self.service.sync_from_shopify(self.integration)
            
            self.assertTrue(result.get('success'))
            self.assertEqual(result.get('synced_count'), 1)
    
    def test_sync_handles_duplicate_content(self):
        """Test: Content sync works"""
        # Create existing content
        Content.objects.create(
            account=self.account,
            site=self.site,
            sector=self.sector,
            title="Test Post",
            html_content="<p>Existing</p>",
            source='wordpress'
        )
        
        mock_posts = [
            {
                'id': 1,
                'title': 'Test Post',
                'content': '<p>Updated content</p>',
            }
        ]
        
        with patch.object(self.service, '_fetch_wordpress_posts') as mock_fetch:
            mock_fetch.return_value = mock_posts
            
            result = self.service.sync_from_wordpress(self.integration)
            
            # Should update existing, not create duplicate
            content_count = Content.objects.filter(
                site=self.site,
                title='Test Post'
            ).count()
            self.assertEqual(content_count, 1)

