import os

from multiguide.helpers import PROJECT_ROOT
from multiguide.evaluation.helpers import generate_evaluation_summary, print_evaluation_summary

# Usage example:
#experiment_dir = 'experiments_old'
# experiment_dir = 'experiments'
# #checkpoint_path = 'tanimoto_routes_completion0.8_augmentation5/checkpoint_50.pt'
# #checkpoint_path = 'tanimoto_routes_dot_separator_fraction0.4_completion0.8_augmentation5/checkpoint_90.pt'
# experiment_group = 'no_guidance'
# #experiment_group = 'reaction_type'
# experiment_group = 'reaction_type'
# #experiment_group = 'similarity'
# #process_df = True
# #time_stamp = '20250923_124256'
# process_df = False
# # # linear
# true_routes_path = 'uspto_190/in_json/test_linear_routes_classes.json'
# # subdir = 'similarity'
# # prefix = 'rootaligned_rsmiles_uspto_190_fraction0.4_thresh10'
# # prefix = 'single_step_datauspto_190first_' # 50k
# # prefix = 'neuralsym_uspto_190first_reactions_similarity_'
# #prefix = 'rootaligned_fraction1.0_thresh500_uspto_190first_reactions_with_targets_similarity'
# #prefix = r'rootaligned_fraction1.0_thresh500_uspto_190first_reactions_with_targets_similarity_reaction\d+_steeredfalse_guidance0_length0_results100_time20250921_194210'
# #prefix = r"rootaligned_fraction1.0_thresh500_uspto_190_nonlinear_reaction_type_reaction\d+_steeredtrue_guidance0.3_length15_results100_time20250922_180831"
# #prefix = r'no_guidance_rsmiles_uspto_190_fraction1.0_thresh500_reaction0_time20250922_235911'
# #prefix = r'reaction_type_rsmiles_uspto_190_fraction1.0_thresh500_reaction0_time20250923_014854'
# #prefix = r'single_step_datauspto_190first_reactions_rsmiles_50k_checkpoints_reaction0_steeredtrue_guidance1.5_length10_results100_time20250923_091553'
# #prefix = r'single_step_datauspto_190first_reactions_rsmiles_uspto_190_fraction1.0_thresh500_dropped_PtoR_aug5_reaction0_steeredtrue_guidance1.5_length10_results100_time20250923_091919'
# #prefix = r'reaction_type_rsmiles_uspto_190_fraction1.0_thresh500_reaction\d+_time20250923_103438'
# #prefix = r'reaction_type_rsmiles_uspto_190_fraction1.0_thresh500_reaction0_time20250923_095658'
# prefix = r'no_guidance_time20250923_124256'
# prefix = r'reaction_type_rsmiles_uspto_190_thresh500_time20250923_103438'

# nonlinear
#true_routes_path = 'uspto_190/in_json/test_nonlinear_routes_classes.json'
#prefix = 'neuralsym_fraction0.4_thresh10_uspto_190first_reactions_nonlinear_'
#prefix = 'rootaligned_rsmiles_uspto_190_fraction0.4_thresh10_dropped_PtoR_aug5_uspto_190first_reactions_nonlinear_' # steered false
#prefix = 'rootaligned_fraction1.0_thresh500_uspto_190first_reactions_nonlinear_' # steered false
#prefix = 'rootaligned_fraction0.4_thresh10_uspto_190_nonlinear_similarity_' # steered true
#prefix = 'rootaligned_fraction1.0_thresh500_uspto_190_nonlinear_similarity_' # steered true
# summary = generate_evaluation_summary(
#     project_root=PROJECT_ROOT,
#     experiment_dir=experiment_dir,
#     experiment_group=experiment_group,
#     experiment_filters={'experiment_regex': prefix},
#     use_starting_material=False,
#     #process_df=process_df,
#     #classifier_checkpoint_path=checkpoint_path,
#     true_routes_path=true_routes_path)


