"""Point d'entrée du bot-detector : boucle principale d'analyse."""
import time

from .config import *  # noqa: F403 — All config for startup banner
from .log import log_info, log_decision
from .cycle import fetch_and_analyze

if __name__ == '__main__':
    log_info('')
    log_info('╔═══════════════════════════════════════════════════════════════╗')
    log_info('║   BOT DETECTOR IA v12 — Pipeline de détection semi-supervisé ║')
    log_info('╚═══════════════════════════════════════════════════════════════╝')
    log_info(f'  Base de données       : {DB}')
    log_info(f'  Contamination EIF     : {CONTAMINATION}')
    log_info(f'  Seuil anomalie        : {ANOMALY_THRESHOLD} (adaptatif p{ANOMALY_PERCENTILE})')
    log_info(f'  Cycle d\'analyse       : toutes les {CYCLE_INTERVAL}s')
    log_info(f'  Retraining            : toutes les {RETRAIN_INTERVAL_H}h (drift seuil={DRIFT_THRESHOLD:.0%})')
    log_info(f'  Modèles               : {MODEL_DIR}')
    log_info(f'  Autoencoder (AE)      : poids={AE_WEIGHT} {"(PyTorch disponible)" if TORCH_AVAILABLE else "(PyTorch absent — désactivé)"}')
    log_info(f'  XGBoost               : poids={XGB_WEIGHT} (min labels={XGB_MIN_LABELS})')
    log_info(f'  SHAP explainabilité   : {"activé" if ENABLE_SHAP else "désactivé (shap non installé)" if not SHAP_AVAILABLE else "désactivé"}')
    log_info(f'  Clustering HDBSCAN    : {"activé" if ENABLE_CLUSTERING else "désactivé"}')
    log_info(f'  Dédup inter-cycles    : TTL={DEDUP_TTL_MIN}min')
    log_info(f'  Récurrence            : weight={RECURRENCE_WEIGHT}')
    log_info(f'  Multi-fenêtres (24h)  : {"activé" if ENABLE_MULTIWINDOW else "désactivé"}')
    log_info(f'  Feature ratio minimum : {MIN_VALID_FEATURE_RATIO:.0%}')
    log_info(f'  Anubis                : ALLOW→KNOWN_BOT, DENY→forcé menace (score IF réel)')
    log_info(f'  Browser légitime      : confidence≥{BROWSER_CONFIDENCE_THRESHOLD} (5 axes), cohorte≥{BROWSER_COHORT_RATIO}')
    log_info(f'  Feedback SOC          : {"activé" if ENABLE_FEEDBACK else "désactivé"} (fenêtre={FEEDBACK_WINDOW_DAYS}j)')
    log_info('')
    log_decision('SERVICE_START', 'boot', '', {
        'db': DB, 'contamination': CONTAMINATION, 'anomaly_threshold': ANOMALY_THRESHOLD,
        'cycle_interval': CYCLE_INTERVAL, 'retrain_interval_h': RETRAIN_INTERVAL_H
    })
    log_info(f'En attente du premier cycle dans {CYCLE_INTERVAL}s…')
    while True:
        try:
            fetch_and_analyze()
        except Exception as e:
            log_info(f"Erreur globale : {e}")
        time.sleep(CYCLE_INTERVAL)
