"""
E2E Test Data Generator

Creates realistic test data scenarios for comprehensive competitive intelligence E2E testing.
"""

import json
from pathlib import Path
from datetime import datetime, timedelta
from typing import Dict, List, Any
import random


class E2ETestDataGenerator:
    """Generates comprehensive test datasets for E2E competitive intelligence testing"""
    
    def __init__(self, output_dir: Path):
        self.output_dir = output_dir
        self.output_dir.mkdir(parents=True, exist_ok=True)
    
    def generate_competitive_content_scenarios(self) -> Dict[str, Any]:
        """Generate various competitive content scenarios for testing"""
        
        scenarios = {
            "hvacr_school_premium": {
                "competitor": "HVACR School",
                "content_type": "professional_guides",
                "articles": [
                    {
                        "title": "Advanced Heat Pump Installation Certification Guide",
                        "content": """# Advanced Heat Pump Installation Certification Guide

## Professional Certification Overview
This comprehensive guide covers advanced heat pump installation techniques for HVAC professionals seeking certification.

## Prerequisites
- 5+ years HVAC experience
- EPA 608 certification
- Electrical troubleshooting knowledge
- Refrigeration fundamentals

## Advanced Installation Techniques

### Site Assessment and Planning
Professional heat pump installation begins with thorough site assessment:

1. **Structural Analysis**
   - Foundation requirements for outdoor units
   - Indoor unit mounting considerations
   - Vibration isolation planning
   - Load-bearing capacity verification

2. **Electrical Infrastructure**
   - Power supply calculations
   - Disconnect sizing and placement
   - Control wiring specifications
   - Emergency shutdown systems

3. **Refrigeration Line Design**
   - Line sizing calculations
   - Elevation considerations
   - Oil return analysis
   - Pressure drop calculations

### Installation Procedures

#### Outdoor Unit Placement
Critical factors for optimal outdoor unit performance:

- **Airflow Requirements**: Minimum 24" clearance on service side, 12" on other sides
- **Foundation**: Concrete pad with proper drainage, vibration dampening
- **Electrical Connections**: Weatherproof disconnect within sight of unit
- **Refrigeration Connections**: Proper brazing techniques, nitrogen purging

#### Indoor Unit Installation
Air handler or fan coil installation considerations:

- **Mounting Location**: Accessibility for service, adequate clearances
- **Ductwork Integration**: Proper sizing, sealing, insulation
- **Condensate Drainage**: Primary and secondary drain systems
- **Control Integration**: Thermostat wiring, staging controls

### System Commissioning

#### Refrigerant Charging
Precision charging procedures:

1. **Evacuation Process**
   - Triple evacuation minimum
   - 500 micron vacuum hold test
   - Electronic leak detection

2. **Charge Verification**
   - Superheat/subcooling method
   - Manufacturer charging charts
   - Performance verification testing

#### Performance Testing
Complete system performance validation:

- **Airflow Measurement**: Total external static pressure, CFM verification
- **Temperature Rise/Fall**: Supply air temperature differential
- **Electrical Analysis**: Amp draw, voltage verification, power factor
- **Efficiency Testing**: SEER/HSPF validation testing

## Troubleshooting Advanced Systems

### Electronic Controls
Modern heat pump control system diagnosis:

- **Communication Protocols**: BACnet, LonWorks, proprietary systems
- **Sensor Validation**: Temperature, pressure, humidity sensors
- **Actuator Testing**: Dampers, valves, variable speed controls

### Variable Refrigerant Flow
VRF system specific considerations:

- **Refrigerant Distribution**: Branch box sizing, line balancing
- **Control Logic**: Zone control, load balancing algorithms
- **Service Procedures**: Refrigerant recovery, system evacuation

## Code Compliance and Safety

### National Electrical Code
Critical NEC requirements for heat pump installations:

- **Article 440**: Air-conditioning and refrigerating equipment
- **Disconnecting means**: Location and accessibility requirements
- **Overcurrent protection**: Sizing for motor loads and controls
- **Grounding**: Equipment grounding conductor requirements

### Mechanical Codes
HVAC mechanical code compliance:

- **Equipment clearances**: Service access requirements
- **Combustion air**: Requirements for fossil fuel backup
- **Condensate disposal**: Drainage and overflow protection
- **Ductwork**: Sizing, sealing, and insulation requirements

## Advanced Diagnostic Techniques

### Digital Manifold Systems
Modern diagnostic tool utilization:

- **Real-time Data Logging**: Temperature, pressure trend analysis
- **Superheat/Subcooling Calculations**: Automatic refrigerant state analysis
- **System Performance Metrics**: Efficiency calculations, baseline comparison

### Thermal Imaging Applications
Infrared thermography for heat pump diagnosis:

- **Heat Exchanger Analysis**: Coil efficiency, airflow distribution
- **Electrical Connections**: Loose connection identification
- **Insulation Integrity**: Thermal bridging, missing insulation
- **Ductwork Assessment**: Air leakage, thermal losses

## Professional Development

### Continuing Education
Advanced certification maintenance:

- **Manufacturer Training**: Brand-specific installation techniques
- **Code Updates**: National and local code changes
- **Technology Advancement**: New refrigerants, control systems
- **Safety Training**: Electrical, refrigerant, and mechanical safety

This guide represents professional-level content targeting certified HVAC technicians and contractors seeking advanced installation expertise.""",
                        "engagement_metrics": {
                            "views": 15000,
                            "likes": 450,
                            "comments": 89,
                            "shares": 67,
                            "engagement_rate": 0.067,
                            "time_on_page": 480
                        },
                        "technical_metadata": {
                            "word_count": 2500,
                            "reading_level": "professional",
                            "technical_depth": 0.95,
                            "complexity_score": 0.88,
                            "code_references": 12,
                            "procedure_steps": 45
                        }
                    },
                    {
                        "title": "Commercial Refrigeration System Diagnostics",
                        "content": """# Commercial Refrigeration System Diagnostics

## Advanced Diagnostic Methodology
Systematic approach to commercial refrigeration troubleshooting using modern diagnostic tools and proven methodologies.

## Diagnostic Equipment

### Essential Tools
- Digital manifold gauge set with data logging
- Thermal imaging camera
- Ultrasonic leak detector
- Digital multimeter with temperature probes
- Refrigerant identifier
- Electronic expansion valve tester

### Advanced Diagnostics
- Vibration analysis equipment
- Oil analysis kits
- Compressor performance analyzers
- System efficiency meters

## System Analysis Procedures

### Initial Assessment
Comprehensive system evaluation protocol:

1. **Visual Inspection**
   - Component condition assessment
   - Refrigeration line inspection
   - Electrical connection verification
   - Safety system functionality

2. **Operating Parameter Analysis**
   - Suction and discharge pressures
   - Superheat and subcooling measurements
   - Amperage and voltage readings
   - Temperature differentials

### Compressor Diagnostics

#### Performance Testing
Compressor efficiency evaluation:

- **Pumping Capacity**: Volumetric efficiency calculations
- **Power Consumption**: Amp draw analysis vs. load conditions
- **Oil Analysis**: Acidity, moisture, contamination levels
- **Valve Testing**: Reed valve integrity, leakage assessment

#### Advanced Analysis
- **Vibration Signature Analysis**: Bearing condition, alignment
- **Thermodynamic Analysis**: P-H diagram plotting
- **Oil Return Evaluation**: System design adequacy

### Heat Exchanger Evaluation

#### Evaporator Analysis
Air-cooled and water-cooled evaporator diagnostics:

- **Heat Transfer Efficiency**: Temperature difference analysis
- **Airflow/Water Flow**: Volume and distribution assessment
- **Coil Condition**: Fin condition, tube integrity
- **Defrost System**: Cycle timing, termination controls

#### Condenser Performance
Condenser system optimization:

- **Heat Rejection Capacity**: Approach temperature analysis
- **Fan System Performance**: Airflow, electrical consumption
- **Water System Analysis**: Flow rates, water quality, scaling
- **Ambient Condition Compensation**: Head pressure control

### Control System Diagnostics

#### Electronic Controls
Modern control system troubleshooting:

- **Sensor Calibration**: Temperature, pressure, humidity sensors
- **Actuator Performance**: Expansion valves, dampers, pumps
- **Communication Systems**: Network diagnostics, protocol analysis
- **Algorithm Verification**: Control logic, setpoint management

### Refrigerant System Analysis

#### Leak Detection
Comprehensive leak identification procedures:

- **Electronic Detection**: Heated diode vs. infrared technology
- **Ultrasonic Methods**: Pressurized leak detection
- **Fluorescent Dye Systems**: UV light leak location
- **Soap Solution Testing**: Traditional bubble detection

#### Contamination Analysis
Refrigerant and oil quality assessment:

- **Moisture Content**: Karl Fischer analysis, sight glass indicators
- **Acid Level**: Oil acidity testing, system chemistry
- **Non-condensable Gases**: Pressure rise testing
- **Refrigerant Purity**: Refrigerant identification, contamination

## Troubleshooting Methodologies

### Systematic Approach
Structured diagnostic process:

1. **Symptom Documentation**: Detailed problem description
2. **System History**: Maintenance records, previous repairs
3. **Operating Condition Analysis**: Load conditions, ambient factors
4. **Component Testing**: Individual component verification
5. **System Integration**: Overall system performance assessment

### Common Problem Patterns

#### Low Capacity Issues
- **Refrigerant Undercharge**: Leak detection, charge verification
- **Heat Exchanger Problems**: Coil fouling, airflow restriction
- **Compressor Wear**: Valve leakage, efficiency degradation
- **Control Issues**: Thermostat calibration, staging problems

#### High Operating Costs
- **System Inefficiency**: Component degradation, poor maintenance
- **Control Optimization**: Scheduling, staging, load management
- **Heat Exchanger Maintenance**: Coil cleaning, fan optimization
- **Refrigerant System**: Proper charging, leak repair

### Advanced Diagnostic Techniques

#### Thermal Analysis
Infrared thermography applications:

- **Component Temperature Mapping**: Hot spots, thermal distribution
- **Heat Exchanger Analysis**: Coil performance, air distribution
- **Electrical System Inspection**: Connection integrity, load balance
- **Insulation Evaluation**: Thermal bridging, envelope integrity

#### Vibration Analysis
Mechanical system condition assessment:

- **Bearing Analysis**: Wear patterns, lubrication condition
- **Alignment Verification**: Coupling condition, shaft alignment
- **Balance Assessment**: Rotor condition, dynamic balance
- **Structural Analysis**: Mounting, vibration isolation

This diagnostic methodology enables systematic identification and resolution of complex commercial refrigeration system problems.""",
                        "engagement_metrics": {
                            "views": 18500,
                            "likes": 520,
                            "comments": 124,
                            "shares": 89,
                            "engagement_rate": 0.072,
                            "time_on_page": 520
                        },
                        "technical_metadata": {
                            "word_count": 3200,
                            "reading_level": "expert",
                            "technical_depth": 0.98,
                            "complexity_score": 0.92,
                            "diagnostic_procedures": 25,
                            "tool_references": 18
                        }
                    }
                ]
            },
            
            "ac_service_tech_practical": {
                "competitor": "AC Service Tech",
                "content_type": "practical_tutorials",
                "articles": [
                    {
                        "title": "Field-Tested Refrigerant Leak Detection Methods",
                        "content": """# Field-Tested Refrigerant Leak Detection Methods

## Real-World Leak Detection
Practical leak detection techniques that work in actual service conditions.

## Detection Method Comparison

### Electronic Leak Detectors
Field experience with different detector technologies:

#### Heated Diode Detectors
- **Pros**: Sensitive to all halogenated refrigerants, robust construction
- **Cons**: Sensor contamination in dirty environments, warm-up time
- **Best Applications**: Indoor units, clean environments, R-22 systems
- **Maintenance**: Regular sensor replacement, calibration checks

#### Infrared Detectors  
- **Pros**: No sensor contamination, immediate response, selective detection
- **Cons**: Higher cost, refrigerant-specific, ambient light sensitivity
- **Best Applications**: Outdoor units, mixed refrigerant environments
- **Maintenance**: Optical cleaning, battery management

### UV Dye Systems
Practical dye injection and detection:

#### Dye Selection
- **Universal Dyes**: Compatible with multiple refrigerant types
- **Oil-Based Dyes**: Better circulation, equipment compatibility
- **Concentration**: Proper dye-to-oil ratios for visibility

#### Detection Techniques
- **UV Light Selection**: LED vs. fluorescent, wavelength considerations
- **Inspection Timing**: System runtime requirements for dye circulation
- **Contamination Avoidance**: Previous dye residue, false positives

### Bubble Solutions
Traditional and modern bubble testing:

#### Commercial Solutions
- **Sensitivity**: Detection threshold comparison
- **Application**: Spray bottles, brush application, immersion testing
- **Environmental Factors**: Temperature effects, wind considerations

#### Homemade Solutions
- **Dish Soap Mix**: Concentration ratios, additives
- **Glycerin Addition**: Bubble persistence, low-temperature performance

## Systematic Leak Detection Process

### Initial Assessment
Pre-detection system evaluation:

1. **System History**: Previous leak locations, repair records
2. **Visual Inspection**: Oil stains, corrosion, physical damage
3. **Pressure Testing**: Standing pressure, pressure rise tests
4. **Component Prioritization**: Statistical failure points

### Detection Sequence
Efficient leak detection workflow:

1. **Major Components First**: Compressor, condenser, evaporator
2. **Connection Points**: Fittings, valves, service ports
3. **Refrigeration Lines**: Mechanical joints, vibration points
4. **Access Panels**: Hidden components, difficult access areas

### Documentation and Verification

#### Leak Cataloging
- **Location Documentation**: Photos, sketches, GPS coordinates
- **Severity Assessment**: Leak rate estimation, refrigerant loss
- **Repair Priority**: Safety concerns, system impact, cost factors

## Advanced Detection Techniques

### Ultrasonic Leak Detection
High-frequency sound detection for pressurized leaks:

#### Equipment Selection
- **Frequency Range**: 20-40 kHz detection capability
- **Sensitivity**: Adjustable threshold, ambient noise filtering
- **Accessories**: Probe tips, headphones, recording capability

#### Application Techniques
- **Pressurization**: Nitrogen testing, system pressure requirements
- **Probe Movement**: Systematic scanning patterns
- **Background Noise**: Identification and filtering

### Pressure Rise Testing
Quantitative leak assessment:

#### Test Setup
- **System Isolation**: Valve positioning, gauge connections
- **Baseline Establishment**: Temperature stabilization, initial readings
- **Monitoring Duration**: Time requirements for accurate assessment

#### Calculation Methods
- **Temperature Compensation**: Pressure/temperature relationships
- **Leak Rate Calculation**: Formula application, units conversion
- **Acceptance Criteria**: Industry standards, manufacturer specifications

## Field Troubleshooting Tips

### Common Problem Areas
Statistically frequent leak locations:

#### Mechanical Connections
- **Flare Fittings**: Overtightening, undertightening, thread damage
- **Brazing Joints**: Flux residue, overheating, incomplete penetration
- **Threaded Connections**: Thread sealant failure, corrosion

#### Component-Specific Issues
- **Compressor**: Shaft seals, suction/discharge connections
- **Condenser**: Tube-to-header joints, fan motor connections
- **Evaporator**: Drain pan corrosion, coil tube damage

### Environmental Considerations

#### Weather Factors
- **Wind Effects**: Dye and bubble dispersion, detector sensitivity
- **Temperature**: Expansion/contraction effects on leak rates
- **Humidity**: Corrosion acceleration, detection interference

#### Access Challenges
- **Confined Spaces**: Ventilation requirements, safety procedures
- **Height Access**: Ladder safety, scaffold requirements
- **Underground Lines**: Excavation needs, locating services

## Cost-Effective Detection Strategies

### Detector Selection
Balancing capability and cost:

- **Entry Level**: Basic heated diode detectors for general use
- **Professional Grade**: Multi-refrigerant capability, data logging
- **Specialized Tools**: Ultrasonic for specific applications

### Maintenance Economics
Tool maintenance for long-term value:

- **Calibration Schedules**: Accuracy maintenance, certification
- **Sensor Replacement**: Cost analysis, performance degradation
- **Battery Management**: Rechargeable vs. disposable, runtime

This practical guide focuses on real-world leak detection experience and field-proven techniques.""",
                        "engagement_metrics": {
                            "views": 12500,
                            "likes": 380,
                            "comments": 95,
                            "shares": 54,
                            "engagement_rate": 0.058,
                            "time_on_page": 360
                        },
                        "technical_metadata": {
                            "word_count": 1850,
                            "reading_level": "intermediate",
                            "technical_depth": 0.78,
                            "complexity_score": 0.65,
                            "practical_tips": 32,
                            "tool_references": 15
                        }
                    }
                ]
            },
            
            "hkia_current_content": {
                "competitor": "HKIA",
                "content_type": "homeowner_focused",
                "articles": [
                    {
                        "title": "Heat Pump Basics for Homeowners",
                        "content": """# Heat Pump Basics for Homeowners

## What is a Heat Pump?
A heat pump is an energy-efficient heating and cooling system that works by moving heat rather than generating it.

## How Heat Pumps Work
Heat pumps use refrigeration technology to extract heat from the outside air (even in cold weather) and move it inside your home for heating. In summer, the process reverses to provide cooling.

### Basic Components
- **Outdoor Unit**: Contains the compressor and outdoor coil
- **Indoor Unit**: Contains the indoor coil and air handler
- **Refrigerant Lines**: Connect indoor and outdoor units
- **Thermostat**: Controls system operation

## Benefits of Heat Pumps

### Energy Efficiency
- Heat pumps can be 2-4 times more efficient than traditional heating
- Lower utility bills compared to electric or oil heating
- Environmentally friendly operation

### Year-Round Comfort
- Provides both heating and cooling
- Consistent temperature control
- Improved indoor air quality with proper filtration

### Cost Savings
- Reduced energy consumption
- Potential utility rebates available
- Lower maintenance costs than separate heating/cooling systems

## Types of Heat Pumps

### Air-Source Heat Pumps
Most common type, extracts heat from outdoor air:
- **Standard Air-Source**: Works well in moderate climates
- **Cold Climate**: Designed for areas with harsh winters
- **Mini-Split**: Ductless systems for individual rooms

### Ground-Source (Geothermal)
Uses stable ground temperature:
- Higher efficiency but more expensive to install
- Excellent for areas with extreme temperatures
- Long-term energy savings

## Is a Heat Pump Right for Your Home?

### Climate Considerations
- Excellent for moderate climates
- Cold-climate models available for harsh winters
- Most effective in areas with mild to moderate temperature swings

### Home Characteristics
- Well-insulated homes benefit most
- Ductwork condition affects efficiency
- Electrical service requirements

### Financial Factors
- Higher upfront cost than traditional systems
- Long-term savings through reduced energy bills
- Available rebates and tax incentives

## Maintenance Tips for Homeowners

### Regular Tasks
- Change air filters monthly
- Keep outdoor unit clear of debris
- Check thermostat batteries
- Schedule annual professional maintenance

### Seasonal Preparation
- **Spring**: Clean outdoor coils, check refrigerant lines
- **Fall**: Clear leaves and debris, test heating mode
- **Winter**: Keep outdoor unit free of snow and ice

## When to Call a Professional
- System not heating or cooling properly
- Unusual noises or odors
- High energy bills
- Ice formation on outdoor unit in heating mode

Heat pumps offer an efficient, environmentally friendly solution for home comfort when properly selected and maintained.""",
                        "engagement_metrics": {
                            "views": 2800,
                            "likes": 67,
                            "comments": 18,
                            "shares": 9,
                            "engagement_rate": 0.034,
                            "time_on_page": 180
                        },
                        "technical_metadata": {
                            "word_count": 1200,
                            "reading_level": "general_public",
                            "technical_depth": 0.25,
                            "complexity_score": 0.30,
                            "homeowner_tips": 15,
                            "call_to_actions": 3
                        }
                    }
                ]
            }
        }
        
        return scenarios
    
    def generate_market_analysis_scenarios(self) -> Dict[str, Any]:
        """Generate market analysis test scenarios"""
        
        market_scenarios = {
            "competitive_landscape": {
                "total_market_size": 125000,  # Total monthly views
                "competitor_shares": {
                    "HVACR School": 0.42,
                    "AC Service Tech": 0.28,
                    "Refrigeration Mentor": 0.15,
                    "HKIA": 0.08,
                    "Others": 0.07
                },
                "growth_rates": {
                    "HVACR School": 0.12,  # 12% monthly growth
                    "AC Service Tech": 0.08,
                    "Refrigeration Mentor": 0.05,
                    "HKIA": 0.02,
                    "Market Average": 0.07
                }
            },
            
            "content_performance_gaps": [
                {
                    "gap_type": "technical_depth",
                    "hkia_average": 0.25,
                    "competitor_benchmark": 0.85,
                    "performance_gap": -0.60,
                    "improvement_potential": 2.4,
                    "top_performer": "HVACR School"
                },
                {
                    "gap_type": "engagement_rate",
                    "hkia_average": 0.030,
                    "competitor_benchmark": 0.065,
                    "performance_gap": -0.035,
                    "improvement_potential": 1.17,
                    "top_performer": "HVACR School"
                },
                {
                    "gap_type": "professional_content_ratio",
                    "hkia_average": 0.15,
                    "competitor_benchmark": 0.78,
                    "performance_gap": -0.63,
                    "improvement_potential": 4.2,
                    "top_performer": "HVACR School"
                }
            ],
            
            "trending_topics": [
                {
                    "topic": "heat_pump_installation",
                    "momentum_score": 0.85,
                    "competitor_coverage": ["HVACR School", "AC Service Tech"],
                    "hkia_coverage": "basic",
                    "opportunity_level": "high"
                },
                {
                    "topic": "commercial_refrigeration",
                    "momentum_score": 0.72,
                    "competitor_coverage": ["HVACR School", "Refrigeration Mentor"],
                    "hkia_coverage": "none",
                    "opportunity_level": "critical"
                },
                {
                    "topic": "diagnostic_techniques",
                    "momentum_score": 0.68,
                    "competitor_coverage": ["AC Service Tech", "HVACR School"],
                    "hkia_coverage": "minimal",
                    "opportunity_level": "high"
                }
            ]
        }
        
        return market_scenarios
    
    def save_scenarios(self) -> None:
        """Save all test scenarios to files"""
        
        # Generate content scenarios
        content_scenarios = self.generate_competitive_content_scenarios()
        with open(self.output_dir / "competitive_content_scenarios.json", 'w') as f:
            json.dump(content_scenarios, f, indent=2, default=str)
        
        # Generate market scenarios
        market_scenarios = self.generate_market_analysis_scenarios()
        with open(self.output_dir / "market_analysis_scenarios.json", 'w') as f:
            json.dump(market_scenarios, f, indent=2, default=str)
        
        print(f"Test scenarios saved to {self.output_dir}")


if __name__ == "__main__":
    generator = E2ETestDataGenerator(Path("tests/e2e_test_data"))
    generator.save_scenarios()