#!/usr/bin/env python3
from __future__ import annotations

import json
from pathlib import Path

import pandas as pd

from validate_targeted_candidates import evaluate_candidate, OUTPUT_DIR

TOP_N = 12


def shortlist(df: pd.DataFrame) -> pd.DataFrame:
    a = df[df['bet_count'] >= 25].sort_values(['bet_acc', 'bet_count', 'test_acc'], ascending=[False, False, False]).head(TOP_N)
    b = df.sort_values(['bet_acc', 'bet_count', 'test_acc'], ascending=[False, False, False]).head(TOP_N)
    out = pd.concat([a, b], ignore_index=True).drop_duplicates(subset=['feature_mode', 'feature_set', 'train_window', 'model_name', 'threshold'])
    return out.reset_index(drop=True)


def main() -> None:
    root = OUTPUT_DIR
    summary = {}
    for code in ['d1', 'e2n']:
        src = pd.read_csv(root / f'{code}_search_results.csv')
        candidates = shortlist(src)
        rows = []
        for idx, row in candidates.iterrows():
            row = row.copy()
            if 'candidate_name' not in row.index:
                row['candidate_name'] = f"{code}_{len(rows)+1}"
            result = evaluate_candidate(code, row)
            rows.append({
                'candidate_name': row['candidate_name'],
                'stable': result['stable'],
                'mean_bet_acc': result['mean_bet_acc'],
                'min_bet_acc': result['min_bet_acc'],
                'total_bet_count': result['total_bet_count'],
                'single_split_bet_acc': float(row['bet_acc']),
                'single_split_bet_count': int(row['bet_count']),
                'feature_mode': row['feature_mode'],
                'feature_set': row['feature_set'],
                'train_window': row['train_window'],
                'model_name': row['model_name'],
                'threshold': float(row['threshold']),
            })
        out = pd.DataFrame(rows).sort_values(['stable', 'mean_bet_acc', 'min_bet_acc', 'total_bet_count'], ascending=[False, False, False, False])
        out.to_csv(root / f'{code}_stable_search_summary.csv', index=False, encoding='utf-8-sig')
        summary[code] = out.head(10).to_dict(orient='records')
    (root / 'stable_search_summary.json').write_text(json.dumps(summary, ensure_ascii=False, indent=2), encoding='utf-8')
    print(json.dumps(summary, ensure_ascii=False, indent=2))


if __name__ == '__main__':
    main()
