import sys
from simulator import parallel_run, summarize, BASE_WEIGHTS
from pathlib import Path
import pandas as pd
out=Path(__file__).resolve().parents[1] / 'outputs'; out.mkdir(exist_ok=True)
RCRD_W=dict(BASE_WEIGHTS); RCRD_W.update({'empty':1.5625,'inflation':3.75,'reserve':3.125,'tardiness':1.5,'charger_wait':0.0,'priority':0.6})
CORE_W=dict(RCRD_W); CORE_W['inflation']=0.0

# Locked confirmatory main experiment: no further tuning after this point.
tasks=[]
for seed in range(1300,1360):
 for profile in ['nominal','demand_surge','aisle_disruption','combined_stress']:
  tasks += [
   dict(seed=seed,profile=profile,policy='FIFO-nearest',weights=CORE_W,fixed_target=True,config_label='FIFO-nearest'),
   dict(seed=seed,profile=profile,policy='EDD-nearest',weights=CORE_W,fixed_target=True,config_label='EDD-nearest'),
   dict(seed=seed,profile=profile,policy='Energy-core',weights=CORE_W,fixed_target=True,config_label='Matched energy-core'),
   dict(seed=seed,profile=profile,policy='RCRD-no-wait',weights=RCRD_W,fixed_target=True,config_label='Proposed RCRD'),
  ]
raw=parallel_run(tasks,'locked-main')
raw['method']=raw.config
raw.to_csv(out/'main_by_run.csv',index=False)
summarize(raw,['method']).to_csv(out/'main_overall.csv',index=False)
summarize(raw,['profile','method']).to_csv(out/'main_by_profile.csv',index=False)

# Resource robustness.
rob=[]
configs=[('fleet_constrained',6,2),('charger_constrained',8,1),('capacity_expanded',10,2)]
for seed in range(1400,1440):
 for profile in ['demand_surge','combined_stress']:
  for cfg,fleet,chg in configs:
   rob += [
    dict(seed=seed,profile=profile,policy='Energy-core',weights=CORE_W,fixed_target=True,fleet=fleet,n_chargers=chg,config_label=cfg+'|Matched energy-core'),
    dict(seed=seed,profile=profile,policy='RCRD-no-wait',weights=RCRD_W,fixed_target=True,fleet=fleet,n_chargers=chg,config_label=cfg+'|Proposed RCRD'),
   ]
rraw=parallel_run(rob,'locked-robustness')
rraw[['capacity_config','method']]=rraw.config.str.split('|',expand=True)
rraw.to_csv(out/'robustness_by_run.csv',index=False)
summarize(rraw,['capacity_config','profile','method']).to_csv(out/'robustness_summary.csv',index=False)

# One-factor-at-a-time sensitivity around locked coefficients.
variants=[('base','none',1.0,dict(RCRD_W))]
for name in ['empty','inflation','reserve','tardiness','priority']:
 for factor in (.75,1.25):
  w=dict(RCRD_W); w[name]*=factor
  variants.append((f'{name}_{factor:.2f}',name,factor,w))
sens=[]; meta={}
for label,name,factor,w in variants:
 meta[label]=(name,factor)
 for seed in range(1500,1520):
  for profile in ['demand_surge','aisle_disruption','combined_stress']:
   sens.append(dict(seed=seed,profile=profile,policy='RCRD-no-wait',weights=w,fixed_target=True,config_label=label))
sraw=parallel_run(sens,'locked-sensitivity')
sraw['weight_name']=sraw.config.map(lambda x:meta[x][0]); sraw['weight_factor']=sraw.config.map(lambda x:meta[x][1])
sraw.to_csv(out/'sensitivity_by_run.csv',index=False)
summarize(sraw,['config','weight_name','weight_factor','profile']).to_csv(out/'sensitivity_summary.csv',index=False)

ctasks=[]
for seed in range(1500,1520):
 for profile in ['demand_surge','aisle_disruption','combined_stress']:
  ctasks.append(dict(seed=seed,profile=profile,policy='Energy-core',weights=CORE_W,fixed_target=True,config_label='Matched energy-core'))
craw=parallel_run(ctasks,'locked-sensitivity-core')
craw.to_csv(out/'sensitivity_core_by_run.csv',index=False)
summarize(craw,['profile']).to_csv(out/'sensitivity_core_summary.csv',index=False)

pd.Series(RCRD_W).to_csv(out/'rcrd_weights.csv',header=['value'])
pd.Series(CORE_W).to_csv(out/'core_weights.csv',header=['value'])
print('LOCKED RCRD',RCRD_W,'CORE',CORE_W)
