"""
Test Phase 2.5: Intelligent Codebase-Aware Gap Detection

This test demonstrates the complete Phase 2.5 system that intelligently
identifies what's missing vs what's already implemented in the codebase.

Author: Atomizer Development Team
Version: 0.1.0 (Phase 2.5)
Last Updated: 2025-01-16
"""

import sys
from pathlib import Path

# Set UTF-8 encoding for Windows console
if sys.platform == 'win32':
    import codecs
    if not isinstance(sys.stdout, codecs.StreamWriter):
        if hasattr(sys.stdout, 'buffer'):
            sys.stdout = codecs.getwriter('utf-8')(sys.stdout.buffer, errors='replace')
            sys.stderr = codecs.getwriter('utf-8')(sys.stderr.buffer, errors='replace')

# Add project root to path
project_root = Path(__file__).parent.parent
sys.path.insert(0, str(project_root))

from optimization_engine.utils.codebase_analyzer import CodebaseCapabilityAnalyzer
from optimization_engine.future.workflow_decomposer import WorkflowDecomposer
from optimization_engine.config.capability_matcher import CapabilityMatcher
from optimization_engine.future.targeted_research_planner import TargetedResearchPlanner


def print_header(text: str, char: str = "="):
    """Print formatted header."""
    print(f"\n{char * 80}")
    print(text)
    print(f"{char * 80}\n")


def print_section(text: str):
    """Print section divider."""
    print(f"\n{'-' * 80}")
    print(text)
    print(f"{'-' * 80}\n")


