"""
Test Complete Research Workflow

This test demonstrates the full end-to-end research workflow:
1. Detect knowledge gap
2. Create research plan
3. Execute interactive research (with user example)
4. Synthesize knowledge
5. Design feature specification
6. Document research session

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

import sys
import os
from pathlib import Path

# Set UTF-8 encoding for Windows console
if sys.platform == 'win32':
    import codecs
    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.future.research_agent import (
    ResearchAgent,
    CONFIDENCE_LEVELS
)


def test_complete_workflow():
    """Test complete research workflow from gap detection to feature design."""
    print("\n" + "="*70)
    print("COMPLETE RESEARCH WORKFLOW TEST")
    print("="*70)

    agent = ResearchAgent()

    # Step 1: Detect Knowledge Gap
    print("\n" + "-"*70)
    print("[Step 1] Detect Knowledge Gap")
    print("-"*70)

    user_request = "Create NX material XML for titanium Ti-6Al-4V"
    print(f"\nUser request: \"{user_request}\"")

    gap = agent.identify_knowledge_gap(user_request)

    print(f"\n  Analysis:")
    print(f"    Missing features: {gap.missing_features}")
    print(f"    Missing knowledge: {gap.missing_knowledge}")
    print(f"    Confidence: {gap.confidence:.2f}")
    print(f"    Research needed: {gap.research_needed}")

    assert gap.research_needed, "Should detect that research is needed"
    print("\n  [PASS] Knowledge gap detected")

    # Step 2: Create Research Plan
    print("\n" + "-"*70)
    print("[Step 2] Create Research Plan")
    print("-"*70)

    plan = agent.create_research_plan(gap)

    print(f"\n  Research plan created with {len(plan.steps)} steps:")
    for step in plan.steps:
        action = step['action']
        priority = step['priority']
        expected_conf = step.get('expected_confidence', 0)
        print(f"    Step {step['step']}: {action} (priority: {priority}, confidence: {expected_conf:.2f})")

    assert len(plan.steps) > 0, "Research plan should have steps"
    assert plan.steps[0]['action'] == 'ask_user_for_example', "First step should ask user"
    print("\n  [PASS] Research plan created")

    # Step 3: Execute Interactive Research
    print("\n" + "-"*70)
    print("[Step 3] Execute Interactive Research")
    print("-"*70)

    # Simulate user providing example XML
    example_xml = """<?xml version="1.0" encoding="UTF-8"?>
<PhysicalMaterial name="Steel_AISI_1020" version="1.0">
    <Density units="kg/m3">7850</Density>
    <YoungModulus units="GPa">200</YoungModulus>
    <PoissonRatio>0.29</PoissonRatio>
    <ThermalExpansion units="1/K">1.17e-05</ThermalExpansion>
    <YieldStrength units="MPa">295</YieldStrength>
    <UltimateTensileStrength units="MPa">420</UltimateTensileStrength>
