"""
Test Generation Engine for Storm Logger
Intelligent test generation for uncovered modules with business focus
"""

import json
import time
import ast
import inspect
import re
from dataclasses import dataclass, asdict
from pathlib import Path
from typing import Dict, List, Optional, Any, Tuple, Union


@dataclass
class TestModule:
    """Generated test module information"""
    module_name: str
    test_file_path: str
    test_classes: List[str]
    test_methods: List[str]
    fixtures_needed: List[str]
    complexity_level: str
    business_impact: str
    estimated_coverage: float


@dataclass
class TestGenerationResult:
    """Result of test generation process"""
    success: bool
    modules_generated: List[TestModule]
    total_tests: int
    estimated_coverage_improvement: float
    business_value: str
    implementation_guide: Dict[str, Any]
    error_message: Optional[str] = None


class StormTestGenerator:
    """
    Intelligent test generation engine for Storm Logger
    Focuses on business-critical modules with enterprise-grade testing
    """
    
    def __init__(self, project_root: str = None):
        self.project_root = Path(project_root) if project_root else Path.cwd()
        self.storm_logger_path = self.project_root / 'storm_logger'
        self.tests_path = self.project_root / 'tests'
        
        # Business-critical modules mapping
        self.business_modules = {
            'business_metrics': {
                'priority': 'CRITICAL',
                'complexity': 'HIGH',
                'business_value': 'Revenue tracking engine - $50M empire foundation',
                'test_focus': ['revenue_tracking', 'performance_metrics', 'quality_metrics', 'trend_analysis']
            },
            'sanitizer': {
                'priority': 'CRITICAL',
                'complexity': 'HIGH',
                'business_value': 'Enterprise compliance - Fortune 500 adoption enabler',
                'test_focus': ['pii_detection', 'gdpr_compliance', 'hipaa_compliance', 'threat_detection']
            },
            'sanitizer_v2': {
                'priority': 'CRITICAL',
                'complexity': 'HIGH',
                'business_value': 'Enhanced security - enterprise moat',
                'test_focus': ['advanced_patterns', 'performance_optimized', 'threat_modeling']
            },
            'patterns': {
                'priority': 'HIGH',
                'complexity': 'MEDIUM',
                'business_value': 'Pattern intelligence - regex hell prevention',
                'test_focus': ['pattern_matching', 'conflict_resolution', 'performance']
            },
            'message_formatter': {
                'priority': 'HIGH',
                'complexity': 'HIGH',
                'business_value': 'Core logging functionality - user experience',
                'test_focus': ['formatting_accuracy', 'performance', 'edge_cases']
            },
            'professional_logger': {
                'priority': 'HIGH',
                'complexity': 'MEDIUM',
                'business_value': 'Main user interface - developer experience',
                'test_focus': ['api_functionality', 'business_intelligence', 'integration']
            }
        }
        
        # Storm Logger test patterns
        self.test_patterns = {
            'fixtures': [
                'storm_logger', 'temp_log_dir', 'mock_datetime', 'capture_logs', 
                'performance_benchmark', 'basic_config'
            ],
            'markers': ['unit', 'integration', 'functional', 'performance'],
            'imports': [
                'import pytest',
                'from unittest.mock import MagicMock, patch, Mock',
                'from storm_logger.config import Framework, LogLevel, StormLoggerConfig'
            ]
        }
    
    def analyze_module_structure(self, module_path: Path) -> Dict[str, Any]:
        """Analyze module structure to understand what to test"""
        try:
            with open(module_path, 'r') as f:
                source = f.read()
            
            tree = ast.parse(source)
            
            structure = {
                'classes': [],
                'functions': [],
                'constants': [],
                'imports': [],
                'complexity_indicators': []
            }
            
            for node in ast.walk(tree):
                if isinstance(node, ast.ClassDef):
                    methods = [n.name for n in node.body if isinstance(n, ast.FunctionDef)]
                    structure['classes'].append({
                        'name': node.name,
                        'methods': methods,
                        'is_dataclass': any(
                            isinstance(d, ast.Name) and d.id == 'dataclass' 
                            for d in node.decorator_list
                        )
                    })
                elif isinstance(node, ast.FunctionDef) and not any(
                    isinstance(parent, ast.ClassDef) for parent in ast.walk(tree)
                    if hasattr(parent, 'body') and node in parent.body
                ):
                    structure['functions'].append({
                        'name': node.name,
                        'args': [arg.arg for arg in node.args.args],
                        'is_async': isinstance(node, ast.AsyncFunctionDef)
                    })
                elif isinstance(node, ast.Assign):
                    for target in node.targets:
                        if isinstance(target, ast.Name) and target.id.isupper():
                            structure['constants'].append(target.id)
            
            # Analyze complexity indicators
            complexity_score = 0
            for node in ast.walk(tree):
                if isinstance(node, (ast.If, ast.For, ast.While, ast.With)):
                    complexity_score += 1
                elif isinstance(node, ast.Try):
                    complexity_score += 2
                elif isinstance(node, ast.Lambda):
                    complexity_score += 1
            
            structure['complexity_indicators'] = {
                'score': complexity_score,
                'level': 'HIGH' if complexity_score > 20 else 'MEDIUM' if complexity_score > 10 else 'LOW'
            }
            
            return structure
            
        except Exception as e:
            return {'error': f'Failed to analyze module: {str(e)}'}
    
    def generate_business_metrics_tests(self) -> str:
        """Generate comprehensive tests for business_metrics.py"""
        return '''"""
Comprehensive test suite for business_metrics.py
Tests the revenue tracking engine and business intelligence core
"""

import pytest
import time
import json
import threading
from datetime import datetime, timedelta
from unittest.mock import MagicMock, patch, Mock
from dataclasses import asdict

from storm_logger.utils.business_metrics import (
    MetricType, BusinessMetric, BusinessMetricsCollector,
    PerformanceMetric, QualityMetric, TrendAnalysis
)
from storm_logger.config import StormLoggerConfig


class TestMetricType:
    """Test the MetricType enumeration"""
    
    def test_all_metric_types_exist(self):
        """Test that all business-critical metric types are defined"""
        expected_types = [
            'REVENUE', 'PERFORMANCE', 'QUALITY', 'SECURITY', 
            'COMPLIANCE', 'USER_ENGAGEMENT', 'SYSTEM_HEALTH'
        ]
        
        for metric_type in expected_types:
            assert hasattr(MetricType, metric_type), f"Missing critical metric type: {metric_type}"
    
    def test_revenue_metric_priority(self):
        """Test that revenue metrics have highest priority"""
        # This is critical for the $50M empire strategy
        assert MetricType.REVENUE.value == 'REVENUE'
        # Revenue metrics should be trackable for business intelligence


class TestBusinessMetric:
    """Test the BusinessMetric dataclass"""
    
    def test_revenue_metric_creation(self):
        """Test creation of revenue-tracking metrics"""
        metric = BusinessMetric(
            name='subscription_revenue',
            type=MetricType.REVENUE,
            value=10000.0,
            unit='USD',
            context='monthly_recurring_revenue',
            threshold=5000.0,
            priority='CRITICAL'
        )
        
        assert metric.name == 'subscription_revenue'
        assert metric.type == MetricType.REVENUE
        assert metric.value == 10000.0
        assert metric.exceeds_threshold()
        assert metric.priority == 'CRITICAL'
    
    def test_performance_metric_creation(self):
        """Test creation of performance metrics"""
        metric = BusinessMetric(
            name='log_processing_speed',
            type=MetricType.PERFORMANCE,
            value=6000000.0,  # 6M logs/sec target
            unit='logs/sec',
            context='enterprise_performance',
            threshold=1000000.0,
            priority='HIGH'
        )
        
        assert metric.type == MetricType.PERFORMANCE
        assert metric.value == 6000000.0
        assert metric.exceeds_threshold()
    
    def test_quality_metric_creation(self):
        """Test creation of quality metrics"""
        metric = BusinessMetric(
            name='test_coverage',
            type=MetricType.QUALITY,
            value=95.5,
            unit='percent',
            context='quality_assurance',
            threshold=90.0,
            priority='HIGH'
        )
        
        assert metric.type == MetricType.QUALITY
        assert metric.exceeds_threshold()
    
    def test_metric_threshold_evaluation(self):
        """Test metric threshold evaluation logic"""
        # Above threshold
        high_metric = BusinessMetric(
            name='test_metric',
            type=MetricType.PERFORMANCE,
            value=150.0,
            threshold=100.0
        )
        assert high_metric.exceeds_threshold()
        
        # Below threshold
        low_metric = BusinessMetric(
            name='test_metric',
            type=MetricType.PERFORMANCE,
            value=50.0,
            threshold=100.0
        )
        assert not low_metric.exceeds_threshold()
        
        # No threshold
        no_threshold_metric = BusinessMetric(
            name='test_metric',
            type=MetricType.PERFORMANCE,
            value=50.0
        )
        assert not no_threshold_metric.exceeds_threshold()
    
    def test_metric_serialization(self):
        """Test metric serialization for business intelligence"""
        metric = BusinessMetric(
            name='enterprise_adoption',
            type=MetricType.REVENUE,
            value=50000000.0,  # $50M target
            unit='USD',
            context='empire_strategy'
        )
        
        serialized = metric.to_dict()
        assert serialized['name'] == 'enterprise_adoption'
        assert serialized['value'] == 50000000.0
        assert serialized['type'] == 'REVENUE'


class TestBusinessMetricsCollector:
    """Test the BusinessMetricsCollector singleton"""
    
    def test_collector_singleton(self):
        """Test that collector is a singleton"""
        collector1 = BusinessMetricsCollector()
        collector2 = BusinessMetricsCollector()
        assert collector1 is collector2
    
    def test_revenue_tracking(self):
        """Test revenue tracking functionality"""
        collector = BusinessMetricsCollector()
        collector.clear_metrics()
        
        # Add revenue metrics
        collector.add_metric(
            name='subscription_revenue',
            metric_type=MetricType.REVENUE,
            value=10000.0,
            unit='USD',
            context='monthly_revenue'
        )
        
        collector.add_metric(
            name='enterprise_contracts',
            metric_type=MetricType.REVENUE,
            value=250000.0,
            unit='USD',
            context='enterprise_sales'
        )
        
        # Verify revenue tracking
        revenue_metrics = collector.get_metrics(metric_type=MetricType.REVENUE)
        assert len(revenue_metrics) == 2
        
        total_revenue = sum(m.value for m in revenue_metrics)
        assert total_revenue == 260000.0
    
    def test_performance_tracking(self):
        """Test performance tracking for enterprise requirements"""
        collector = BusinessMetricsCollector()
        collector.clear_metrics()
        
        # Track performance metrics
        collector.add_metric(
            name='log_throughput',
            metric_type=MetricType.PERFORMANCE,
            value=6000000.0,  # 6M logs/sec
            unit='logs/sec',
            context='enterprise_performance'
        )
        
        collector.add_metric(
            name='response_time',
            metric_type=MetricType.PERFORMANCE,
            value=0.1,  # 100ms
            unit='seconds',
            context='api_performance'
        )
        
        performance_metrics = collector.get_metrics(metric_type=MetricType.PERFORMANCE)
        assert len(performance_metrics) == 2
        
        # Verify performance benchmarks
        throughput_metric = next(m for m in performance_metrics if m.name == 'log_throughput')
        assert throughput_metric.value == 6000000.0
    
    def test_quality_assurance_tracking(self):
        """Test quality metrics tracking"""
        collector = BusinessMetricsCollector()
        collector.clear_metrics()
        
        # Track quality metrics
        collector.add_metric(
            name='test_coverage',
            metric_type=MetricType.QUALITY,
            value=95.0,
            unit='percent',
            context='quality_assurance'
        )
        
        collector.add_metric(
            name='code_quality_score',
            metric_type=MetricType.QUALITY,
            value=9.8,
            unit='score',
            context='code_quality'
        )
        
        quality_metrics = collector.get_metrics(metric_type=MetricType.QUALITY)
        assert len(quality_metrics) == 2
    
    def test_business_intelligence_reporting(self):
        """Test business intelligence report generation"""
        collector = BusinessMetricsCollector()
        collector.clear_metrics()
        
        # Add comprehensive metrics
        collector.add_metric('revenue', MetricType.REVENUE, 50000000.0, 'USD')
        collector.add_metric('performance', MetricType.PERFORMANCE, 6000000.0, 'logs/sec')
        collector.add_metric('quality', MetricType.QUALITY, 95.0, 'percent')
        
        # Generate business intelligence report
        report = collector.get_business_intelligence_report()
        
        assert 'revenue_metrics' in report
        assert 'performance_metrics' in report
        assert 'quality_metrics' in report
        assert 'recommendations' in report
        assert 'trends' in report
    
    def test_trend_analysis(self):
        """Test trend analysis for business intelligence"""
        collector = BusinessMetricsCollector()
        collector.clear_metrics()
        
        # Add time-series data
        base_time = time.time()
        for i in range(10):
            collector.add_metric(
                name='daily_revenue',
                metric_type=MetricType.REVENUE,
                value=1000.0 + (i * 100),  # Growing revenue
                unit='USD',
                context='daily_tracking',
                timestamp=base_time + (i * 86400)  # Daily intervals
            )
        
        trends = collector.calculate_trends()
        assert 'revenue_trend' in trends
        assert trends['revenue_trend']['direction'] == 'INCREASING'
    
    def test_performance_benchmarking(self, performance_benchmark):
        """Test performance benchmarking capabilities"""
        collector = BusinessMetricsCollector()
        collector.clear_metrics()
        
        # Benchmark metric collection performance
        with performance_benchmark(max_duration=0.001):  # 1ms max
            for i in range(100):
                collector.add_metric(
                    name=f'benchmark_metric_{i}',
                    metric_type=MetricType.PERFORMANCE,
                    value=i * 10.0,
                    unit='operations/sec'
                )
        
        # Verify all metrics were collected
        metrics = collector.get_metrics()
        assert len(metrics) == 100
    
    def test_concurrent_metric_collection(self):
        """Test thread-safe metric collection"""
        collector = BusinessMetricsCollector()
        collector.clear_metrics()
        
        def collect_metrics(thread_id):
            for i in range(50):
                collector.add_metric(
                    name=f'thread_{thread_id}_metric_{i}',
                    metric_type=MetricType.PERFORMANCE,
                    value=i * 10.0,
                    unit='operations/sec'
                )
        
        threads = []
        for i in range(4):
            thread = threading.Thread(target=collect_metrics, args=(i,))
            threads.append(thread)
            thread.start()
        
        for thread in threads:
            thread.join()
        
        # Verify all metrics were collected safely
        metrics = collector.get_metrics()
        assert len(metrics) == 200  # 4 threads * 50 metrics each
    
    def test_metric_export_formats(self):
        """Test metric export for business intelligence"""
        collector = BusinessMetricsCollector()
        collector.clear_metrics()
        
        # Add sample metrics
        collector.add_metric('revenue', MetricType.REVENUE, 10000.0, 'USD')
        collector.add_metric('performance', MetricType.PERFORMANCE, 5000.0, 'logs/sec')
        
        # Test JSON export
        json_export = collector.export_metrics(format='json')
        assert json_export is not None
        json_data = json.loads(json_export)
        assert 'metrics' in json_data
        assert len(json_data['metrics']) == 2
        
        # Test CSV export
        csv_export = collector.export_metrics(format='csv')
        assert csv_export is not None
        assert 'name,type,value,unit' in csv_export
    
    def test_cleanup_old_metrics(self):
        """Test automatic cleanup of old metrics"""
        collector = BusinessMetricsCollector()
        collector.clear_metrics()
        
        # Add old metrics
        old_time = time.time() - 86400  # 1 day ago
        collector.add_metric(
            name='old_metric',
            metric_type=MetricType.PERFORMANCE,
            value=100.0,
            timestamp=old_time
        )
        
        # Add recent metrics
        collector.add_metric(
            name='recent_metric',
            metric_type=MetricType.PERFORMANCE,
            value=200.0
        )
        
        # Trigger cleanup
        collector.cleanup_old_metrics(max_age=3600)  # 1 hour
        
        metrics = collector.get_metrics()
        assert len(metrics) == 1
        assert metrics[0].name == 'recent_metric'


class TestBusinessIntelligenceIntegration:
    """Test business intelligence integration"""
    
    def test_empire_strategy_metrics(self):
        """Test metrics aligned with $50M empire strategy"""
        collector = BusinessMetricsCollector()
        collector.clear_metrics()
        
        # Storm Test Framework metrics ($10M)
        collector.add_metric('storm_test_revenue', MetricType.REVENUE, 10000000.0, 'USD')
        
        # Django-Spellbook Sites metrics ($20M)
        collector.add_metric('django_sites_revenue', MetricType.REVENUE, 20000000.0, 'USD')
        
        # Advanced Dashboard Platform metrics ($15M)
        collector.add_metric('dashboard_revenue', MetricType.REVENUE, 15000000.0, 'USD')
        
        # MIDAS Toolchain metrics ($5M)
        collector.add_metric('midas_revenue', MetricType.REVENUE, 5000000.0, 'USD')
        
        # Verify total empire value
        revenue_metrics = collector.get_metrics(metric_type=MetricType.REVENUE)
        total_revenue = sum(m.value for m in revenue_metrics)
        assert total_revenue == 50000000.0  # $50M empire target
    
    def test_enterprise_performance_requirements(self):
        """Test enterprise performance requirements"""
        collector = BusinessMetricsCollector()
        collector.clear_metrics()
        
        # Enterprise performance benchmarks
        collector.add_metric('log_throughput', MetricType.PERFORMANCE, 6000000.0, 'logs/sec')
        collector.add_metric('response_time', MetricType.PERFORMANCE, 0.1, 'seconds')
        collector.add_metric('uptime', MetricType.QUALITY, 99.99, 'percent')
        collector.add_metric('error_rate', MetricType.QUALITY, 0.01, 'percent')
        
        # Generate enterprise report
        report = collector.get_business_intelligence_report()
        
        # Verify enterprise requirements are met
        assert report['performance_metrics']['log_throughput']['value'] >= 6000000.0
        assert report['quality_metrics']['uptime']['value'] >= 99.9
    
    def test_compliance_tracking(self):
        """Test compliance metrics for enterprise adoption"""
        collector = BusinessMetricsCollector()
        collector.clear_metrics()
        
        # Compliance metrics
        collector.add_metric('gdpr_compliance', MetricType.COMPLIANCE, 100.0, 'percent')
        collector.add_metric('hipaa_compliance', MetricType.COMPLIANCE, 100.0, 'percent')
        collector.add_metric('soc2_compliance', MetricType.COMPLIANCE, 100.0, 'percent')
        
        compliance_metrics = collector.get_metrics(metric_type=MetricType.COMPLIANCE)
        assert len(compliance_metrics) == 3
        
        # All compliance metrics should be 100%
        for metric in compliance_metrics:
            assert metric.value == 100.0


class TestRealWorldScenarios:
    """Test real-world business scenarios"""
    
    def test_monthly_business_review(self):
        """Test monthly business review scenario"""
        collector = BusinessMetricsCollector()
        collector.clear_metrics()
        
        # Simulate monthly metrics
        collector.add_metric('monthly_revenue', MetricType.REVENUE, 4166666.67, 'USD')  # $50M / 12
        collector.add_metric('new_customers', MetricType.REVENUE, 150, 'count')
        collector.add_metric('churn_rate', MetricType.QUALITY, 2.5, 'percent')
        collector.add_metric('system_uptime', MetricType.QUALITY, 99.95, 'percent')
        
        # Generate monthly report
        report = collector.get_business_intelligence_report()
        
        # Verify business KPIs
        assert 'revenue_metrics' in report
        assert 'quality_metrics' in report
        assert 'recommendations' in report
    
    def test_enterprise_customer_onboarding(self):
        """Test enterprise customer onboarding metrics"""
        collector = BusinessMetricsCollector()
        collector.clear_metrics()
        
        # Track onboarding metrics
        collector.add_metric('onboarding_time', MetricType.PERFORMANCE, 2.5, 'hours')
        collector.add_metric('setup_success_rate', MetricType.QUALITY, 98.5, 'percent')
        collector.add_metric('support_tickets', MetricType.QUALITY, 3, 'count')
        collector.add_metric('customer_satisfaction', MetricType.QUALITY, 4.8, 'score')
        
        # Verify onboarding quality
        quality_metrics = collector.get_metrics(metric_type=MetricType.QUALITY)
        satisfaction_metric = next(m for m in quality_metrics if m.name == 'customer_satisfaction')
        assert satisfaction_metric.value >= 4.5  # High satisfaction required
    
    def test_security_incident_tracking(self):
        """Test security incident tracking"""
        collector = BusinessMetricsCollector()
        collector.clear_metrics()
        
        # Track security metrics
        collector.add_metric('security_incidents', MetricType.SECURITY, 0, 'count')
        collector.add_metric('vulnerability_patches', MetricType.SECURITY, 15, 'count')
        collector.add_metric('security_scan_score', MetricType.SECURITY, 95.0, 'score')
        
        security_metrics = collector.get_metrics(metric_type=MetricType.SECURITY)
        assert len(security_metrics) == 3
        
        # Zero security incidents is the goal
        incident_metric = next(m for m in security_metrics if m.name == 'security_incidents')
        assert incident_metric.value == 0
'''

    def generate_sanitizer_tests(self) -> str:
        """Generate comprehensive tests for sanitizer.py"""
        return '''"""
Comprehensive test suite for sanitizer.py
Tests enterprise compliance and security sanitization
"""

import pytest
import re
import threading
import time
from unittest.mock import MagicMock, patch, Mock

from storm_logger.utils.sanitizer import (
    LogSanitizer, SensitivityLevel, SanitizationRule,
    ComplianceMode, ThreatLevel
)
from storm_logger.config import StormLoggerConfig


class TestLogSanitizer:
    """Test the LogSanitizer class"""
    
    def test_sanitizer_initialization(self):
        """Test sanitizer initialization with default settings"""
        sanitizer = LogSanitizer()
        assert sanitizer.sensitivity_level == SensitivityLevel.MEDIUM
        assert sanitizer.compliance_mode == ComplianceMode.GDPR
        assert sanitizer.enabled is True
    
    def test_sanitizer_with_custom_config(self):
        """Test sanitizer with custom configuration"""
        config = StormLoggerConfig(
            sanitization_level='high',
            compliance_mode='hipaa',
            enable_threat_detection=True
        )
        
        sanitizer = LogSanitizer(config)
        assert sanitizer.sensitivity_level == SensitivityLevel.HIGH
        assert sanitizer.compliance_mode == ComplianceMode.HIPAA
        assert sanitizer.threat_detection_enabled is True
    
    def test_pii_detection_and_removal(self):
        """Test PII detection and removal - critical for enterprise compliance"""
        sanitizer = LogSanitizer()
        
        # Test email detection
        message = "User email: john.doe@company.com contacted support"
        sanitized = sanitizer.sanitize(message)
        assert "john.doe@company.com" not in sanitized
        assert "[EMAIL]" in sanitized
        
        # Test phone number detection
        message = "Contact number: 555-123-4567 for urgent matters"
        sanitized = sanitizer.sanitize(message)
        assert "555-123-4567" not in sanitized
        assert "[PHONE]" in sanitized
        
        # Test SSN detection
        message = "SSN: 123-45-6789 for tax purposes"
        sanitized = sanitizer.sanitize(message)
        assert "123-45-6789" not in sanitized
        assert "[SSN]" in sanitized
    
    def test_financial_data_sanitization(self):
        """Test financial data sanitization - critical for PCI compliance"""
        sanitizer = LogSanitizer()
        
        # Test credit card detection
        message = "Payment with card 4532-1234-5678-9012 processed"
        sanitized = sanitizer.sanitize(message)
        assert "4532-1234-5678-9012" not in sanitized
        assert "[CREDIT_CARD]" in sanitized
        
        # Test bank account detection
        message = "Transfer to account 12345678901234567890"
        sanitized = sanitizer.sanitize(message)
        assert "12345678901234567890" not in sanitized
        assert "[ACCOUNT]" in sanitized
    
    def test_security_credentials_sanitization(self):
        """Test security credentials sanitization"""
        sanitizer = LogSanitizer()
        
        # Test API key detection
        message = "API key: sk_live_abcdef123456789012345678"
        sanitized = sanitizer.sanitize(message)
        assert "sk_live_abcdef123456789012345678" not in sanitized
        assert "[API_KEY]" in sanitized
        
        # Test password detection
        message = "Password: SuperSecret123! for authentication"
        sanitized = sanitizer.sanitize(message)
        assert "SuperSecret123!" not in sanitized
        assert "[PASSWORD]" in sanitized
    
    def test_gdpr_compliance_mode(self):
        """Test GDPR compliance mode"""
        sanitizer = LogSanitizer(compliance_mode=ComplianceMode.GDPR)
        
        # GDPR requires strict personal data protection
        message = "Customer John Smith, DOB: 1990-01-01, lives in Berlin"
        sanitized = sanitizer.sanitize(message)
        
        # Personal identifiers should be removed
        assert "John Smith" not in sanitized
        assert "1990-01-01" not in sanitized
        assert "[PERSON_NAME]" in sanitized
        assert "[DATE]" in sanitized
    
    def test_hipaa_compliance_mode(self):
        """Test HIPAA compliance mode"""
        sanitizer = LogSanitizer(compliance_mode=ComplianceMode.HIPAA)
        
        # HIPAA requires medical data protection
        message = "Patient ID: P123456, diagnosis: diabetes, DOB: 1985-05-15"
        sanitized = sanitizer.sanitize(message)
        
        # Medical identifiers should be removed
        assert "P123456" not in sanitized
        assert "diabetes" not in sanitized
        assert "1985-05-15" not in sanitized
        assert "[PATIENT_ID]" in sanitized
        assert "[MEDICAL_INFO]" in sanitized
    
    def test_soc2_compliance_mode(self):
        """Test SOC2 compliance mode"""
        sanitizer = LogSanitizer(compliance_mode=ComplianceMode.SOC2)
        
        # SOC2 requires security controls
        message = "User admin accessed database with password: secret123"
        sanitized = sanitizer.sanitize(message)
        
        # Security-sensitive information should be removed
        assert "secret123" not in sanitized
        assert "[PASSWORD]" in sanitized
    
    def test_threat_detection(self):
        """Test threat detection capabilities"""
        sanitizer = LogSanitizer(enable_threat_detection=True)
        
        # Test SQL injection detection
        message = "Query: SELECT * FROM users WHERE id = '1' OR '1'='1'"
        sanitized = sanitizer.sanitize(message)
        threat_level = sanitizer.get_threat_level(message)
        
        assert threat_level == ThreatLevel.HIGH
        assert "[SQL_INJECTION]" in sanitized
        
        # Test XSS detection
        message = "Input: <script>alert('xss')</script>"
        sanitized = sanitizer.sanitize(message)
        threat_level = sanitizer.get_threat_level(message)
        
        assert threat_level == ThreatLevel.MEDIUM
        assert "[XSS_ATTEMPT]" in sanitized
    
    def test_custom_sanitization_rules(self):
        """Test custom sanitization rules"""
        sanitizer = LogSanitizer()
        
        # Add custom rule for company-specific data
        custom_rule = SanitizationRule(
            name="internal_employee_id",
            pattern=r"EMP\\d{6}",
            replacement="[EMPLOYEE_ID]",
            sensitivity=SensitivityLevel.HIGH
        )
        sanitizer.add_custom_rule(custom_rule)
        
        message = "Employee EMP123456 accessed the system"
        sanitized = sanitizer.sanitize(message)
        
        assert "EMP123456" not in sanitized
        assert "[EMPLOYEE_ID]" in sanitized
    
    def test_whitelist_functionality(self):
        """Test whitelist functionality for safe data"""
        sanitizer = LogSanitizer()
        
        # Add safe email to whitelist
        sanitizer.add_to_whitelist("support@company.com")
        
        message = "Contact support@company.com or john.doe@company.com"
        sanitized = sanitizer.sanitize(message)
        
        # Whitelisted email should remain
        assert "support@company.com" in sanitized
        # Non-whitelisted email should be sanitized
        assert "john.doe@company.com" not in sanitized
        assert "[EMAIL]" in sanitized
    
    def test_performance_with_large_messages(self, performance_benchmark):
        """Test sanitization performance with large messages"""
        sanitizer = LogSanitizer()
        
        # Create large message with multiple PII elements
        large_message = "\\n".join([
            f"User {i}: email{i}@company.com, phone: 555-123-{i:04d}, SSN: {i:03d}-45-{i:04d}"
            for i in range(1000)
        ])
        
        with performance_benchmark(max_duration=1.0):  # 1 second max
            sanitized = sanitizer.sanitize(large_message)
        
        # Verify sanitization worked
        assert "@company.com" not in sanitized
        assert "555-123-" not in sanitized
        assert "[EMAIL]" in sanitized
        assert "[PHONE]" in sanitized
    
    def test_concurrent_sanitization(self):
        """Test thread-safe sanitization"""
        sanitizer = LogSanitizer()
        results = []
        
        def sanitize_messages(thread_id):
            for i in range(100):
                message = f"Thread {thread_id}: email{i}@test.com and phone 555-{thread_id:03d}-{i:04d}"
                sanitized = sanitizer.sanitize(message)
                results.append((thread_id, sanitized))
        
        threads = []
        for i in range(4):
            thread = threading.Thread(target=sanitize_messages, args=(i,))
            threads.append(thread)
            thread.start()
        
        for thread in threads:
            thread.join()
        
        # Verify all messages were sanitized
        assert len(results) == 400
        for thread_id, sanitized in results:
            assert "@test.com" not in sanitized
            assert "555-" not in sanitized
            assert "[EMAIL]" in sanitized
            assert "[PHONE]" in sanitized
    
    def test_audit_trail_generation(self):
        """Test audit trail generation for compliance"""
        sanitizer = LogSanitizer(enable_audit_trail=True)
        
        message = "User john.doe@company.com accessed confidential data"
        sanitized = sanitizer.sanitize(message)
        
        # Get audit trail
        audit_trail = sanitizer.get_audit_trail()
        
        assert len(audit_trail) > 0
        assert audit_trail[-1]['pattern_matched'] == 'email'
        assert audit_trail[-1]['sensitivity_level'] == SensitivityLevel.MEDIUM
        assert audit_trail[-1]['compliance_mode'] == ComplianceMode.GDPR
    
    def test_false_positive_prevention(self):
        """Test prevention of false positives"""
        sanitizer = LogSanitizer()
        
        # Test email-like but safe strings
        message = "Configuration: timeout=30s, max_connections=100"
        sanitized = sanitizer.sanitize(message)
        
        # Should not be overly aggressive
        assert "timeout=30s" in sanitized
        assert "max_connections=100" in sanitized
        
        # Test date-like but safe strings
        message = "Version: 2.0.1, Build: 20240101"
        sanitized = sanitizer.sanitize(message)
        
        # Should not sanitize version numbers
        assert "2.0.1" in sanitized
        assert "20240101" in sanitized
    
    def test_enterprise_security_requirements(self):
        """Test enterprise security requirements"""
        sanitizer = LogSanitizer(
            sensitivity_level=SensitivityLevel.STRICT,
            compliance_mode=ComplianceMode.ENTERPRISE
        )
        
        # Test comprehensive enterprise data
        message = """
        Enterprise Security Log:
        User: john.doe@company.com
        Employee ID: EMP123456
        IP Address: 192.168.1.100
        Session: abc123def456789
        API Key: sk_live_abcdef123456
        Database: postgresql://user:pass@db.company.com/prod
        """
        
        sanitized = sanitizer.sanitize(message)
        
        # All sensitive data should be removed
        assert "john.doe@company.com" not in sanitized
        assert "EMP123456" not in sanitized
        assert "192.168.1.100" not in sanitized
        assert "abc123def456789" not in sanitized
        assert "sk_live_abcdef123456" not in sanitized
        assert "user:pass" not in sanitized
        
        # Should have appropriate replacements
        assert "[EMAIL]" in sanitized
        assert "[EMPLOYEE_ID]" in sanitized
        assert "[IP_ADDRESS]" in sanitized
        assert "[SESSION_ID]" in sanitized
        assert "[API_KEY]" in sanitized
        assert "[DATABASE_URL]" in sanitized


class TestComplianceIntegration:
    """Test compliance integration scenarios"""
    
    def test_gdpr_right_to_erasure(self):
        """Test GDPR right to erasure compliance"""
        sanitizer = LogSanitizer(compliance_mode=ComplianceMode.GDPR)
        
        # Simulate data subject request
        message = "Processing request for john.smith@email.com data deletion"
        sanitized = sanitizer.sanitize(message)
        
        # Personal data should be removed
        assert "john.smith@email.com" not in sanitized
        assert "[EMAIL]" in sanitized
        
        # Verify compliance metadata
        compliance_info = sanitizer.get_compliance_info()
        assert compliance_info['gdpr_compliant'] is True
        assert 'data_processed' in compliance_info
    
    def test_hipaa_phi_protection(self):
        """Test HIPAA PHI protection"""
        sanitizer = LogSanitizer(compliance_mode=ComplianceMode.HIPAA)
        
        # Protected Health Information
        message = "Patient John Doe, DOB: 1980-01-01, Diagnosis: Type 2 Diabetes"
        sanitized = sanitizer.sanitize(message)
        
        # All PHI should be removed
        assert "John Doe" not in sanitized
        assert "1980-01-01" not in sanitized
        assert "Type 2 Diabetes" not in sanitized
        assert "[PHI]" in sanitized
    
    def test_multi_compliance_mode(self):
        """Test multiple compliance modes simultaneously"""
        sanitizer = LogSanitizer(
            compliance_mode=[ComplianceMode.GDPR, ComplianceMode.HIPAA, ComplianceMode.SOC2]
        )
        
        # Mixed data types
        message = "Patient: john@email.com, SSN: 123-45-6789, Card: 4532-1234-5678-9012"
        sanitized = sanitizer.sanitize(message)
        
        # All compliance requirements should be met
        assert "john@email.com" not in sanitized
        assert "123-45-6789" not in sanitized
        assert "4532-1234-5678-9012" not in sanitized
        
        # Verify all compliance modes are satisfied
        compliance_info = sanitizer.get_compliance_info()
        assert compliance_info['gdpr_compliant'] is True
        assert compliance_info['hipaa_compliant'] is True
        assert compliance_info['soc2_compliant'] is True


class TestRealWorldScenarios:
    """Test real-world enterprise scenarios"""
    
    def test_enterprise_application_logs(self):
        """Test sanitization of enterprise application logs"""
        sanitizer = LogSanitizer(
            sensitivity_level=SensitivityLevel.HIGH,
            compliance_mode=ComplianceMode.ENTERPRISE
        )
        
        # Typical enterprise log entry
        log_entry = """
        2024-01-01 10:00:00 - INFO - User Authentication
        Username: john.doe@company.com
        IP: 192.168.1.100
        Session: sess_abc123def456
        Device: Mozilla/5.0 (Windows NT 10.0; Win64; x64)
        Status: SUCCESS
        """
        
        sanitized = sanitizer.sanitize(log_entry)
        
        # Timestamp and non-sensitive info should remain
        assert "2024-01-01 10:00:00" in sanitized
        assert "INFO" in sanitized
        assert "User Authentication" in sanitized
        assert "STATUS: SUCCESS" in sanitized
        
        # Sensitive data should be sanitized
        assert "john.doe@company.com" not in sanitized
        assert "192.168.1.100" not in sanitized
        assert "sess_abc123def456" not in sanitized
    
    def test_payment_processing_logs(self):
        """Test sanitization of payment processing logs"""
        sanitizer = LogSanitizer(
            sensitivity_level=SensitivityLevel.STRICT,
            compliance_mode=ComplianceMode.PCI_DSS
        )
        
        # Payment processing log
        payment_log = """
        Payment Processing:
        Card: 4532-1234-5678-9012
        CVV: 123
        Amount: $150.00
        Merchant: Online Store
        Status: APPROVED
        """
        
        sanitized = sanitizer.sanitize(payment_log)
        
        # All card data should be removed
        assert "4532-1234-5678-9012" not in sanitized
        assert "123" not in sanitized
        assert "[CREDIT_CARD]" in sanitized
        assert "[CVV]" in sanitized
        
        # Non-sensitive data should remain
        assert "$150.00" in sanitized
        assert "Online Store" in sanitized
        assert "APPROVED" in sanitized
    
    def test_medical_records_sanitization(self):
        """Test sanitization of medical records"""
        sanitizer = LogSanitizer(
            sensitivity_level=SensitivityLevel.STRICT,
            compliance_mode=ComplianceMode.HIPAA
        )
        
        # Medical record log
        medical_log = """
        Medical Record Access:
        Patient: Jane Smith
        DOB: 1985-03-15
        MRN: MR789012
        Diagnosis: Hypertension
        Prescriptions: Lisinopril 10mg
        Provider: Dr. Johnson
        """
        
        sanitized = sanitizer.sanitize(medical_log)
        
        # All PHI should be removed
        assert "Jane Smith" not in sanitized
        assert "1985-03-15" not in sanitized
        assert "MR789012" not in sanitized
        assert "Hypertension" not in sanitized
        assert "Lisinopril" not in sanitized
        assert "Dr. Johnson" not in sanitized
        
        # Should have appropriate PHI markers
        assert "[PHI]" in sanitized
    
    def test_api_security_logs(self):
        """Test sanitization of API security logs"""
        sanitizer = LogSanitizer(
            sensitivity_level=SensitivityLevel.HIGH,
            enable_threat_detection=True
        )
        
        # API security log with potential threats
        api_log = """
        API Security Event:
        Endpoint: /api/v1/users
        Method: GET
        Query: SELECT * FROM users WHERE id = 1' OR '1'='1
        Headers: {'Authorization': 'Bearer sk_live_123456'}
        Response: 401 Unauthorized
        Threat Level: HIGH
        """
        
        sanitized = sanitizer.sanitize(api_log)
        
        # Threat indicators should be sanitized
        assert "SELECT * FROM users WHERE id = 1' OR '1'='1" not in sanitized
        assert "Bearer sk_live_123456" not in sanitized
        assert "[SQL_INJECTION]" in sanitized
        assert "[API_KEY]" in sanitized
        
        # Non-sensitive operational data should remain
        assert "/api/v1/users" in sanitized
        assert "GET" in sanitized
        assert "401 Unauthorized" in sanitized
'''

    def generate_patterns_tests(self) -> str:
        """Generate comprehensive tests for patterns/ directory"""
        return '''"""
Comprehensive test suite for patterns/ directory
Tests the pattern matching system that prevents regex hell
"""

import pytest
import re
import time
from unittest.mock import MagicMock, patch, Mock

from storm_logger.utils.patterns import (
    PatternMatcher, PatternConfig, PatternPriority,
    PatternRegistry, PatternConflictResolver
)
from storm_logger.utils.patterns.payment import PaymentCardMatcher
from storm_logger.utils.patterns.sensitive_data import SensitiveDataMatcher
from storm_logger.utils.patterns.base import BasePatternMatcher


class TestBasePatternMatcher:
    """Test the base pattern matcher functionality"""
    
    def test_base_pattern_matcher_initialization(self):
        """Test base pattern matcher initialization"""
        matcher = BasePatternMatcher()
        assert matcher.priority == PatternPriority.MEDIUM
        assert matcher.enabled is True
        assert matcher.compiled_patterns is not None
    
    def test_pattern_compilation(self):
        """Test pattern compilation and caching"""
        matcher = BasePatternMatcher()
        
        # Define test patterns
        patterns = {
            'test_pattern': (r'\\btest\\b', '[TEST]'),
            'number_pattern': (r'\\d+', '[NUMBER]')
        }
        
        # Compile patterns
        compiled = matcher.compile_patterns(patterns)
        
        assert len(compiled) == 2
        assert 'test_pattern' in compiled
        assert 'number_pattern' in compiled
        assert isinstance(compiled['test_pattern'], re.Pattern)
    
    def test_pattern_matching_basic(self):
        """Test basic pattern matching"""
        matcher = BasePatternMatcher()
        
        # Define simple patterns
        patterns = {
            'hello': (r'hello', '[GREETING]'),
            'world': (r'world', '[LOCATION]')
        }
        
        matcher.set_patterns(patterns)
        
        # Test matching
        text = "hello world"
        result = matcher.replace_all(text)
        
        assert result == "[GREETING] [LOCATION]"
    
    def test_pattern_priority_system(self):
        """Test pattern priority system"""
        high_priority_matcher = BasePatternMatcher(priority=PatternPriority.HIGH)
        low_priority_matcher = BasePatternMatcher(priority=PatternPriority.LOW)
        
        assert high_priority_matcher.priority.value > low_priority_matcher.priority.value
    
    def test_pattern_conflict_detection(self):
        """Test pattern conflict detection"""
        matcher = BasePatternMatcher()
        
        # Define conflicting patterns
        patterns = {
            'general_number': (r'\\d+', '[NUMBER]'),
            'specific_phone': (r'\\d{3}-\\d{3}-\\d{4}', '[PHONE]')
        }
        
        matcher.set_patterns(patterns)
        
        # Test with conflicting text
        text = "Call 555-123-4567 for info"
        conflicts = matcher.detect_conflicts(text)
        
        # Should detect that both patterns match
        assert len(conflicts) > 0
    
    def test_pattern_performance_caching(self, performance_benchmark):
        """Test pattern performance with caching"""
        matcher = BasePatternMatcher()
        
        patterns = {
            'email': (r'\\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Z|a-z]{2,}\\b', '[EMAIL]')
        }
        matcher.set_patterns(patterns)
        
        text = "Contact support@company.com for help"
        
        # First run (compilation)
        with performance_benchmark(max_duration=0.01):
            result1 = matcher.replace_all(text)
        
        # Second run (cached)
        with performance_benchmark(max_duration=0.001):
            result2 = matcher.replace_all(text)
        
        assert result1 == result2
        assert "support@company.com" not in result1
        assert "[EMAIL]" in result1


class TestPaymentCardMatcher:
    """Test the payment card pattern matcher"""
    
    def test_payment_card_matcher_initialization(self):
        """Test payment card matcher initialization"""
        matcher = PaymentCardMatcher()
        assert matcher.priority == PatternPriority.HIGH
        assert matcher.enabled is True
    
    def test_credit_card_detection(self):
        """Test credit card number detection"""
        matcher = PaymentCardMatcher()
        
        # Test various card formats
        test_cases = [
            ("Visa: 4532-1234-5678-9012", "4532-1234-5678-9012"),
            ("MasterCard: 5555 5555 5555 4444", "5555 5555 5555 4444"),
            ("Amex: 3782 8224 6310 005", "3782 8224 6310 005"),
            ("Card 4532123456789012", "4532123456789012")
        ]
        
        for text, card_number in test_cases:
            result = matcher.replace_all(text)
            assert card_number not in result
            assert "[CREDIT_CARD]" in result
    
    def test_cvv_detection(self):
        """Test CVV detection"""
        matcher = PaymentCardMatcher()
        
        test_cases = [
            "CVV: 123",
            "CVC: 456", 
            "Security code: 789",
            "CVV2: 012"
        ]
        
        for text in test_cases:
            result = matcher.replace_all(text)
            assert not any(char.isdigit() for char in result.split(': ')[1])
            assert "[CVV]" in result
    
    def test_card_type_identification(self):
        """Test card type identification"""
        matcher = PaymentCardMatcher()
        
        # Test different card types
        visa_card = "4532-1234-5678-9012"
        mastercard = "5555-5555-5555-4444"
        amex = "3782-8224-6310-005"
        
        visa_result = matcher.replace_all(f"Visa: {visa_card}")
        mastercard_result = matcher.replace_all(f"MC: {mastercard}")
        amex_result = matcher.replace_all(f"Amex: {amex}")
        
        # All should be detected as credit cards
        assert "[CREDIT_CARD]" in visa_result
        assert "[CREDIT_CARD]" in mastercard_result
        assert "[CREDIT_CARD]" in amex_result
    
    def test_luhn_validation(self):
        """Test Luhn algorithm validation"""
        matcher = PaymentCardMatcher()
        
        # Valid card numbers (pass Luhn check)
        valid_cards = [
            "4532-1234-5678-9012",  # Visa
            "5555-5555-5555-4444",  # MasterCard
        ]
        
        # Invalid card numbers (fail Luhn check)
        invalid_cards = [
            "4532-1234-5678-9013",  # Invalid Visa
            "5555-5555-5555-4445",  # Invalid MasterCard
        ]
        
        for card in valid_cards:
            result = matcher.replace_all(f"Valid card: {card}")
            assert card not in result
            assert "[CREDIT_CARD]" in result
        
        for card in invalid_cards:
            result = matcher.replace_all(f"Invalid card: {card}")
            # Should still sanitize for security
            assert card not in result
            assert "[CREDIT_CARD]" in result
    
    def test_partial_card_numbers(self):
        """Test partial card number handling"""
        matcher = PaymentCardMatcher()
        
        # Test last 4 digits
        text = "Card ending in 9012"
        result = matcher.replace_all(text)
        assert "9012" not in result
        assert "[CARD_LAST4]" in result
        
        # Test first 6 digits
        text = "Card starting with 453212"
        result = matcher.replace_all(text)
        assert "453212" not in result
        assert "[CARD_BIN]" in result
    
    def test_international_card_formats(self):
        """Test international card formats"""
        matcher = PaymentCardMatcher()
        
        # European format
        european_card = "4532 1234 5678 9012"
        result = matcher.replace_all(f"European format: {european_card}")
        assert european_card not in result
        assert "[CREDIT_CARD]" in result
        
        # Condensed format
        condensed_card = "4532123456789012"
        result = matcher.replace_all(f"Condensed: {condensed_card}")
        assert condensed_card not in result
        assert "[CREDIT_CARD]" in result
    
    def test_false_positive_prevention(self):
        """Test prevention of false positives"""
        matcher = PaymentCardMatcher()
        
        # Should not match non-card numbers
        safe_numbers = [
            "Phone: 555-123-4567",
            "Date: 2024-01-15",
            "Version: 1.2.3.4",
            "IP: 192.168.1.100"
        ]
        
        for text in safe_numbers:
            result = matcher.replace_all(text)
            assert result == text  # Should remain unchanged
    
    def test_performance_with_large_text(self, performance_benchmark):
        """Test performance with large text blocks"""
        matcher = PaymentCardMatcher()
        
        # Create large text with multiple card numbers
        large_text = "\\n".join([
            f"Transaction {i}: Card 4532-1234-5678-{i:04d}, CVV: {i:03d}"
            for i in range(1000)
        ])
        
        with performance_benchmark(max_duration=0.5):  # 500ms max
            result = matcher.replace_all(large_text)
        
        # Verify all cards were sanitized
        assert "4532-1234-5678-" not in result
        assert result.count("[CREDIT_CARD]") == 1000
        assert result.count("[CVV]") == 1000


class TestSensitiveDataMatcher:
    """Test the sensitive data pattern matcher"""
    
    def test_sensitive_data_matcher_initialization(self):
        """Test sensitive data matcher initialization"""
        matcher = SensitiveDataMatcher()
        assert matcher.priority == PatternPriority.HIGH
        assert matcher.enabled is True
    
    def test_email_detection(self):
        """Test email address detection"""
        matcher = SensitiveDataMatcher()
        
        email_tests = [
            "user@company.com",
            "admin+test@example.org",
            "user.name@sub.domain.com",
            "123@numbers.net"
        ]
        
        for email in email_tests:
            text = f"Contact: {email}"
            result = matcher.replace_all(text)
            assert email not in result
            assert "[EMAIL]" in result
    
    def test_phone_number_detection(self):
        """Test phone number detection"""
        matcher = SensitiveDataMatcher()
        
        phone_tests = [
            "555-123-4567",
            "(555) 123-4567",
            "555.123.4567",
            "+1-555-123-4567",
            "1-800-FLOWERS"
        ]
        
        for phone in phone_tests:
            text = f"Call: {phone}"
            result = matcher.replace_all(text)
            if phone.isdigit() or '-' in phone or '.' in phone or '(' in phone:
                assert phone not in result
                assert "[PHONE]" in result
    
    def test_ssn_detection(self):
        """Test Social Security Number detection"""
        matcher = SensitiveDataMatcher()
        
        ssn_tests = [
            "123-45-6789",
            "123456789",
            "123 45 6789"
        ]
        
        for ssn in ssn_tests:
            text = f"SSN: {ssn}"
            result = matcher.replace_all(text)
            assert ssn not in result
            assert "[SSN]" in result
    
    def test_ip_address_detection(self):
        """Test IP address detection"""
        matcher = SensitiveDataMatcher()
        
        ip_tests = [
            "192.168.1.1",
            "10.0.0.1",
            "172.16.0.1",
            "8.8.8.8"
        ]
        
        for ip in ip_tests:
            text = f"Server: {ip}"
            result = matcher.replace_all(text)
            assert ip not in result
            assert "[IP_ADDRESS]" in result
    
    def test_api_key_detection(self):
        """Test API key detection"""
        matcher = SensitiveDataMatcher()
        
        api_key_tests = [
            "sk_live_abcdef123456789",
            "pk_test_123456789abcdef",
            "api_key_xyz789012345",
            "Bearer eyJhbGciOiJIUzI1NiJ9"
        ]
        
        for api_key in api_key_tests:
            text = f"Auth: {api_key}"
            result = matcher.replace_all(text)
            assert api_key not in result
            assert "[API_KEY]" in result
    
    def test_password_detection(self):
        """Test password detection"""
        matcher = SensitiveDataMatcher()
        
        password_tests = [
            "password: SuperSecret123!",
            "pwd=MyPassword456",
            "pass: admin123",
            "secret: TopSecret789"
        ]
        
        for password_text in password_tests:
            result = matcher.replace_all(password_text)
            # Password value should be removed
            assert "SuperSecret123!" not in result
            assert "MyPassword456" not in result
            assert "admin123" not in result
            assert "TopSecret789" not in result
            assert "[PASSWORD]" in result
    
    def test_database_url_detection(self):
        """Test database URL detection"""
        matcher = SensitiveDataMatcher()
        
        db_url_tests = [
            "postgresql://user:pass@localhost:5432/db",
            "mysql://admin:secret@db.company.com/prod",
            "redis://user:password@redis.server.com:6379"
        ]
        
        for db_url in db_url_tests:
            text = f"Database: {db_url}"
            result = matcher.replace_all(text)
            assert "user:pass" not in result
            assert "admin:secret" not in result
            assert "user:password" not in result
            assert "[DATABASE_URL]" in result
    
    def test_session_token_detection(self):
        """Test session token detection"""
        matcher = SensitiveDataMatcher()
        
        token_tests = [
            "session_id: abc123def456789",
            "token: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9",
            "csrf_token: xyz789012345abc"
        ]
        
        for token_text in token_tests:
            result = matcher.replace_all(token_text)
            assert "abc123def456789" not in result
            assert "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9" not in result
            assert "xyz789012345abc" not in result
            assert "[SESSION_TOKEN]" in result
    
    def test_comprehensive_sensitive_data_scenario(self):
        """Test comprehensive sensitive data scenario"""
        matcher = SensitiveDataMatcher()
        
        # Complex enterprise log entry
        log_entry = """
        User Authentication Event:
        Email: john.doe@company.com
        Phone: 555-123-4567
        IP Address: 192.168.1.100
        Session ID: sess_abc123def456789
        API Key: sk_live_abcdef123456789
        Database: postgresql://user:password@db.company.com/prod
        SSN: 123-45-6789
        """
        
        result = matcher.replace_all(log_entry)
        
        # All sensitive data should be removed
        assert "john.doe@company.com" not in result
        assert "555-123-4567" not in result
        assert "192.168.1.100" not in result
        assert "sess_abc123def456789" not in result
        assert "sk_live_abcdef123456789" not in result
        assert "user:password" not in result
        assert "123-45-6789" not in result
        
        # Should have appropriate replacements
        assert "[EMAIL]" in result
        assert "[PHONE]" in result
        assert "[IP_ADDRESS]" in result
        assert "[SESSION_TOKEN]" in result
        assert "[API_KEY]" in result
        assert "[DATABASE_URL]" in result
        assert "[SSN]" in result


class TestPatternRegistry:
    """Test the pattern registry system"""
    
    def test_pattern_registry_initialization(self):
        """Test pattern registry initialization"""
        registry = PatternRegistry()
        assert registry.matchers == {}
        assert registry.conflict_resolver is not None
    
    def test_matcher_registration(self):
        """Test matcher registration"""
        registry = PatternRegistry()
        
        payment_matcher = PaymentCardMatcher()
        sensitive_matcher = SensitiveDataMatcher()
        
        registry.register_matcher('payment', payment_matcher)
        registry.register_matcher('sensitive', sensitive_matcher)
        
        assert 'payment' in registry.matchers
        assert 'sensitive' in registry.matchers
        assert registry.matchers['payment'] == payment_matcher
        assert registry.matchers['sensitive'] == sensitive_matcher
    
    def test_pattern_conflict_resolution(self):
        """Test pattern conflict resolution"""
        registry = PatternRegistry()
        
        # Create matchers with overlapping patterns
        payment_matcher = PaymentCardMatcher()
        sensitive_matcher = SensitiveDataMatcher()
        
        registry.register_matcher('payment', payment_matcher)
        registry.register_matcher('sensitive', sensitive_matcher)
        
        # Test text with potential conflicts
        text = "Pay with card 4532-1234-5678-9012 or call 555-123-4567"
        result = registry.replace_all(text, prevent_overlaps=True)
        
        # Should handle both patterns without conflicts
        assert "4532-1234-5678-9012" not in result
        assert "555-123-4567" not in result
        assert "[CREDIT_CARD]" in result
        assert "[PHONE]" in result
    
    def test_priority_based_matching(self):
        """Test priority-based pattern matching"""
        registry = PatternRegistry()
        
        # High priority matcher
        high_priority = PaymentCardMatcher()  # HIGH priority
        medium_priority = SensitiveDataMatcher()  # MEDIUM priority
        
        registry.register_matcher('high', high_priority)
        registry.register_matcher('medium', medium_priority)
        
        # Test with overlapping patterns
        text = "Account: 4532-1234-5678-9012"
        result = registry.replace_all(text, priority_order=True)
        
        # High priority should win
        assert "[CREDIT_CARD]" in result
    
    def test_bulk_text_processing(self, performance_benchmark):
        """Test bulk text processing performance"""
        registry = PatternRegistry()
        
        # Register all matchers
        registry.register_matcher('payment', PaymentCardMatcher())
        registry.register_matcher('sensitive', SensitiveDataMatcher())
        
        # Create bulk text
        texts = [
            f"User {i}: email{i}@test.com, card 4532-1234-5678-{i:04d}"
            for i in range(100)
        ]
        
        with performance_benchmark(max_duration=2.0):  # 2 seconds max
            results = registry.replace_all_bulk(texts)
        
        # Verify all texts were processed
        assert len(results) == 100
        for result in results:
            assert "@test.com" not in result
            assert "4532-1234-5678-" not in result
            assert "[EMAIL]" in result
            assert "[CREDIT_CARD]" in result
    
    def test_pattern_statistics(self):
        """Test pattern matching statistics"""
        registry = PatternRegistry()
        
        registry.register_matcher('payment', PaymentCardMatcher())
        registry.register_matcher('sensitive', SensitiveDataMatcher())
        
        # Process test text
        text = "Contact: user@test.com, Card: 4532-1234-5678-9012"
        result = registry.replace_all(text)
        
        # Get statistics
        stats = registry.get_statistics()
        
        assert 'total_matches' in stats
        assert 'matches_by_type' in stats
        assert 'processing_time' in stats
        assert stats['total_matches'] > 0
    
    def test_custom_pattern_integration(self):
        """Test integration of custom patterns"""
        registry = PatternRegistry()
        
        # Create custom matcher
        class CustomMatcher(BasePatternMatcher):
            def get_pattern_definitions(self):
                return {
                    'employee_id': (r'EMP\\d{6}', '[EMPLOYEE_ID]'),
                    'internal_code': (r'INT-\\d{4}', '[INTERNAL_CODE]')
                }
        
        custom_matcher = CustomMatcher()
        registry.register_matcher('custom', custom_matcher)
        
        # Test custom patterns
        text = "Employee EMP123456 with code INT-7890"
        result = registry.replace_all(text)
        
        assert "EMP123456" not in result
        assert "INT-7890" not in result
        assert "[EMPLOYEE_ID]" in result
        assert "[INTERNAL_CODE]" in result


class TestRealWorldPatternScenarios:
    """Test real-world pattern matching scenarios"""
    
    def test_enterprise_log_processing(self):
        """Test enterprise log processing scenario"""
        registry = PatternRegistry()
        
        # Register enterprise-relevant matchers
        registry.register_matcher('payment', PaymentCardMatcher())
        registry.register_matcher('sensitive', SensitiveDataMatcher())
        
        # Enterprise log entry
        log_entry = """
        2024-01-15 14:30:22 - Payment Processing
        Customer: john.doe@company.com
        Card: 4532-1234-5678-9012
        Amount: $250.00
        IP: 192.168.1.100
        Session: sess_abc123def456
        Status: APPROVED
        """
        
        result = registry.replace_all(log_entry)
        
        # Verify sanitization
        assert "john.doe@company.com" not in result
        assert "4532-1234-5678-9012" not in result
        assert "192.168.1.100" not in result
        assert "sess_abc123def456" not in result
        
        # Non-sensitive data should remain
        assert "$250.00" in result
        assert "APPROVED" in result
        assert "2024-01-15 14:30:22" in result
    
    def test_compliance_audit_scenario(self):
        """Test compliance audit scenario"""
        registry = PatternRegistry()
        
        # Register compliance-focused matchers
        registry.register_matcher('payment', PaymentCardMatcher())
        registry.register_matcher('sensitive', SensitiveDataMatcher())
        
        # Compliance audit log
        audit_log = """
        Compliance Audit Report:
        PII Elements Found:
        - Email: admin@company.com
        - Phone: 555-987-6543
        - SSN: 987-65-4321
        Financial Data:
        - Card: 5555-5555-5555-4444
        - Account: 1234567890123456
        Security Tokens:
        - API Key: sk_live_xyz789012345
        """
        
        result = registry.replace_all(audit_log)
        
        # All sensitive data should be sanitized
        assert "admin@company.com" not in result
        assert "555-987-6543" not in result
        assert "987-65-4321" not in result
        assert "5555-5555-5555-4444" not in result
        assert "1234567890123456" not in result
        assert "sk_live_xyz789012345" not in result
        
        # Audit structure should remain
        assert "Compliance Audit Report:" in result
        assert "PII Elements Found:" in result
        assert "Financial Data:" in result
        assert "Security Tokens:" in result
    
    def test_performance_monitoring_scenario(self):
        """Test performance monitoring scenario"""
        registry = PatternRegistry()
        
        # Register all matchers
        registry.register_matcher('payment', PaymentCardMatcher())
        registry.register_matcher('sensitive', SensitiveDataMatcher())
        
        # Process large volume of logs
        start_time = time.time()
        
        logs = [
            f"User {i}: email{i}@company.com processed payment 4532-1234-5678-{i:04d}"
            for i in range(500)
        ]
        
        results = []
        for log in logs:
            result = registry.replace_all(log)
            results.append(result)
        
        processing_time = time.time() - start_time
        
        # Performance requirements
        assert processing_time < 5.0  # Should process 500 logs in under 5 seconds
        assert len(results) == 500
        
        # Verify all sensitive data was sanitized
        for result in results:
            assert "@company.com" not in result
            assert "4532-1234-5678-" not in result
            assert "[EMAIL]" in result
            assert "[CREDIT_CARD]" in result
'''

    def generate_missing_tests_for_module(self, module_name: str, module_path: Path) -> str:
        """Generate tests for any missing module"""
        try:
            structure = self.analyze_module_structure(module_path)
            
            if 'error' in structure:
                return f"# Error analyzing {module_name}: {structure['error']}"
            
            # Get module info
            module_info = self.business_modules.get(module_name, {
                'priority': 'MEDIUM',
                'complexity': 'MEDIUM',
                'business_value': 'Supporting functionality',
                'test_focus': ['functionality', 'integration', 'edge_cases']
            })
            
            # Generate test template
            test_template = f'''"""
Comprehensive test suite for {module_name}
{module_info['business_value']}
"""

import pytest
import time
from unittest.mock import MagicMock, patch, Mock
from dataclasses import asdict

{chr(10).join(self.test_patterns['imports'])}


'''
            
            # Generate test classes for each class in module
            for class_info in structure['classes']:
                class_name = class_info['name']
                test_template += f'''
class Test{class_name}:
    """Test the {class_name} class"""
    
    def test_{class_name.lower()}_initialization(self):
        """Test {class_name} initialization"""
        # TODO: Implement initialization test
        pass
    
'''
                
                # Generate test methods for each method
                for method_name in class_info['methods']:
                    if not method_name.startswith('_'):  # Skip private methods
                        test_template += f'''    def test_{method_name}(self):
        """Test {method_name} method"""
        # TODO: Implement {method_name} test
        pass
    
'''
            
            # Generate test functions for module functions
            for func_info in structure['functions']:
                func_name = func_info['name']
                if not func_name.startswith('_'):  # Skip private functions
                    test_template += f'''
class Test{func_name.title()}Function:
    """Test the {func_name} function"""
    
    def test_{func_name}_basic(self):
        """Test {func_name} basic functionality"""
        # TODO: Implement {func_name} test
        pass
    
'''
            
            # Add business-focused tests
            test_template += f'''
class Test{module_name.title()}BusinessLogic:
    """Test business logic for {module_name}"""
    
    def test_business_requirements(self):
        """Test business requirements compliance"""
        # TODO: Implement business requirements test
        pass
    
    def test_performance_requirements(self, performance_benchmark):
        """Test performance requirements"""
        # TODO: Implement performance test
        pass
    
    def test_enterprise_integration(self):
        """Test enterprise integration scenarios"""
        # TODO: Implement enterprise integration test
        pass
    
    def test_compliance_requirements(self):
        """Test compliance requirements"""
        # TODO: Implement compliance test
        pass
'''
            
            return test_template
            
        except Exception as e:
            return f"# Error generating tests for {module_name}: {str(e)}"
    
    def generate_missing_tests(self, target_modules: List[str] = None) -> str:
        """
        Generate tests for missing/low-coverage modules
        
        Args:
            target_modules: Specific modules to generate tests for
            
        Returns:
            JSON string with test generation results
        """
        try:
            results = []
            
            # Determine which modules to target
            if target_modules is None:
                target_modules = [
                    'business_metrics', 'sanitizer', 'sanitizer_v2', 
                    'patterns', 'message_formatter', 'professional_logger'
                ]
            
            for module_name in target_modules:
                # Find module file
                module_path = None
                
                if module_name == 'patterns':
                    # Handle patterns directory
                    patterns_dir = self.storm_logger_path / 'utils' / 'patterns'
                    if patterns_dir.exists():
                        # Generate tests for all pattern files
                        pattern_files = list(patterns_dir.glob('*.py'))
                        for pattern_file in pattern_files:
                            if pattern_file.name != '__init__.py':
                                test_content = self.generate_patterns_tests()
                                results.append(TestModule(
                                    module_name=f"patterns.{pattern_file.stem}",
                                    test_file_path=f"tests/unit/test_patterns_{pattern_file.stem}.py",
                                    test_classes=[f"Test{pattern_file.stem.title()}"],
                                    test_methods=[f"test_{pattern_file.stem}_functionality"],
                                    fixtures_needed=['storm_logger', 'performance_benchmark'],
                                    complexity_level='MEDIUM',
                                    business_impact='HIGH',
                                    estimated_coverage=85.0
                                ))
                        continue
                
                # Find individual module files
                possible_paths = [
                    self.storm_logger_path / 'utils' / f'{module_name}.py',
                    self.storm_logger_path / f'{module_name}.py',
                    self.storm_logger_path / 'core' / f'{module_name}.py',
                    self.storm_logger_path / 'core' / 'formatters' / f'{module_name}.py'
                ]
                
                for path in possible_paths:
                    if path.exists():
                        module_path = path
                        break
                
                if not module_path:
                    continue
                
                # Generate appropriate test content
                if module_name == 'business_metrics':
                    test_content = self.generate_business_metrics_tests()
                elif module_name == 'sanitizer':
                    test_content = self.generate_sanitizer_tests()
                elif module_name == 'sanitizer_v2':
                    test_content = self.generate_sanitizer_tests().replace('sanitizer.py', 'sanitizer_v2.py')
                else:
                    test_content = self.generate_missing_tests_for_module(module_name, module_path)
                
                # Create test module info
                module_info = self.business_modules.get(module_name, {})
                
                test_module = TestModule(
                    module_name=module_name,
                    test_file_path=f"tests/unit/test_{module_name}.py",
                    test_classes=[f"Test{module_name.title()}"],
                    test_methods=[f"test_{module_name}_functionality"],
                    fixtures_needed=self.test_patterns['fixtures'],
                    complexity_level=module_info.get('complexity', 'MEDIUM'),
                    business_impact=module_info.get('priority', 'MEDIUM'),
                    estimated_coverage=90.0 if module_info.get('priority') == 'CRITICAL' else 85.0
                )
                
                results.append(test_module)
            
            # Calculate overall impact
            total_tests = sum(len(tm.test_methods) for tm in results)
            avg_coverage = sum(tm.estimated_coverage for tm in results) / len(results) if results else 0
            
            # Generate implementation guide
            implementation_guide = {
                'steps': [
                    'Review generated test files',
                    'Implement TODO sections with actual test logic',
                    'Add appropriate fixtures and test data',
                    'Run tests and verify coverage improvement',
                    'Refine tests based on actual module behavior'
                ],
                'priorities': {
                    'critical': [tm.module_name for tm in results if tm.business_impact == 'CRITICAL'],
                    'high': [tm.module_name for tm in results if tm.business_impact == 'HIGH'],
                    'medium': [tm.module_name for tm in results if tm.business_impact == 'MEDIUM']
                },
                'fixtures_needed': list(set(
                    fixture for tm in results for fixture in tm.fixtures_needed
                )),
                'estimated_effort': f"{len(results) * 2} days"
            }
            
            business_value = "Enterprise-grade test coverage for $50M revenue foundation"
            
            result = TestGenerationResult(
                success=True,
                modules_generated=results,
                total_tests=total_tests,
                estimated_coverage_improvement=avg_coverage,
                business_value=business_value,
                implementation_guide=implementation_guide
            )
            
            return json.dumps({
                'status': 'success',
                'result': asdict(result),
                'timestamp': time.time()
            }, indent=2)
            
        except Exception as e:
            error_result = TestGenerationResult(
                success=False,
                modules_generated=[],
                total_tests=0,
                estimated_coverage_improvement=0.0,
                business_value="",
                implementation_guide={},
                error_message=str(e)
            )
            
            return json.dumps({
                'status': 'error',
                'result': asdict(error_result),
                'timestamp': time.time()
            })


def generate_missing_tests(
    project_path: str,
    target_modules: List[str] = None,
    focus_area: str = "business_critical"
) -> str:
    """
    Generate tests for missing/low-coverage modules
    
    Args:
        project_path: Path to the Storm Logger project
        target_modules: Specific modules to generate tests for
        focus_area: Focus area for test generation
        
    Returns:
        JSON string with test generation results
    """
    generator = StormTestGenerator(project_path)
    return generator.generate_missing_tests(target_modules)


def generate_business_intelligence_tests(
    project_path: str,
    test_type: str = "comprehensive"
) -> str:
    """
    Generate business intelligence focused tests
    
    Args:
        project_path: Path to the Storm Logger project
        test_type: Type of tests to generate
        
    Returns:
        JSON string with business intelligence test code
    """
    generator = StormTestGenerator(project_path)
    return generator.generate_business_metrics_tests()


def generate_compliance_tests(
    project_path: str,
    compliance_type: str = "enterprise"
) -> str:
    """
    Generate compliance-focused tests
    
    Args:
        project_path: Path to the Storm Logger project
        compliance_type: Type of compliance tests
        
    Returns:
        JSON string with compliance test code
    """
    generator = StormTestGenerator(project_path)
    return generator.generate_sanitizer_tests()