from __future__ import absolute_import, division, print_function, unicode_literals

import os

from publisher import settings
from publisher.processing.data_sources.utils import read_csv_file


def get_procedure_details(procedure_code, production_data):

    procedure_row = next((r for r in production_data if is_procedure_row(procedure_code, r)), None)
    if not procedure_row:
        raise EOFError("Could not find Procedure: %s in production csv." % procedure_code)

    procedure_name = procedure_row["Procedure Name"] or "%s - UNKNOWN" % procedure_code
    specialties = procedure_row["Speciality"].split("~") if procedure_row["Speciality"] else []
    channel = procedure_row["Channel"]
    vbs = procedure_row["VBS"]

    return procedure_name, specialties, channel, vbs


def get_procedure_phase_list(procedure_code, production_data):
    return [get_phase_details(r) for r in production_data if is_procedure_row(procedure_code, r)]


def get_phase_data(phase_code, production_data):
    return [get_phase_details(r) for r in production_data if is_phase_row(phase_code, r)]


def get_phase_details(r):
    return {"phase_code": r["Name"], "released_as": r["Released As"], 'phase_name': r['Module Name'],
            'procedure_code': r["Procedure Group"], 'vbs': r["VBS"]}


def is_procedure_row(procedure_code, r):
    return r["Procedure Group"] == procedure_code and r["Status"] != "RETIRED"


def is_phase_row(phase_code, r):
    return r["Name"] == phase_code and r["Status"] != "RETIRED"


def get_production_csv_data():
    if os.path.isfile(settings.BASE_DATA_DIR):
        module_csv = os.path.join(settings.PRODUCTION_INFO_DIR, "module.csv")
    else:
        module_csv = os.path.join(settings.PRODUCTION_INFO_BACKUP_DIR, "module.csv")

    headers, rows = read_csv_file(module_csv)
    return rows
