"""
Base test class for integration tests
Provides common fixtures and utilities
"""
from django.test import TestCase
from rest_framework.test import APIClient
from rest_framework import status
from igny8_core.auth.models import User, Account, Plan, Site, Sector, Industry, IndustrySector, SeedKeyword


class IntegrationTestBase(TestCase):
    """Base class for integration tests with common fixtures"""
    
    def setUp(self):
        """Set up test fixtures"""
        self.client = APIClient()
        
        # Create test plan
        self.plan = Plan.objects.create(
            name="Test Plan",
            slug="test-plan",
            price=0,
            credits_per_month=1000
        )
        
        # Create test user first (Account needs owner)
        self.user = User.objects.create_user(
            username='testuser',
            email='test@test.com',
            password='testpass123',
            role='owner'
        )
        
        # Create test 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"
        )
        
        # Create site
        self.site = Site.objects.create(
            name="Test Site",
            slug="test-site",
            account=self.account,
            industry=self.industry
        )
        
        # Create sector (Sector needs industry_sector reference)
        self.sector = Sector.objects.create(
            name="Test Sector",
            slug="test-sector",
            site=self.site,
            account=self.account,
            industry_sector=self.industry_sector
        )
        
        # Create seed keyword
        self.seed_keyword = SeedKeyword.objects.create(
            keyword="test keyword",
            industry=self.industry,
            sector=self.industry_sector,
            volume=1000,
            difficulty=50,
            intent="informational"
        )
        
        # Authenticate client
        self.client.force_authenticate(user=self.user)
        
        # Set account on request (simulating middleware)
        self.client.force_authenticate(user=self.user)
    
    def assert_unified_response_format(self, response, expected_success=True):
        """Assert response follows unified format"""
        self.assertIn('success', response.data)
        self.assertEqual(response.data['success'], expected_success)
        
        if expected_success:
            # Success responses should have data or results
            self.assertTrue('data' in response.data or 'results' in response.data)
        else:
            # Error responses should have error
            self.assertIn('error', response.data)
    
    def assert_paginated_response(self, response):
        """Assert response is a paginated response"""
        self.assert_unified_response_format(response, expected_success=True)
        self.assertIn('success', response.data)
        self.assertIn('count', response.data)
        self.assertIn('results', response.data)
        self.assertIn('next', response.data)
        self.assertIn('previous', response.data)

