#!/usr/bin/env python3

"""Convert data csv files to PRObs Observations.

"""

import sys
from pathlib import Path
import argparse

from probs_runner import probs_convert_data, Datasource, load_datasource

# Import logging helpers
from doit_utils import log_to_file, setup_logging_to_console
import logging


CODE_DIR = Path(__file__).parent
ONTOLOGY_DIR = Path(__file__).parent.resolve().parent / 'ontology'


def parse_arguments():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--log-level",
                        help="log level (debug, info, warning, error)",
                        default="warning")
    parser.add_argument("type", help="data type of file to convert")
    parser.add_argument('input_file',
                        type=Path,
                        help='PRODCOM csv file to convert')
    parser.add_argument('output_file',
                        type=Path,
                        help='path to save output RDF')
    return parser.parse_args()


@log_to_file("build/log_convert_data.txt")
def convert_data_prodcom(data_type, data_csv, output_path):
    # Process each country's CSV file, using the shared set of loading rules
    # and shared process hierarchy definitions.
    #
    # data_csv is e.g. "PRODCOM2016DATA.csv"
    #
    # Assume that there is a file named e.g. "PRODCOM2016DATA_defs.dlog" with
    # definitions of the prefixes and time period and region to use.
    if data_type in ("prodcom_bulk_sold", "prodcom_bulk_total"):
        load_script = CODE_DIR / "load_data_prodcom_bulk.rdfox"
    else:
        load_script = CODE_DIR / f"load_data_{data_type}.rdfox"
    load_units = CODE_DIR / "load_data_units.rdfox"
    load_geonames = CODE_DIR / "load_data_geonames.rdfox"
    map_file = f"map_{data_type}.dlog"
    defs_file = (data_csv.stem + "_defs.dlog")
    units_file = "unit_conversion.dlog"
    load_data = [load_script, load_geonames]
    input_files={
        "data.csv" : data_csv,
        "geonames.csv" : CODE_DIR / "geonames.csv",
        "metrics_units.csv": CODE_DIR / "metrics_units.csv",
#        "probs-prodcom.ttl" : ONTOLOGY_DIR / "probs-prodcom.ttl",
        map_file : CODE_DIR / map_file,
    }
    if data_type in ("prodcom", "prodcom_bulk_sold", "prodcom_bulk_total"):
       load_data.append(load_units)
       input_files[units_file] = CODE_DIR / units_file
    if data_type in ("prodcom", "prodcom_list", "prodcom_correspondence"):
       input_files[defs_file] = data_csv.parent / defs_file
    datasource = Datasource.from_files(
        input_files=input_files,       
        load_data_script=load_data,
    )
    probs_convert_data([datasource], output_path)


if __name__ == "__main__":
    args = parse_arguments()
    setup_logging_to_console(level=getattr(logging, args.log_level.upper()))
    convert_data_prodcom(args.type, args.input_file, args.output_file)
