from pathlib import Path
import subprocess

cfg = {
    'schema': 'temp',
    'scripts_dir': 'E:\\GlobalCube\\Tasks\\scripts',
    'query_dir': 'E:\\GlobalCube\\System\\NAVISION\\SQL\\exec',
}
target_tables = {
    'A': 'IFT_Actuals',
    'B': 'IFT_Budget',
    'C': 'IFT_Commitment',
    'K': 'IFT_Controlling_Documents',
    'P': 'IFT_Plan'
}


def task(args):
    return subprocess.Popen(args, stdout=subprocess.DEVNULL).wait()


def ift_import(csv_file, target_table):
    bulkcopy = str(Path(cfg['scripts_dir']) / 'bcp_import.bat')
    sqlquery = str(Path(cfg['scripts_dir']) / 'sqlquery.bat')
    sqlexec = str(Path(cfg['scripts_dir']) / 'sqlexec.bat')
    query_file = target_table + '.sql'

    task(f"{sqlquery} \"TRUNCATE TABLE [{cfg['schema']}].[{target_table}]\" ")
    task(f"{bulkcopy} [{cfg['schema']}].[{target_table}] \"{csv_file}\" ")
    task(f"{sqlexec} {query_file} \"{csv_file}\" ")


def import_dir(base_dir=None):
    if base_dir is None:
        base_dir = 'E:\\IFT\\staging'

    for csv_file in Path(base_dir).glob('*.csv'):
        print(csv_file.name)
        if csv_file.name.count('_') == 2:
            _, filetype, _ = csv_file.name.split('_')
            ift_import(str(csv_file), target_tables[filetype])
            csv_file.unlink()


if __name__ == '__main__':
    import_dir()
