"""
DEPRECATED: Tests for DeploymentService - SiteBlueprint models removed
Phase 5: Sites Renderer & Publishing
"""
from django.test import TestCase
from django.utils import timezone

from igny8_core.auth.models import Account, Site, Sector, User, Plan, Industry, IndustrySector
from igny8_core.business.publishing.models import DeploymentRecord
from igny8_core.business.publishing.services.deployment_service import DeploymentService


class DeploymentServiceTestCase(TestCase):
    """DEPRECATED: Test cases for DeploymentService"""
    
    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"
        )
        # DEPRECATED: SiteBlueprint model removed
        self.blueprint = None
        self.service = DeploymentService()
    
    def test_get_status_returns_deployed_record(self):
        """Test: Sites are accessible publicly"""
        DeploymentRecord.objects.create(
            account=self.account,
            site=self.site,
            sector=self.sector,
            site_blueprint=self.blueprint,
            version=1,
            status='deployed',
            deployment_url='https://test-site.igny8.com',
            deployed_at=timezone.now()
        )
        
        status = self.service.get_status(self.blueprint)
        
        self.assertIsNotNone(status)
        self.assertEqual(status.status, 'deployed')
        self.assertEqual(status.deployment_url, 'https://test-site.igny8.com')
    
    def test_get_latest_deployment_returns_most_recent(self):
        """Test: Deployment works end-to-end"""
        DeploymentRecord.objects.create(
            account=self.account,
            site=self.site,
            sector=self.sector,
            site_blueprint=self.blueprint,
            version=1,
            status='failed',
            created_at=timezone.now()
        )
        
        latest = DeploymentRecord.objects.create(
            account=self.account,
            site=self.site,
            sector=self.sector,
            site_blueprint=self.blueprint,
            version=2,
            status='deployed',
            deployment_url='https://test-site.igny8.com',
            deployed_at=timezone.now()
        )
        
        result = self.service.get_latest_deployment(self.blueprint)
        
        self.assertIsNotNone(result)
        self.assertEqual(result.version, 2)
        self.assertEqual(result.status, 'deployed')
    
    def test_rollback_reverts_to_previous_version(self):
        """Test: Deployment works end-to-end"""
        DeploymentRecord.objects.create(
            account=self.account,
            site=self.site,
            sector=self.sector,
            site_blueprint=self.blueprint,
            version=1,
            status='deployed',
            deployment_url='https://test-site.igny8.com',
            deployed_at=timezone.now()
        )
        
        result = self.service.rollback(self.blueprint, target_version=1)
        
        self.assertTrue(result.get('success'))
        self.blueprint.refresh_from_db()
        self.assertEqual(self.blueprint.deployed_version, 1)

