"""
Test Phase 2.6 with CBAR Element Genetic Algorithm Optimization

Tests intelligent step classification with:
- 1D element force extraction
- Minimum value calculation (not maximum)
- CBAR element (not CBUSH)
- Genetic algorithm (not Optuna TPE)
"""

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')

project_root = Path(__file__).parent.parent
sys.path.insert(0, str(project_root))

from optimization_engine.future.workflow_decomposer import WorkflowDecomposer
from optimization_engine.future.step_classifier import StepClassifier
from optimization_engine.utils.codebase_analyzer import CodebaseCapabilityAnalyzer
from optimization_engine.config.capability_matcher import CapabilityMatcher


def main():
    user_request = """I want to extract forces in direction Z of all the 1D elements and find the average of it, then find the minimum value and compere it to the average, then assign it to a objective metric that needs to be minimized.

I want to iterate on the FEA properties of the Cbar element stiffness in X to make the objective function minimized.

I want to use genetic algorithm to iterate and optimize this"""

    print('=' * 80)
    print('PHASE 2.6 TEST: CBAR Genetic Algorithm Optimization')
    print('=' * 80)
    print()
    print('User Request:')
    print(user_request)
    print()
    print('=' * 80)
    print()

    # Initialize all Phase 2.5 + 2.6 components
    decomposer = WorkflowDecomposer()
    classifier = StepClassifier()
    analyzer = CodebaseCapabilityAnalyzer()
    matcher = CapabilityMatcher(analyzer)

    # Step 1: Decompose workflow
    print('[1] Decomposing Workflow')
    print('-' * 80)
    steps = decomposer.decompose(user_request)
    print(f'Identified {len(steps)} workflow steps:')
    print()
    for i, step in enumerate(steps, 1):
        print(f'  {i}. {step.action.replace("_", " ").title()}')
        print(f'     Domain: {step.domain}')
        print(f'     Params: {step.params}')
        print()

    # Step 2: Classify steps (Phase 2.6)
    print()
    print('[2] Classifying Steps (Phase 2.6 Intelligence)')
    print('-' * 80)
    classified = classifier.classify_workflow(steps, user_request)
    print(classifier.get_summary(classified))
    print()

    # Step 3: Match to capabilities (Phase 2.5)
    print()
    print('[3] Matching to Existing Capabilities (Phase 2.5)')
    print('-' * 80)
    match = matcher.match(steps)
    print(f'Coverage: {match.coverage:.0%} ({len(match.known_steps)}/{len(steps)} steps)')
    print(f'Confidence: {match.overall_confidence:.0%}')
    print()

    print('KNOWN Steps (Already Implemented):')
    if match.known_steps:
        for i, known in enumerate(match.known_steps, 1):
            print(f'  {i}. {known.step.action.replace("_", " ").title()} ({known.step.domain})')
            if known.implementation != 'unknown':
                impl_name = Path(known.implementation).name if ('\\' in known.implementation or '/' in known.implementation) else known.implementation
                print(f'     File: {impl_name}')
    else:
        print('  None')
    print()

    print('MISSING Steps (Need Research):')
    if match.unknown_steps:
        for i, unknown in enumerate(match.unknown_steps, 1):
            print(f'  {i}. {unknown.step.action.replace("_", " ").title()} ({unknown.step.domain})')
            print(f'     Required: {unknown.step.params}')
            if unknown.similar_capabilities:
                similar_str = ', '.join(unknown.similar_capabilities)
                print(f'     Similar to: {similar_str}')
                print(f'     Confidence: {unknown.confidence:.0%} (can adapt)')
            else:
                print(f'     Confidence: {unknown.confidence:.0%} (needs research)')
            print()
    else:
        print('  None - all capabilities are known!')
    print()

    # Step 4: Intelligent Analysis
    print()
    print('[4] Intelligent Decision: What to Research vs Auto-Generate')
    print('-' * 80)
    print()

    eng_features = classified['engineering_features']
    inline_calcs = classified['inline_calculations']
    hooks = classified['post_processing_hooks']

    print('ENGINEERING FEATURES (Need Research/Documentation):')
    if eng_features:
        for item in eng_features:
            step = item['step']
            classification = item['classification']
            print(f'  - {step.action} ({step.domain})')
            print(f'    Reason: {classification.reasoning}')
            print(f'    Requires documentation: {classification.requires_documentation}')
            print()
    else:
        print('  None')
        print()

    print('INLINE CALCULATIONS (Auto-Generate Python):')
    if inline_calcs:
        for item in inline_calcs:
            step = item['step']
            classification = item['classification']
            print(f'  - {step.action}')
            print(f'    Complexity: {classification.complexity}')
            print(f'    Auto-generate: {classification.auto_generate}')
            print()
    else:
        print('  None')
        print()

    print('POST-PROCESSING HOOKS (Generate Middleware):')
    if hooks:
        for item in hooks:
            step = item['step']
            print(f'  - {step.action}')
            print(f'    Will generate hook script for custom objective calculation')
            print()
    else:
        print('  None detected (but likely needed based on request)')
        print()

    # Step 5: Key Differences from Previous Test
    print()
    print('[5] Differences from CBUSH/Optuna Request')
    print('-' * 80)
    print()
    print('Changes Detected:')
    print('  - Element type: CBAR (was CBUSH)')
    print('  - Direction: X (was Z)')
    print('  - Metric: minimum (was maximum)')
    print('  - Algorithm: genetic algorithm (was Optuna TPE)')
    print()
    print('What This Means:')
    print('  - CBAR stiffness properties are different from CBUSH')
    print('  - Genetic algorithm may not be implemented (Optuna is)')
    print('  - Same pattern for force extraction (Z direction still works)')
    print('  - Same pattern for intermediate calculations (min vs max is trivial)')
    print()

    # Summary
    print()
    print('=' * 80)
    print('SUMMARY: Atomizer Intelligence')
    print('=' * 80)
    print()
    print(f'Total Steps: {len(steps)}')
    print(f'Engineering Features: {len(eng_features)} (research needed)')
    print(f'Inline Calculations: {len(inline_calcs)} (auto-generate)')
    print(f'Post-Processing Hooks: {len(hooks)} (auto-generate)')
    print()
    print('Research Effort:')
    print(f'  Features needing documentation: {sum(1 for item in eng_features if item["classification"].requires_documentation)}')
    print(f'  Features needing research: {sum(1 for item in eng_features if item["classification"].requires_research)}')
    print(f'  Auto-generated code: {len(inline_calcs) + len(hooks)} items')
    print()


if __name__ == '__main__':
    main()
