#! /usr/bin/env python
# -*- coding: utf-8 -*-

import click

import numpy as np
import pandas as pd

from saqc.core import run
from saqc.flagger import CategoricalFlagger


FLAGGERS = {
    "numeric": CategoricalFlagger([-1, 0, 1]),
    "category": CategoricalFlagger(["NIL", "OK", "BAD"]),
}


@click.command()
@click.option(
    "-c", "--config", type=click.Path(exists=True), required=True, help="path to the configuration file",
)
@click.option(
    "-d", "--data", type=click.Path(exists=True), required=True, help="path to the data file",
)
@click.option("-o", "--outfile", type=click.Path(exists=False), help="path to the output file")
@click.option(
    "--flagger", default="category", type=click.Choice(FLAGGERS.keys()), help="the flagging scheme to use",
)
@click.option("--nodata", default=np.nan, help="nodata value")
@click.option("--fail/--no-fail", default=True, help="whether to stop the program run on errors")
def main(config, data, flagger, outfile, nodata, fail):

    data = pd.read_csv(data, index_col=0, parse_dates=True,)

    data_result, flagger_result = run(
        config_file=config,
        flagger=FLAGGERS[flagger],
        data=data,
        nodata=nodata,
        error_policy="raise" if fail else "warn",
    )

    if outfile:
        flags = flagger_result.getFlags()
        flags_out = flags.where((flags.isnull() | flagger_result.isFlagged()), flagger_result.GOOD)
        cols_out = sum([[c, c + "_flags"] for c in flags_out], [])
        data_out = data_result.join(flags_out, rsuffix="_flags")
        data_out[cols_out].to_csv(outfile, header=True, index=True)


if __name__ == "__main__":
    main()
