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

from igny8_core.auth.models import Account, Site, Sector, User, Plan, Industry, IndustrySector
from igny8_core.business.publishing.services.adapters.base_adapter import BaseAdapter
from igny8_core.business.publishing.services.adapters.sites_renderer_adapter import SitesRendererAdapter
from igny8_core.business.publishing.services.adapters.wordpress_adapter import WordPressAdapter
from igny8_core.business.site_building.models import SiteBlueprint


class AdapterPatternTestCase(TestCase):
    """Test cases for adapter pattern"""
    
    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.blueprint = SiteBlueprint.objects.create(
            account=self.account,
            site=self.site,
            sector=self.sector,
            name="Test Blueprint",
            status='ready'
        )
    
    def test_sites_renderer_adapter_implements_base_interface(self):
        """Test: Adapter pattern works correctly"""
        adapter = SitesRendererAdapter()
        
        self.assertIsInstance(adapter, BaseAdapter)
        self.assertTrue(hasattr(adapter, 'publish'))
        self.assertTrue(hasattr(adapter, 'test_connection'))
        self.assertTrue(hasattr(adapter, 'get_status'))
    
    def test_wordpress_adapter_implements_base_interface(self):
        """Test: Adapter pattern works correctly"""
        adapter = WordPressAdapter()
        
        self.assertIsInstance(adapter, BaseAdapter)
        self.assertTrue(hasattr(adapter, 'publish'))
        self.assertTrue(hasattr(adapter, 'test_connection'))
        self.assertTrue(hasattr(adapter, 'get_status'))
    
    def test_sites_renderer_adapter_deploys_site(self):
        """Test: Multi-destination publishing works"""
        adapter = SitesRendererAdapter()
        
        result = adapter.deploy(self.blueprint)
        
        self.assertTrue(result.get('success'))
        self.assertIsNotNone(result.get('deployment_url'))
        self.assertIsNotNone(result.get('version'))
    
    def test_wordpress_adapter_publishes_content(self):
        """Test: Multi-destination publishing works"""
        from igny8_core.business.content.models import Content
        
        content = Content.objects.create(
            account=self.account,
            site=self.site,
            sector=self.sector,
            title="Test Content",
            html_content="<p>Test</p>"
        )
        
        adapter = WordPressAdapter()
        config = {
            'site_url': 'https://example.com',
            'username': 'test',
            'app_password': 'test'
        }
        
        # Patch WordPressClient at the point where it's used in the adapter
        with patch('igny8_core.business.publishing.services.adapters.wordpress_adapter.WordPressClient') as mock_client_class:
            mock_instance = Mock()
            mock_instance.create_post.return_value = {'id': 123, 'link': 'https://example.com/post/123'}
            mock_client_class.return_value = mock_instance
            
            result = adapter.publish(content, config)
            
            self.assertTrue(result.get('success'))
            self.assertIsNotNone(result.get('external_id'))
            self.assertIsNotNone(result.get('url'))

