#!/usr/bin/env python3
'''Run mg_import_csv and test that recovery works with mg_recovery_check.'''

import argparse
import subprocess
import os
import tempfile


_SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))


def parse_args():
    argp = argparse.ArgumentParser(description=__doc__)
    argp.add_argument('--mg-import-csv', required=True,
                      help='Path to mg_import_csv executable.')
    argp.add_argument('--mg-recovery-check', required=True,
                      help='Path to mg_recovery_check executable.')
    return argp.parse_args()


def main():
    args = parse_args()
    comment_nodes = os.path.join(_SCRIPT_DIR, 'csv', 'comment_nodes.csv')
    comment_nodes_2 = os.path.join(_SCRIPT_DIR, 'csv', 'comment_nodes_2.csv')
    forum_nodes = os.path.join(_SCRIPT_DIR, 'csv', 'forum_nodes.csv')
    relationships_0 = os.path.join(_SCRIPT_DIR, 'csv', 'relationships_0.csv')
    relationships_1 = os.path.join(_SCRIPT_DIR, 'csv', 'relationships_1.csv')
    with tempfile.TemporaryDirectory(suffix='-durability',
                                     dir=_SCRIPT_DIR) as durability_dir:
        snapshot_dir = os.path.join(durability_dir, 'snapshots')
        os.makedirs(snapshot_dir, exist_ok=True)
        out_snapshot = os.path.join(snapshot_dir, 'snapshot')
        mg_import_csv = [args.mg_import_csv, '--nodes', comment_nodes,
                         '--nodes={}'.format(forum_nodes),
                         '--node-label', 'First', '--node-label', 'Second',
                         '--relationships={}'.format(relationships_0),
                         '--relationships', relationships_1,
                         '--out', out_snapshot, '--csv-delimiter=|',
                         '--quote', 'Ö',  # test multi-char quote
                         '--array-delimiter=;']
        subprocess.check_call(mg_import_csv)
        mg_recovery_check = [args.mg_recovery_check,
                             '--durability-dir', durability_dir,
                             '--gtest_filter=*RecoveryTest*:-*RelationshipType*']  # noqa
        subprocess.check_call(mg_recovery_check)

        # New snapshot to test relationship type flag and single-char quote
        out_snapshot = os.path.join(snapshot_dir, 'snapshot2')
        mg_import_csv = [args.mg_import_csv, '--nodes', comment_nodes_2,
                         '--nodes={}'.format(forum_nodes),
                         '--relationships={}'.format(relationships_0),
                         '--relationships', relationships_1,
                         '--relationship-type', "TYPE",
                         '--out', out_snapshot, '--csv-delimiter=|',
                         '--quote', '\"', '--array-delimiter=;']
        subprocess.check_call(mg_import_csv)
        mg_recovery_check = [args.mg_recovery_check,
                             '--durability-dir', durability_dir,
                             '--gtest_filter=*RelationshipType*:*Quote*']
        subprocess.check_call(mg_recovery_check)


if __name__ == '__main__':
    main()
