#!/usr/bin/env python3
"""
Merge the two raters' sheets into one merged.csv and print the scores.

Usage:
    python3 human_eval/merge_sheets.py sheet_A.csv sheet_B.csv [more.csv ...]

Writes merged.csv next to the first input and then runs score_human_eval.py on
it. Any number of sheets may be passed; rows are concatenated under one header.
"""
import sys, os, csv, subprocess

HEADER = ['clip_id', 'annotator', 'environment', 'activity', 'intention',
          'interpretation', 'notes']


def main():
    if len(sys.argv) < 3:
        print(__doc__)
        sys.exit(1)
    inputs = sys.argv[1:]
    rows = []
    for path in inputs:
        with open(path, newline='') as f:
            for r in csv.DictReader(f):
                r = {(k or '').strip().lower(): v for k, v in r.items()}
                rows.append([r.get(c, '') for c in HEADER])
    out = os.path.join(os.path.dirname(os.path.abspath(inputs[0])), 'merged.csv')
    with open(out, 'w', newline='') as f:
        w = csv.writer(f)
        w.writerow(HEADER)
        w.writerows(rows)
    print(f'Merged {len(rows)} rows from {len(inputs)} sheet(s) -> {out}\n')

    scorer = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                          'score_human_eval.py')
    subprocess.run([sys.executable, scorer, out])


if __name__ == '__main__':
    main()
