"""Matched baseline versus DirectNet/decoder recipe for 2+2 and 4+4.

Uses existing corpus and held-out real evidence only. No new fitting or
selection uses AASD/Carta labels; scores use fixed p_stay values.
"""
import os
import numpy as np
import pandas as pd

import eval_external as E
import fuse_external as FX
import fuse_general as FG
from common import DS_KEYS
from common_exp import EXP, fold_file
from fuse_evidence import LA

OUT=os.path.join(EXP,'results','research','baseline_recipe_4plus4.csv')
PSTAYS=(.95,.998)


def model_names(seed,recipe):
    suf='' if seed==0 else f'_s{seed}'
    dec=f'vlaai_ws__eaP_n5_t60_aug_cluster_onset_dom_xFUG_ptSPK{suf}'
    clf=f'direct_clf__eaP_n5_t60_w10b3_ptSPK{suf}'
    return (dec+'_recipe',clf+'_aug-sw2recipe') if recipe else (dec,clf+'_aug-sw2')


def preflight():
    for recipe in (False,True):
        tag='_recipeP' if recipe else '_bestP'
        ct='_w10b3recipeP' if recipe else '_w10b3sw2P'
        for seed in range(4):
            dec,clf=model_names(seed,recipe)
            for exp in (dec,clf):
                for ds in DS_KEYS:
                    path=fold_file(exp,ds)
                    if not os.path.isfile(path):raise FileNotFoundError(path)
            for ds in ('AASD','Carta2026'):
                dn=f'aasd_c1{tag}' if ds=='AASD' else f'ext_c1_{ds}{tag}'
                for path in (f'{dn}_evidence_s{seed}.pkl',f'ext_clf_{ds}{ct}_evidence_s{seed}.pkl'):
                    path=os.path.join(EXP,'results','research',path)
                    if not os.path.isfile(path):raise FileNotFoundError(path)


def main():
    preflight();rows=[]
    for group,ids in (('s01',(0,1)),('s23',(2,3)),('all4',(0,1,2,3))):
        for recipe in (False,True):
            names=[model_names(i,recipe) for i in ids]
            decs=[p[0] for p in names];clfs=[p[1] for p in names]
            tag='_recipeP' if recipe else '_bestP'
            ct='_w10b3recipeP' if recipe else '_w10b3sw2P'
            Z,meta=FG.build(decs,clfs)
            for ds in DS_KEYS:
                for p in PSTAYS:
                    r=FG.one(Z[ds],meta,ds,.5,p)
                    rows.append(dict(group=group,method='recipe' if recipe else 'sw2',kind='synthetic',**r))
            external,emis=FX.build_external([[d,str(i)] for d,i in zip(decs,ids)],
                [[c,ct,str(i)] for c,i in zip(clfs,ids)],[.5],['AASD','Carta2026'],tag)
            for ds in external:
                for p in PSTAYS:
                    r=E.score(external[ds][.5],*emis[.5],p,LA)
                    rows.append(dict(group=group,method='recipe' if recipe else 'sw2',kind='real',ds=ds,p_stay=p,**r))
            print(group,'recipe' if recipe else 'sw2','done',flush=True)
    df=pd.DataFrame(rows);df.to_csv(OUT,index=False)
    print(df.groupby(['group','method','kind','p_stay'])[['H','Acc','SDT']].mean().round(4),flush=True)
    print('wrote',OUT,flush=True)


if __name__=='__main__':main()
