#!/usr/bin/env python

import argparse
import os

keys = []
table = {}


def set_keys():
    keys.append("folder:")
    keys.append("mode:")
    keys.append("random-seed:")
    keys.append("mutator-model:")
    keys.append("target-model:")
    keys.append("target-guardrail-id:")
    keys.append("target-guardrail-version:")
    keys.append("evaluation-model:")
    keys.append("template-select-policy:")
    keys.append("mutation-select-policy:")
    keys.append("max-queries:")
    keys.append("warmup-percent:")
    keys.append("max-jailbreaks-per-question:")
    keys.append("num-threads:")
    keys.append("#-questions:")
    keys.append("#-templates-original:")
    keys.append("time-seconds:")
    keys.append("seed-asr-top-1:")
    keys.append("seed-asr-top-5:")
    keys.append("seed-asr-top-10:")
    keys.append("seed-asr-top-20:")
    keys.append("seed-asr-all:")
    keys.append("unseeded-asr-top-1:")
    keys.append("unseeded-asr-top-5:")
    keys.append("unseeded-asr-top-10:")
    keys.append("unseeded-asr-top-20:")
    keys.append("unseeded-asr-all:")
    keys.append("#-iterations:")
    keys.append("#-queries:")
    keys.append("#-jailbreaks:")
    keys.append("#-rejects:")
    keys.append("#-questions-jailbroken:")
    keys.append("#-templates-final:")
    keys.append("#-templates-all:")
    keys.append("#-templates-invalid:")
    keys.append("guardrails-#-blocked:")
    keys.append("guardrails-#-unblocked:")
    keys.append("guardrails-%-vulnerable-blocked:")
    keys.append("guardrails-%-vulnerable-unblocked:")


def reset_values():
    for key in keys:
        table[key] = "-1"


def get_values(stat_f):
    table["folder:"] = os.path.basename(os.path.dirname(stat_f))
    with open(stat_f) as f:
        for line in f.readlines()[0:]:
            entry = line.split(':', 1)
            if len(entry) >= 1:
                lhs = entry[0] + ":"
                table[lhs] = "-1"
                assert (len(entry) == 2)
                rhs = entry[1].lstrip().rstrip()
                rhs = rhs.replace(" ", "_").replace(",", "|")
                table[lhs] = rhs


def print_keys(out_f):
    for key in keys:
        out_f.write("%s," % key.replace(":", ""))
    out_f.write("\n")


def print_values(out_f):
    for key in keys:
        out_f.write("%s," % table[key])
    out_f.write("\n")


def getopts(header):
    p = argparse.ArgumentParser(description=str(header), formatter_class=argparse.RawDescriptionHelpFormatter)
    p.add_argument('input_file', help='input file name', type=str)
    p.add_argument('output_file', help='output file name', type=str)
    args, leftovers = p.parse_known_args()
    return args, p.parse_args()


def main():
    known, opts = getopts("")

    stat_file = opts.input_file
    if not os.path.isfile(stat_file):
        raise Exception("Unable to find stats file %s" % stat_file)

    set_keys()

    out_name = opts.output_file
    print_header = not os.path.isfile(out_name)
    out_f = open(out_name, "a+")

    if print_header:
        print_keys(out_f)

    reset_values()
    get_values(stat_file)
    print_values(out_f)
    out_f.close()


if __name__ == '__main__':
    main()