experiment_dir = 'experiments/manual_synthesis'
# experiment_group = 'no_guidance'
# experiment_regex = r'neuralsym_retrostar_time20250923_192343'
# experiment_group = 'reaction_type'
# experiment_regex = r'reaction_type_rsmiles_uspto_190_fraction1.0_thresh500_renormtrue_guidance2.0_length10_time'
# experiment_group = 'reaction_type'
# experiment_regex = r'reaction_type_rsmiles_uspto_190_fraction1.0_thresh500_renormtrue_guidance\S+_length\S+_time'
# experiment_group = 'tanimoto'
# experiment_regex = r'tanimoto_rsmiles_uspto_190_fraction1.0_thresh500_renormtrue_guidance\S+_length\S+_time'
# experiment_group = 'reaction_type'
# experiment_regex = r'uspto_190_seed42_modelrootaligned_steeredtrue_guidance1.5_length10_results100_candidates53_time20251026_112757'
experiment_group = 'no_guidance'
experiment_regex = r'uspto_190_seed42_modelrootaligned_steeredfalse_guidance0.5_length10_results100_candidates53_time20251027_111323'
process_df = False
true_routes_path = 'uspto_190/in_json/test_processed.json'
baseline_experiment_group = 'no_guidance'
baseline_experiment_filters = {'time_stamp': 'time20251026_112304'}
summary = generate_evaluation_summary(
    project_root=PROJECT_ROOT,
    experiment_dir=experiment_dir,
    experiment_group=experiment_group, 
    use_starting_material=False,
    true_routes_path=true_routes_path,
    process_df=process_df,
    experiment_filters={'experiment_regex': experiment_regex},
    baseline_experiment_group=baseline_experiment_group,
    baseline_experiment_filters=baseline_experiment_filters
)

# print avg per product metrics in the comparison
# metrics = ['avg_class_accuracy_per_product', 
# 'avg_rxn_name_accuracy_per_product', 'avg_round_trip_accuracy_per_product', 
# 'avg_best_tanimoto_to_starting_per_product', 'avg_best_tanimoto_to_target_per_product',
# 'avg_num_samples_per_product']
total_steps = [route['total_steps'] for route in summary['route_completion']['mixed_param_completion']['route_details']]
total_solved = [route['steps_solved'] for route in summary['route_completion']['mixed_param_completion']['route_details']]
differences = [steps - solved for steps, solved in zip(total_steps, total_solved)]

for i in range(0, max(differences)+1):
    print(f"Number of routes with {i} steps solved: {differences.count(i)}")

metrics = ['avg_num_samples_per_product','avg_class_correct_samples_per_product', 
            'avg_rxn_name_correct_samples_per_product', 'avg_round_trip_correct_samples_per_product', 
              'avg_best_tanimoto_to_target_per_product', 'avg_best_tanimoto_to_starting_per_product'
            ]
for metric in metrics:
    metric_name = summary['baseline_comparison']['improvement_metrics']['comparison_summary'][metric]['metric_name']
    print('-'*100)
    print(f"{metric_name}")
    print(f"guided value: {summary['baseline_comparison']['improvement_metrics']['comparison_summary'][metric]['guided_value']}")
    print(f"baseline value: {summary['baseline_comparison']['improvement_metrics']['comparison_summary'][metric]['baseline_value']}")
    print(f"absolute improvement: {summary['baseline_comparison']['improvement_metrics']['comparison_summary'][metric]['absolute_improvement']}")
    print(f"improvement ratio: {summary['baseline_comparison']['improvement_metrics']['comparison_summary'][metric]['improvement_ratio']}")
    print(f"improvement percentage: {summary['baseline_comparison']['improvement_metrics']['comparison_summary'][metric]['improvement_percentage']}")

# results = summary['route_completion']
# mixed_results = results['mixed_param_completion']
# param_analysis = results['parameter_analysis']

# print(f"sample accuracy metrics: {summary['sample_accuracy_metrics']}")
# #print(f"accuracy summary: {summary['summary_statistics']['accuracy_summary']}")

# # Overall completion rate using best params for each step
# print(f"Completion rate: {mixed_results['completion_rate']}")

# # See which params worked for step 0
# step_0_analysis = param_analysis['step_analysis'][0]
# print(f"Best param for step 0: {step_0_analysis['best_param']}")

# # See detailed route results
# for route_detail in mixed_results['route_details']:
#     if route_detail['steps_solved'] == route_detail['total_steps']:
#         print(f"Route {route_detail['route_idx']} fully solved!")
#         for step in route_detail['step_results']:
#             print(f"  Step {step['reaction_idx']}: {step['working_params']}")

#print_evaluation_summary(summary)
