"""
Atomizer Optimization Engine
============================

Structural optimization framework for Siemens NX.

New Module Structure (v2.0):
- core/           - Optimization runners
- processors/     - Data processing (surrogates, dynamic_response)
- nx/             - NX/Nastran integration
- study/          - Study management
- reporting/      - Reports and analysis
- config/         - Configuration
- extractors/     - Physics extraction (unchanged)
- insights/       - Visualizations (unchanged)
- gnn/            - Graph neural networks (unchanged)
- hooks/          - NX hooks (unchanged)
- utils/          - Utilities
- validators/     - Validation (unchanged)

Quick Start:
    from optimization_engine.core import OptimizationRunner
    from optimization_engine.nx import NXSolver
    from optimization_engine.extractors import extract_displacement
"""

__version__ = '2.0.0'

import warnings as _warnings
import importlib as _importlib

# =============================================================================
# SUBMODULE LIST
# =============================================================================
_SUBMODULES = {
    'core', 'processors', 'nx', 'study', 'reporting', 'config',
    'extractors', 'insights', 'gnn', 'hooks', 'utils', 'validators',
}

# =============================================================================
# BACKWARDS COMPATIBILITY LAYER
# =============================================================================
# These aliases allow old imports to work with deprecation warnings.
# Will be removed in v3.0.

_DEPRECATED_MAPPINGS = {
    # Core
    'runner': 'optimization_engine.core.runner',
    'base_runner': 'optimization_engine.core.base_runner',
    'intelligent_optimizer': 'optimization_engine.core.intelligent_optimizer',
    'method_selector': 'optimization_engine.core.method_selector',
    'strategy_selector': 'optimization_engine.core.strategy_selector',
    'strategy_portfolio': 'optimization_engine.core.strategy_portfolio',
    'gradient_optimizer': 'optimization_engine.core.gradient_optimizer',
    'runner_with_neural': 'optimization_engine.core.runner_with_neural',

    # Surrogates
    'neural_surrogate': 'optimization_engine.processors.surrogates.neural_surrogate',
    'generic_surrogate': 'optimization_engine.processors.surrogates.generic_surrogate',
    'adaptive_surrogate': 'optimization_engine.processors.surrogates.adaptive_surrogate',
    'simple_mlp_surrogate': 'optimization_engine.processors.surrogates.simple_mlp_surrogate',
    'active_learning_surrogate': 'optimization_engine.processors.surrogates.active_learning_surrogate',
    'surrogate_tuner': 'optimization_engine.processors.surrogates.surrogate_tuner',
    'auto_trainer': 'optimization_engine.processors.surrogates.auto_trainer',
    'training_data_exporter': 'optimization_engine.processors.surrogates.training_data_exporter',

    # NX
    'nx_solver': 'optimization_engine.nx.solver',
    'nx_updater': 'optimization_engine.nx.updater',
    'nx_session_manager': 'optimization_engine.nx.session_manager',
    'solve_simulation': 'optimization_engine.nx.solve_simulation',
    'solve_simulation_simple': 'optimization_engine.nx.solve_simulation_simple',
    'model_cleanup': 'optimization_engine.nx.model_cleanup',
    'export_expressions': 'optimization_engine.nx.export_expressions',
    'import_expressions': 'optimization_engine.nx.import_expressions',
    'mesh_converter': 'optimization_engine.nx.mesh_converter',

    # Study
    'study_creator': 'optimization_engine.study.creator',
    'study_wizard': 'optimization_engine.study.wizard',
    'study_state': 'optimization_engine.study.state',
    'study_reset': 'optimization_engine.study.reset',
    'study_continuation': 'optimization_engine.study.continuation',
    'benchmarking_substudy': 'optimization_engine.study.benchmarking',
    'generate_history_from_trials': 'optimization_engine.study.history_generator',

    # Reporting
    'generate_report': 'optimization_engine.reporting.report_generator',
    'generate_report_markdown': 'optimization_engine.reporting.markdown_report',
    'comprehensive_results_analyzer': 'optimization_engine.reporting.results_analyzer',
    'visualizer': 'optimization_engine.reporting.visualizer',
    'landscape_analyzer': 'optimization_engine.reporting.landscape_analyzer',

    # Config
    'config_manager': 'optimization_engine.config.manager',
    'optimization_config_builder': 'optimization_engine.config.builder',
    'optimization_setup_wizard': 'optimization_engine.config.setup_wizard',
    'capability_matcher': 'optimization_engine.config.capability_matcher',
    'template_loader': 'optimization_engine.config.template_loader',

    # Utils
    'logger': 'optimization_engine.utils.logger',
    'auto_doc': 'optimization_engine.utils.auto_doc',
    'realtime_tracking': 'optimization_engine.utils.realtime_tracking',
    'codebase_analyzer': 'optimization_engine.utils.codebase_analyzer',
    'pruning_logger': 'optimization_engine.utils.pruning_logger',

    # Future
    'research_agent': 'optimization_engine.future.research_agent',
    'pynastran_research_agent': 'optimization_engine.future.pynastran_research_agent',
    'targeted_research_planner': 'optimization_engine.future.targeted_research_planner',
    'workflow_decomposer': 'optimization_engine.future.workflow_decomposer',
    'step_classifier': 'optimization_engine.future.step_classifier',

    # Extractors/Validators
    'op2_extractor': 'optimization_engine.extractors.op2_extractor',
    'extractor_library': 'optimization_engine.extractors.extractor_library',
    'simulation_validator': 'optimization_engine.validators.simulation_validator',

    # Processors
    'adaptive_characterization': 'optimization_engine.processors.adaptive_characterization',
}

# =============================================================================
# LAZY LOADING
# =============================================================================

def __getattr__(name):
    """Lazy import for submodules and backwards compatibility."""
    # Handle submodule imports (e.g., from optimization_engine import core)
    if name in _SUBMODULES:
        return _importlib.import_module(f'optimization_engine.{name}')

    # Handle deprecated imports with warnings
    if name in _DEPRECATED_MAPPINGS:
        new_module = _DEPRECATED_MAPPINGS[name]
        _warnings.warn(
            f"Importing '{name}' from optimization_engine is deprecated. "
            f"Use '{new_module}' instead. "
            f"This will be removed in v3.0.",
            DeprecationWarning,
            stacklevel=2
        )
        return _importlib.import_module(new_module)

    raise AttributeError(f"module 'optimization_engine' has no attribute '{name}'")


__all__ = [
    # Version
    '__version__',
    # Submodules
    'core',
    'processors',
    'nx',
    'study',
    'reporting',
    'config',
    'extractors',
    'insights',
    'gnn',
    'hooks',
    'utils',
    'validators',
]