</PhysicalMaterial>"""

    print("\n  User provides example XML (steel material)")

    # Execute research with user response
    user_responses = {1: example_xml}  # Response to step 1
    findings = agent.execute_interactive_research(plan, user_responses)

    print(f"\n  Findings collected:")
    print(f"    Sources: {list(findings.sources.keys())}")
    print(f"    Confidence scores: {findings.confidence_scores}")

    assert 'user_example' in findings.sources, "Should have user example in findings"
    assert findings.confidence_scores['user_example'] == CONFIDENCE_LEVELS['user_validated'], \
        "User example should have highest confidence"
    print("\n  [PASS] Research executed and findings collected")

    # Step 4: Synthesize Knowledge
    print("\n" + "-"*70)
    print("[Step 4] Synthesize Knowledge")
    print("-"*70)

    knowledge = agent.synthesize_knowledge(findings)

    print(f"\n  Knowledge synthesized:")
    print(f"    Overall confidence: {knowledge.confidence:.2f}")
    print(f"    Patterns extracted: {len(knowledge.patterns)}")

    if knowledge.schema and 'xml_structure' in knowledge.schema:
        xml_schema = knowledge.schema['xml_structure']
        print(f"    XML root element: {xml_schema['root_element']}")
        print(f"    Required fields: {len(xml_schema['required_fields'])}")

    assert knowledge.confidence > 0.8, "Should have high confidence with user-validated example"
    assert knowledge.schema is not None, "Should have extracted schema"
    print("\n  [PASS] Knowledge synthesized")

    # Step 5: Design Feature
    print("\n" + "-"*70)
    print("[Step 5] Design Feature Specification")
    print("-"*70)

    feature_name = "nx_material_generator"
    feature_spec = agent.design_feature(knowledge, feature_name)

    print(f"\n  Feature specification created:")
    print(f"    Feature ID: {feature_spec['feature_id']}")
    print(f"    Name: {feature_spec['name']}")
    print(f"    Category: {feature_spec['category']}")
    print(f"    Subcategory: {feature_spec['subcategory']}")
    print(f"    Lifecycle stage: {feature_spec['lifecycle_stage']}")
    print(f"    Implementation file: {feature_spec['implementation']['file_path']}")
    print(f"    Number of inputs: {len(feature_spec['interface']['inputs'])}")
    print(f"    Number of outputs: {len(feature_spec['interface']['outputs'])}")

    assert feature_spec['feature_id'] == feature_name, "Feature ID should match requested name"
    assert 'implementation' in feature_spec, "Should have implementation details"
    assert 'interface' in feature_spec, "Should have interface specification"
    assert 'metadata' in feature_spec, "Should have metadata"
    assert feature_spec['metadata']['confidence'] == knowledge.confidence, \
        "Feature metadata should include confidence score"
    print("\n  [PASS] Feature specification designed")

    # Step 6: Document Session
    print("\n" + "-"*70)
    print("[Step 6] Document Research Session")
    print("-"*70)

    session_path = agent.document_session(
        topic='nx_materials_complete_workflow',
        knowledge_gap=gap,
        findings=findings,
        knowledge=knowledge,
        generated_files=[
            feature_spec['implementation']['file_path'],
            'knowledge_base/templates/material_xml_template.py'
        ]
    )

    print(f"\n  Session documented at:")
    print(f"    {session_path}")

    # Verify session files
    required_files = ['user_question.txt', 'sources_consulted.txt',
                     'findings.md', 'decision_rationale.md']
    for file_name in required_files:
        file_path = session_path / file_name
        if file_path.exists():
            print(f"    [OK] {file_name}")
        else:
            print(f"    [MISSING] {file_name}")
            assert False, f"Required file {file_name} not created"

    print("\n  [PASS] Research session documented")

    # Step 7: Validate with User (placeholder test)
    print("\n" + "-"*70)
    print("[Step 7] Validate with User")
    print("-"*70)

    validation_result = agent.validate_with_user(feature_spec)

    print(f"\n  Validation result: {validation_result}")
    print("  (Placeholder - would be interactive in real implementation)")

    assert isinstance(validation_result, bool), "Validation should return boolean"
    print("\n  [PASS] Validation method working")

    # Summary
    print("\n" + "="*70)
    print("COMPLETE WORKFLOW TEST PASSED!")
    print("="*70)

    print("\n  Summary:")
    print(f"    Knowledge gap detected: {gap.user_request}")
    print(f"    Research plan steps: {len(plan.steps)}")
    print(f"    Findings confidence: {knowledge.confidence:.2f}")
    print(f"    Feature designed: {feature_spec['feature_id']}")
    print(f"    Session documented: {session_path.name}")

    print("\n  Research Agent is fully functional!")
    print("  Ready for:")
    print("    - Interactive LLM integration")
    print("    - Web search integration (Phase 2 Week 2)")
    print("    - Feature code generation")
    print("    - Knowledge base retrieval")

    return True


if __name__ == '__main__':
    try:
        success = test_complete_workflow()
        sys.exit(0 if success else 1)
    except Exception as e:
        print(f"\n[ERROR] {e}")
        import traceback
        traceback.print_exc()
        sys.exit(1)
