#!/usr/bin/env python3.7

import codecs
import csv
import logging
import os
import shutil
import time


def read_ean_lookup(ref_path):
    ref_dict = {}

    header_read = False
    full_ref_path = os.path.join(ref_path, "Ref_FBA_EK.csv")
    with codecs.open(full_ref_path, "rb", encoding='utf-8') as f:

        reader = csv.reader(f, delimiter=';')
        ean_idx = -1
        price_idx = -1
        for row in reader:
            if header_read:
                ean = row[ean_idx].strip()
                price_euro = row[price_idx]
                price_conv = price_euro.replace(".", "").replace(",", ".")
                try:
                    price = float(price_conv)
                    ref_dict[ean] = price
                except ValueError as ve:
                    logging.error(ve)
            else:
                ean_idx = row.index("VariationEAN")
                price_idx = row.index("PurchasePrice")
                header_read = True

    return ref_dict

def convert_fba(in_file, out_file, ref_dict):
    header_read = False
    ean_index = -1
    qty_index = -1
    matches = 0
    non_matches = 0

    with codecs.open(in_file, "r", encoding='utf-8') as f, \
        codecs.open(out_file, "w", encoding='utf-8') as of:

        reader = csv.reader(f, delimiter='\t')
        writer = csv.writer(of, delimiter=';')

        total = 0.0
        for row in reader:
            if header_read:
                ean = row[ean_index]
                qty = row[qty_index]

                num_qty = 0.0
                if ean:
                    if len(ean) > 13:
                        ean = ean[:13]

                    if ean in ref_dict:
                        try:
                            num_qty = float(qty)
                            price = ref_dict[ean]
                            if isinstance(price, float):
                                sub_total = round(num_qty * price, 2)
                                total += sub_total
                                writer.writerow([ean, sub_total])
                                matches += 1
                            else:
                                logging.error("Can't parse price {}".format(price))
                        except ValueError as ve:
                            logging.error(ve)
                            logging.error("could not convert " + qty)
                    else:
                        non_matches += 1
                        logging.warning("{0} not found ".format(ean))
            else:
                header_read = True

                if "sku" in row:
                    ean_index = row.index("sku")

                if "afn-total-quantity" in row:
                    qty_index = row.index("afn-total-quantity")

                if ean_index < 0 or qty_index < 0:
                    raise Exception("Could not find needed columns")

                writer.writerow(['EAN', 'Amount'])
        writer.writerow(['Total', round(total, 2)])
    logging.info(f"Matches {matches}")
    logging.info(f"Non-matches {non_matches}")

def process(data_root):
    path_ref = os.path.join(data_root, "ref")
    path_in = os.path.join(data_root, "in")
    path_out = os.path.join(data_root, "out")
    path_proc = os.path.join(data_root, "processed")
    path_err = os.path.join(data_root, "error")
    path_log = os.path.join(data_root, "log")

    files = os.listdir(path_in)
    num_files = len(files)

    print("Found {0} files...".format(num_files))

    if num_files == 0:
        return

    ref_dict = read_ean_lookup(path_ref)

    for f in files:
        log_file_name = time.strftime("%Y%m%d-%H%M%S") + '.log'

        in_file = os.path.join(path_in, f)
        out_file_name = 'fba_' + os.path.splitext(f)[0] + '.csv'
        out_file = os.path.join(path_out, out_file_name)
        proc_file = os.path.join(path_proc, f)
        err_file = os.path.join(path_err, f)
        log_file = os.path.join(path_log, log_file_name)

        logging.basicConfig(filename=log_file, level=logging.DEBUG)

        logging.debug("Calculating inventory for %s...", in_file)

        if os.path.isfile(in_file):
            try:
                print("Converting {0} to {1}...".format(in_file, out_file))
                convert_fba(in_file, out_file, ref_dict)
                shutil.move(in_file, proc_file)
            except Exception as e:
                logging.error(e)
                shutil.move(in_file, err_file)
                os.remove(out_file)


def main():
    if os.path.exists(r"/ftproot/"):
        data_root = r"/ftproot/"
    elif os.path.exists(r"/home/holger/git/bitbucket/thelen/data/"):
        data_root = r"/home/holger/git/bitbucket/thelen/data/"
    else:
        print(
            "Unknown environment. Please set data_root to data folder location. Aborting."
        )
        return

    project_data_root = os.path.join(data_root, "Matching_FBA_EK")

    process(project_data_root)

    print("Done.")


if __name__ == "__main__":
    main()
