import pandas as pd
from pathlib import Path
import sequences.params as pm
from sequences.utils import extract_log_info

def recover_position_info(df):
    ''' Recover the position information from the CSV file.'''
    block = df[df['Name'] == 'block'].iloc[-1:]['Value'].values[0]
    trial = df[df['Name'] == 'trial'].iloc[-1:]['Value'].values[0]
    sequence = df[df['Name'] == 'sequence number'].iloc[-1:]['Value'].values[0]
    stimulus = df[df['Name'] == 'stimulus number'].iloc[-1:]['Value'].values[0]
    print(f'Block: {block}, Trial: {trial}, Sequence: {sequence}, Stimulus: {stimulus}')
    # if question nr has a higher index (presented last) than the sequence nr, we are in the question phase

if __name__ == '__main__':
    subject_id = input('Enter the subject ID: ')
    run_id = input('Enter the run ID: ')
    subj_dir = Path(pm.output_dir) / f'sub-{subject_id}'
    log_fn = list(subj_dir.glob(f'sub-{subject_id}*run-{run_id}*.log'))[0]
    log_info = extract_log_info(log_fn)
    log_df = pd.DataFrame(log_info)
    log_df.to_csv('/Users/pironn/Desktop/dflog.csv')
    recover_position_info(log_df)

  