"""
Regex Analysis Module
Provides regex analysis and conflict detection functionality.
"""

import json
import re
from typing import List, Dict, Any


def debug_pattern_conflicts(patterns: List[str], test_data: str) -> str:
    """
    Analyze pattern conflicts and overlaps.
    
    Args:
        patterns: List of regex patterns to analyze
        test_data: Test data to analyze patterns against
        
    Returns:
        JSON string with conflict analysis
    """
    conflicts = []
    overlaps = []
    
    # Analyze each pattern
    for i, pattern1 in enumerate(patterns):
        try:
            compiled1 = re.compile(pattern1)
            matches1 = compiled1.findall(test_data)
            
            for j, pattern2 in enumerate(patterns[i+1:], i+1):
                try:
                    compiled2 = re.compile(pattern2)
                    matches2 = compiled2.findall(test_data)
                    
                    # Check for overlapping matches
                    common_matches = set(matches1) & set(matches2)
                    if common_matches:
                        overlaps.append({
                            "pattern1": pattern1,
                            "pattern2": pattern2,
                            "pattern1_index": i,
                            "pattern2_index": j,
                            "common_matches": list(common_matches),
                            "conflict_type": "overlapping_matches"
                        })
                        
                except re.error as e:
                    conflicts.append({
                        "pattern": pattern2,
                        "pattern_index": j,
                        "error": str(e),
                        "conflict_type": "invalid_regex"
                    })
                    
        except re.error as e:
            conflicts.append({
                "pattern": pattern1,
                "pattern_index": i,
                "error": str(e),
                "conflict_type": "invalid_regex"
            })
    
    result = {
        "pattern_conflict_analysis": {
            "test_data": test_data,
            "total_patterns": len(patterns),
            "conflicts": conflicts,
            "overlaps": overlaps,
            "analysis_summary": {
                "invalid_patterns": len([c for c in conflicts if c["conflict_type"] == "invalid_regex"]),
                "overlapping_patterns": len(overlaps),
                "total_issues": len(conflicts) + len(overlaps)
            },
            "recommendations": [
                "Use priority levels to resolve conflicts",
                "Combine overlapping patterns where possible",
                "Test patterns with comprehensive test data",
                "Use Storm Logger's pattern management system"
            ],
            "storm_logger_solution": {
                "pattern_matcher": "Use PatternMatcher classes with priority levels",
                "conflict_resolution": "Priority-based pattern matching",
                "testing": "Comprehensive test suites for pattern validation",
                "maintenance": "Version-controlled pattern libraries"
            }
        }
    }
    
    return json.dumps(result, indent=2)