# augment.py
# augment the training sets

import argparse
import pandas as pd
import os
import ast
from nkululeko.experiment import Experiment
import configparser
from nkululeko.util import Util
from nkululeko.constants import VERSION


def main(src_dir):
    parser = argparse.ArgumentParser(description="Call the nkululeko framework.")
    parser.add_argument("--config", default="exp.ini", help="The base configuration")
    args = parser.parse_args()
    if args.config is not None:
        config_file = args.config
    else:
        config_file = f"{src_dir}/exp.ini"

    # test if the configuration file exists
    if not os.path.isfile(config_file):
        print(f"ERROR: no such file: {config_file}")
        exit()

    # load one configuration per experiment
    config = configparser.ConfigParser()
    config.read(config_file)
    # create a new experiment
    expr = Experiment(config)
    util = Util("augment")
    util.debug(
        f"running {expr.name} from config {config_file}, nkululeko version"
        f" {VERSION}"
    )

    if util.config_val("EXP", "no_warnings", False):
        import warnings

        warnings.filterwarnings("ignore")

    # load the data
    expr.load_datasets()

    # split into train and test
    expr.fill_train_and_tests()
    util.debug(f"train shape : {expr.df_train.shape}, test shape:{expr.df_test.shape}")

    # augment
    augmentings = util.config_val("AUGMENT", "augment", False)
    got_one = False
    if augmentings:
        augmentings = ast.literal_eval(augmentings)
        def_ret = pd.DataFrame()
        if "traditional" in augmentings:
            df_ret = expr.augment()
            got_one = True
        if "random_splice" in augmentings:
            df_ret = pd.concat([def_ret, expr.random_splice()])
            got_one = True
    if not augmentings:
        util.error("no augmentation selected")
    if not got_one:
        util.error(f"invalid augmentation(s): {augmentings}")
    # remove encoded labels
    target = util.config_val("DATA", "target", "emotion")
    if "class_label" in df_ret.columns:
        df_ret = df_ret.drop(columns=[target])
        df_ret = df_ret.rename(columns={"class_label": target})
    # save file
    filename = util.config_val("AUGMENT", "result", "augmented.csv")

    df_ret.to_csv(f"{expr.data_dir}/{filename}")
    util.debug(f"saved augmentation table to {filename} to {expr.data_dir}")
    print("DONE")


if __name__ == "__main__":
    cwd = os.path.dirname(os.path.abspath(__file__))
    main(cwd)  # use this if you want to state the config file path on command line
