"""
Парсеры CLI-аргументов и определение режимов.
"""

import argparse


def build_parser() -> argparse.ArgumentParser:
    """Строит парсер аргументов CLI."""
    parser = argparse.ArgumentParser(description="MOEX ML Trading Strategy")
    parser.add_argument('--ticker', type=str, default='SBER', help='Ticker symbol')
    parser.add_argument('--timeframe', type=str, default='H1', choices=['H1', 'D1', 'W1'])
    parser.add_argument('--limit', type=int, default=None, help='Row limit')
    parser.add_argument('--cv', action='store_true', help='Run cross-validation')
    parser.add_argument('--list', action='store_true', help='List MOEX instruments')
    parser.add_argument('--save', type=str, default=None, help='Save model to path')
    parser.add_argument('--3class', '--three-class', dest='three_class', action='store_true',
                        help='Run 3-class outcome classifier')
    parser.add_argument('--rl', action='store_true', help='Run DQN reinforcement learning')
    parser.add_argument('--directional', action='store_true',
                        help='Run directional probability model (long/short success probs)')
    parser.add_argument('--multi', action='store_true',
                        help='Use multi-timeframe features (H1 + D1 + W1)')
    parser.add_argument('--train-tickers', type=str, default=None,
                        help='Comma-separated tickers to train on (e.g. SBER,GAZP,LKOH)')
    parser.add_argument('--lstm', action='store_true', help='Run LSTM model with 50-period window')
    parser.add_argument('--xgb', action='store_true', help='Use XGBoost instead of RandomForest')
    parser.add_argument('--ensemble', action='store_true', help='Ensemble RF + XGBoost')
    parser.add_argument('--importance', action='store_true',
                        help='Feature importance analysis with reduced model comparison')
    parser.add_argument('--top-k', type=int, default=None,
                        help='Use only top K features (from importance ranking) for directional model')
    parser.add_argument('--stacking', action='store_true',
                        help='Stacking ensemble (RF + XGB + LightGBM + meta-learner)')
    parser.add_argument('--lightgbm', action='store_true', help='Use LightGBM instead of RandomForest')
    parser.add_argument('--optimize', action='store_true',
                        help='Optuna hyperparameter optimization for LightGBM')
    parser.add_argument('--calibrate', action='store_true',
                        help='Calibrate probabilities via Platt scaling on held-out val set')
    parser.add_argument('--autoencoder', action='store_true',
                        help='Unsupervised autoencoder anomaly detection strategy')
    parser.add_argument('--experts', action='store_true',
                        help='Train expert ensemble (parallel LSTM experts + RF)')
    parser.add_argument('--cascade', action='store_true', help='Use cascaded expert architecture')
    parser.add_argument('--moe', action='store_true',
                        help='Multi-Timeframe MoE: train separate ExpertEnsemble for H1, D1, W1')
    parser.add_argument('--load', type=str, default=None, help='Load pre-trained model for inference')
    parser.add_argument('--benchmark', action='store_true',
                        help='Run Walk-Forward CV benchmark for RF vs XGBoost vs LightGBM')
    parser.add_argument('--benchmark-experts', action='store_true',
                        help='Include LSTM Expert Ensemble in benchmark (slow, 10-15 mins)')
    parser.add_argument('--select-features', action='store_true',
                        help='Run feature selection and collinearity removal')
    parser.add_argument('--k-features', type=int, default=30,
                        help='Number of top features to keep (used with --select-features)')
    return parser


def select_mode(args) -> str:
    """Определяет активный режим из взаимоисключающих флагов."""
    if args.moe and args.load:
        return 'moe_infer'
    if args.moe:
        return 'moe_train'
    if args.experts and args.load:
        return 'experts_infer'
    if args.experts:
        return 'experts_train'
    if args.lstm:
        return 'lstm'
    if args.directional:
        return 'directional'
    if args.list:
        return 'list'
    if args.importance:
        return 'importance'
    if args.rl:
        return 'rl'
    if args.cv:
        return 'cv'
    if args.benchmark:
        return 'benchmark'
    if args.select_features:
        return 'select_features'
    if args.three_class:
        return '3class'
    if args.autoencoder:
        return 'autoencoder'
    return 'pipeline'
