"""
Format the H-CoT and DH-CoT response files, remove redundant content, and add the original promot questions..
"""

import os
import pandas as pd
from pathlib import Path

from Tools.Response_Generation import save_format_descriptions_HCoT

import argparse
from omegaconf import OmegaConf

# Read config
parser = argparse.ArgumentParser()
parser.add_argument("--config", type=str, default="./config/cfg_Format_Response.yaml")
args = parser.parse_args()

OmegaConf.register_new_resolver("tuple", lambda *x: tuple(x))
config_API = OmegaConf.load("./config/cfg_API.yaml")
config = OmegaConf.load(args.config)
config = OmegaConf.merge(config_API, config)


# Specify folder path
dataset_csv = config.dataset_csv # the dataset of promot questions 
description_folder = config.description_folder # Folder path of files to be processed

# Recursive matching to find all .csv files
folder_path = Path(description_folder)
csv_files = list(folder_path.rglob('*.csv'))

# Iterate over the found CSV files and read the contents
for file_path in csv_files:
    print(f"Processing file: {file_path}")
    try:
        # save_format_descriptions(dataset_csv, file_path)
        save_format_descriptions_HCoT(dataset_csv, file_path)
    except Exception as e:
        print(f"Handle exceptions: {e}")