"""
Tests for IntegrationService
Phase 6: Site Integration & Multi-Destination Publishing
"""
from django.test import TestCase

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.integration_service import IntegrationService


class IntegrationServiceTestCase(TestCase):
    """Test cases for IntegrationService"""
    
    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.service = IntegrationService()
    
    def test_create_integration_stores_config(self):
        """Test: Site integrations work correctly"""
        integration = self.service.create_integration(
            site=self.site,
            platform='wordpress',
            config={'url': 'https://example.com'},
            credentials={'api_key': 'test-key'},
            platform_type='cms'
        )
        
        self.assertIsNotNone(integration)
        self.assertEqual(integration.platform, 'wordpress')
        self.assertEqual(integration.platform_type, 'cms')
        self.assertEqual(integration.config_json.get('url'), 'https://example.com')
        self.assertTrue(integration.is_active)
    
    def test_get_integrations_for_site_returns_all(self):
        """Test: Site integrations work correctly"""
        self.service.create_integration(
            site=self.site,
            platform='wordpress',
            config={},
            credentials={}
        )
        self.service.create_integration(
            site=self.site,
            platform='shopify',
            config={},
            credentials={}
        )
        
        integrations = self.service.get_integrations_for_site(self.site)
        
        self.assertEqual(integrations.count(), 2)
        platforms = [i.platform for i in integrations]
        self.assertIn('wordpress', platforms)
        self.assertIn('shopify', platforms)
    
    def test_test_connection_validates_credentials(self):
        """Test: Site integrations work correctly"""
        # Test with unsupported platform to verify NotImplementedError is raised
        integration = self.service.create_integration(
            site=self.site,
            platform='unsupported_platform',
            config={'url': 'https://example.com'},
            credentials={'api_key': 'test-key'}
        )
        
        with self.assertRaises(NotImplementedError):
            # Connection testing should raise NotImplementedError for unsupported platforms
            self.service.test_connection(integration)
    
    def test_update_integration_updates_fields(self):
        """Test: Site integrations work correctly"""
        integration = self.service.create_integration(
            site=self.site,
            platform='wordpress',
            config={'url': 'https://old.com'},
            credentials={}
        )
        
        updated = self.service.update_integration(
            integration,
            config={'url': 'https://new.com'},
            is_active=False
        )
        
        self.assertEqual(updated.config_json.get('url'), 'https://new.com')
        self.assertFalse(updated.is_active)

