import os
from nbutils import add_project_path_to_sys_path


# Add project root to sys.path
add_project_path_to_sys_path()

import datetime
import logging

try:
    import src.openbis_lib as openbis_lib
    import src.hdf5_ops as hdf5_ops
    #import pipelines.metadata_revision as metadata_revision
    print("Imports successful!")
except ImportError as e:
    print(f"Import error: {e}")


def main():

    #df_h5 = hdf5_lib.read_hdf5_as_dataframe_v2('BeamTimeMetaData.h5')
    #df_h5['lastModifiedDatestr'] = df_h5['lastModifiedDatestr'].astype('datetime64[ns]')
    #df_h5 = df_h5.sort_values(by='lastModifiedDatestr')

    
    openbis_obj = openbis_lib.initialize_openbis_obj()
    
    # Create df with sample measurements of type 'ISS_MEASUREMENT'
    samples = openbis_obj.get_samples(type='ISS_MEASUREMENT',props=['FILENUMBER'])
    for sample in samples:
        print(type(sample))
        print(sample.identifier)
    df_openbis = samples.df.copy(deep=True)
    h5_file_path = os.path.join(os.path.curdir,'input_files\\BeamTimeMetaData.h5')
    df_h5 = hdf5_ops.read_mtable_as_dataframe(h5_file_path)

    # dataframe preprocessing steps
    df_h5, df_openbis = openbis_lib.align_datetime_observation_windows(df_h5, df_openbis)    
    df_openbis = openbis_lib.pair_openbis_and_h5_dataframes(df_openbis, df_h5, 'REFORMATED_FILENUMBER', 'name')

    

    current_date = datetime.date.today()
    log_filename = 'logs\\computed_openbis_props_logs_' + current_date.strftime('%d-%m-%Y') + '.log'
    logging_flag = True

    #logger = logging.getLogger(__name__)
    #logger.setLevel(logging.DEBUG)

    log_file_path = os.path.join(os.path.curdir,log_filename)

    logging.basicConfig(filename=log_file_path,
                        level=logging.DEBUG,
                        format="%(asctime)s %(levelname)s %(message)s",                        
                        datefmt="%d-%m-%Y %H:%M:%S",
                        )  

    # update sample properties in openbis database only if they are labeled as bad

    props_include_list = ['sample_name', 'temp', 'cell_pressure','method_name', 'region', 'lens_mode', 'acq_mode', 'dwell_time']
    props_include_list = ['ke_range_center','ke_range_step']
    props_include_list = [ 'temp', 'cell_pressure','photon_energy','dwell_time','passenergy','ke_range_center','ke_step_center','position_x','position_y','position_z']
    
    props_include_list = ['position_x','position_y','position_z']
    props_include_list = [ 'temp', 'cell_pressure','photon_energy','dwell_time','passenergy','ke_range_center','ke_step_center']


    for sample_idx in df_openbis.index:

        #    logging.basicConfig(log_filename)
        #print(formatted_dict)
        sample_props_dict = openbis_lib.compute_openbis_sample_props_from_h5(df_openbis, df_h5, sample_idx)

        #sample_props_dict[ke_range_center]

        formatted_dict = [f"{key}:{value}" for key, value in sample_props_dict.items()]
        formatted_dict = "\n".join(formatted_dict)
        logging.debug('\n'+formatted_dict)
        try:
            filenumber = -1 if sample_props_dict['FILENUMBER'] == '' else int(sample_props_dict['FILENUMBER'])
            
            if filenumber >= 85 :
                print(filenumber)
            #if 'bad' in sample_props_dict['sample_name']:
                logging.info('The above sample is to be updated in openbis:')                
                openbis_lib.single_sample_update(sample_props_dict,samples,props_include_list)
        except KeyError:
            logging.error(KeyError)
        #print(props_dict)
    openbis_obj.logout()

    # Choose samples and specifici properties to update: create a log 


if __name__=="__main__":
    main()