def test_phase_2_5():
    """Test the complete Phase 2.5 intelligent gap detection system."""

    print_header("PHASE 2.5: Intelligent Codebase-Aware Gap Detection Test")

    print("This test demonstrates how the Research Agent now understands")
    print("the existing Atomizer codebase before asking for examples.\n")

    # Test request (the problematic one from before)
    test_request = (
        "I want to evaluate strain on a part with sol101 and optimize this "
        "(minimize) using iterations and optuna to lower it varying all my "
        "geometry parameters that contains v_ in its expression"
    )

    print("User Request:")
    print(f'  "{test_request}"')
    print()

    # Initialize Phase 2.5 components
    print_section("[1] Initializing Phase 2.5 Components")

    analyzer = CodebaseCapabilityAnalyzer()
    print("  CodebaseCapabilityAnalyzer initialized")

    decomposer = WorkflowDecomposer()
    print("  WorkflowDecomposer initialized")

    matcher = CapabilityMatcher(analyzer)
    print("  CapabilityMatcher initialized")

    planner = TargetedResearchPlanner()
    print("  TargetedResearchPlanner initialized")

    # Step 1: Analyze codebase capabilities
    print_section("[2] Analyzing Atomizer Codebase Capabilities")

    capabilities = analyzer.analyze_codebase()

    print("  Scanning optimization_engine directory...")
    print("  Analyzing Python files for capabilities...\n")

    print("  Found Capabilities:")
    print(f"    Optimization: {sum(capabilities['optimization'].values())} implemented")
    print(f"    Simulation:   {sum(capabilities['simulation'].values())} implemented")
    print(f"    Result Extraction: {sum(capabilities['result_extraction'].values())} implemented")
    print(f"    Geometry:     {sum(capabilities['geometry'].values())} implemented")
    print()

    print("  Result Extraction Detail:")
    for cap_name, exists in capabilities['result_extraction'].items():
        status = "FOUND" if exists else "MISSING"
        print(f"    {cap_name:15s} : {status}")

    # Step 2: Decompose workflow
    print_section("[3] Decomposing User Request into Workflow Steps")

    workflow_steps = decomposer.decompose(test_request)

    print(f"  Identified {len(workflow_steps)} atomic workflow steps:\n")
    for i, step in enumerate(workflow_steps, 1):
        print(f"  {i}. {step.action.replace('_', ' ').title()}")
        print(f"     Domain: {step.domain}")
        if step.params:
            print(f"     Params: {step.params}")
        print()

    # Step 3: Match to capabilities
    print_section("[4] Matching Workflow to Existing Capabilities")

    match = matcher.match(workflow_steps)

    print(f"  Coverage: {match.coverage:.0%} ({len(match.known_steps)}/{len(workflow_steps)} steps)")
    print(f"  Confidence: {match.overall_confidence:.0%}\n")

    print("  KNOWN Steps (Already Implemented):")
    for i, known in enumerate(match.known_steps, 1):
        print(f"    {i}. {known.step.action.replace('_', ' ').title()}")
        if known.implementation:
            impl_file = Path(known.implementation).name if known.implementation != 'unknown' else 'multiple files'
            print(f"       Implementation: {impl_file}")
    print()

    print("  MISSING Steps (Need Research):")
    for i, unknown in enumerate(match.unknown_steps, 1):
        print(f"    {i}. {unknown.step.action.replace('_', ' ').title()}")
        print(f"       Required: {unknown.step.params}")
        if unknown.similar_capabilities:
            print(f"       Can adapt from: {', '.join(unknown.similar_capabilities)}")
            print(f"       Confidence: {unknown.confidence:.0%} (pattern reuse)")
        else:
            print(f"       Confidence: {unknown.confidence:.0%} (needs research)")

    # Step 4: Create targeted research plan
    print_section("[5] Creating Targeted Research Plan")

    research_plan = planner.plan(match)

    print(f"  Generated {len(research_plan)} research steps\n")

    if research_plan:
        print("  Research Plan:")
        for i, step in enumerate(research_plan, 1):
            print(f"\n  Step {i}: {step['description']}")
            print(f"    Action: {step['action']}")
            if 'details' in step:
                if 'capability' in step['details']:
                    print(f"    Study: {step['details']['capability']}")
                if 'query' in step['details']:
                    print(f"    Query: \"{step['details']['query']}\"")
            print(f"    Expected confidence: {step['expected_confidence']:.0%}")

    # Summary
    print_section("[6] Summary - Expected vs Actual Behavior")

    print("  OLD Behavior (Phase 2):")
    print("    - Detected keyword 'geometry'")
    print("    - Asked user for geometry examples")
    print("    - Completely missed the actual request")
    print("    - Wasted time on known capabilities\n")

    print("  NEW Behavior (Phase 2.5):")
    print(f"    - Analyzed full workflow: {len(workflow_steps)} steps")
    print(f"    - Identified {len(match.known_steps)} steps already implemented:")
    for known in match.known_steps:
        print(f"        {known.step.action}")
    print(f"    - Identified {len(match.unknown_steps)} missing capability:")
    for unknown in match.unknown_steps:
        print(f"        {unknown.step.action} (can adapt from {unknown.similar_capabilities[0] if unknown.similar_capabilities else 'scratch'})")
    print(f"    - Focused research: ONLY {len(research_plan)} steps needed")
    print(f"    - Strategy: Adapt from existing OP2 extraction pattern\n")

    # Validation
    print_section("[7] Validation")

    success = True

    # Check 1: Should identify strain as missing
    has_strain_gap = any(
        'strain' in str(step.step.params)
        for step in match.unknown_steps
    )
    print(f"  Correctly identified strain extraction as missing: {has_strain_gap}")
    if not has_strain_gap:
        print("    FAILED: Should have identified strain as the gap")
        success = False

    # Check 2: Should NOT research known capabilities
    researching_known = any(
        step['action'] in ['identify_parameters', 'update_parameters', 'run_analysis', 'optimize']
        for step in research_plan
    )
    print(f"  Does NOT research known capabilities: {not researching_known}")
    if researching_known:
        print("    FAILED: Should not research already-known capabilities")
        success = False

    # Check 3: Should identify similar capabilities
    has_similar = any(
        len(step.similar_capabilities) > 0
        for step in match.unknown_steps
    )
    print(f"  Found similar capabilities (displacement, stress): {has_similar}")
    if not has_similar:
        print("    FAILED: Should have found displacement/stress as similar")
        success = False

    # Check 4: Should have high overall confidence
    high_confidence = match.overall_confidence >= 0.80
    print(f"  High overall confidence (>= 80%): {high_confidence} ({match.overall_confidence:.0%})")
    if not high_confidence:
        print("    WARNING: Confidence should be high since only 1/5 steps is missing")

    print_header("TEST RESULT: " + ("SUCCESS" if success else "FAILED"), "=")

    if success:
        print("Phase 2.5 is working correctly!")
        print()
        print("Key Achievements:")
        print("  - Understands existing codebase before asking for help")
        print("  - Identifies ONLY actual gaps (strain extraction)")
        print("  - Leverages similar code patterns (displacement, stress)")
        print("  - Focused research (4 steps instead of asking about everything)")
        print("  - High confidence due to pattern reuse (90%)")
        print()

    return success


def main():
    """Main entry point."""
    try:
        success = test_phase_2_5()
        sys.exit(0 if success else 1)
    except Exception as e:
        print(f"\nERROR: {e}")
        import traceback
        traceback.print_exc()
        sys.exit(1)


if __name__ == '__main__':
    main()
