# -*- coding: utf-8 -*-
"""cpo-pipeline.tree.parsers.result_parsers

This module provides functions for parsing result files generated by tools
during the Tree phase of the cpo-pipeline.
"""

import csv

def parse_workflow_results(path_to_result):
    """
    Args:
        path_to_result (str): Path to the result file.

    Returns:
        list of dict: Parsed results.
        For example:
        [
            {
                "id": "BC11-Cfr001",
                "expected_species": "Citrobacter freundii",
                "mlst_species": "Citrobacter freundii",
                "sequence_type": "22",
                "mlst_scheme": "cfreundii",
                "carbapenem_resistance_genes": [
                    "NDM-1",
                    "Haemophilus influenzae PBP3 conferring resistance to beta-lactam antibiotics",
                    "marA"
                ],
                "other_amr_genes": [
                    "mdtB",
                    "TEM-1",
                    "msrE"
                ],
                "plasmids": [
                    {
                        "id": "683",
                        "num_contigs", 112,
                        "length_bp", 1702592,
                        "rep_type": "IncL/M,rep_cluster_1254",
                        "mobility": "Conjugative",
                        "nearest_reference": "JX988621"
                    },
                    ...
                ],
            }
        ]
    """
    worflow_results = []
    
    with open(path_to_result) as result_file:
        reader = csv.DictReader(result_file, delimiter = '\t')
        for row in reader:
            workflow_result = {}
            workflow_result['id'] = row['id']
            workflow_result['expected_species'] = row['expected_species']
            workflow_result['mlst_species'] = row['expected_species']
            workflow_result['sequence_type'] = row['sequence_type']
            workflow_result['mlst_scheme'] = row['mlst_scheme']
            workflow_result['carbapenem_resistance_genes'] = row['carbapenem_resistance_genes'].split(';')
            workflow_result['other_amr_genes'] = row['other_amr_genes'].split(';')
            plasmids = []
            for plasmid_id in row['plasmid_id'].split(';'):
                plasmids.append({"id": plasmid_id})
            for index, plasmid_num_contigs in enumerate(row['plasmid_num_contigs'].split(';')):
                plasmids[index]['num_contigs'] = plasmid_num_contigs
            for index, plasmid_length_bp in enumerate(row['plasmid_length_bp'].split(';')):
                plasmids[index]['length_bp'] = plasmid_length_bp
            for index, plasmid_rep_type in enumerate(row['plasmid_rep_type'].split(';')):
                plasmids[index]['rep_type'] = plasmid_rep_type
            for index, plasmid_mobility in enumerate(row['plasmid_mobility'].split(';')):
                plasmids[index]['mobility'] = plasmid_mobility
            for index, plasmid_nearest_reference in enumerate(row['plasmid_nearest_reference'].split(';')):
                plasmids[index]['nearest_reference'] = plasmid_nearest_reference
            workflow_result['plasmids'] = plasmids
        
    return worflow_results

