// Parse and return an LDB Request

// FUNCTIONS
//  post(body, id): Exported:
            // body: [req.body from controller]
            // id  : [req.params.id from controller - this is expected to be CRS or TIPLOC]

//  convertTiploc(TIPLOC)  : Exported: Looks up CRS, Name & STANOX for Tiploc

const log  = require('../utils/log.utils');          // Log Helper
const ldb  = require('ldbs-json')
const util = require('../utils/ldb.utils')
const san  = require('../utils/sanitizer.utils')

const ldbKey   = process.env.OWL_LDB_KEY
const ldbsvKey = process.env.OWL_LDB_SVKEY

async function get(body, id){
    var cleanId = san.cleanApiEndpointTxt(id);
    var obj = await util.checkCrs(cleanId);
    try {
        var crs = obj[0]['3ALPHA'];
        log.out(`ldbService.get: Determined CRS for lookup to be: ${crs}`);
        var data = await arrDepBoard(crs);
    } catch (err) {
        log.out(`ldbService.get: Error, Unable to find CRS: ${err}`)
        var data = {ERROR:'NOT_FOUND',description:'The entered station was not found.  Please check and try again.'};
    }
    try {
        if (data.GetStationBoardResult.nrccMessages) {
            var msg = await util.cleanMessages(data.GetStationBoardResult.nrccMessages);
            data.GetStationBoardResult.nrccMessages.message = msg;
        }
        if (data.GetStationBoardResult.trainServices && !Array.isArray(data.GetStationBoardResult.trainServices.service)) {
            log.out(`ldbService.get: Transforming trainServices`)
            var services = await util.cleanServices(data.GetStationBoardResult.trainServices.service)
            data.GetStationBoardResult.trainServices.service = services;
        }
        if (data.GetStationBoardResult.ferryServices && !Array.isArray(data.GetStationBoardResult.ferryServices.service)) {
            log.out(`ldbService.get: Transforming ferryServices`)
            var services = await util.cleanServices(data.GetStationBoardResult.ferryServices.service)
            data.GetStationBoardResult.ferryServices.service = services;
        }
        if (data.GetStationBoardResult.busServices && !Array.isArray(data.GetStationBoardResult.busServices.service)) {
            log.out(`ldbService.get: Transforming busServices`)
            var services = await util.cleanServices(data.GetStationBoardResult.busServices.service)
            data.GetStationBoardResult.busServices.service = services;
        }
    } catch (err) {
        log.out(`ldbService.get: Error transforming upstream data: ${err}`)
    }
    return data;
}

async function arrDepBoard(CRS){
    log.out(`ldbService.arrDepBoard: Trying to fetch ArrDep Board for ${CRS}`)
    try {
        var options = {
            numRows: 10,
            crs: CRS.toUpperCase()
        }
        var api = new ldb(ldbKey,false)
        var reply = await api.call("GetArrDepBoardWithDetails",options)
        return reply
    } catch (err) {
        log.out(`ldbService.arrDepBoard: Lookup Failed for: ${CRS}`)
        return {GetStationBoardResult: "not available", Reason: `The CRS code ${CRS} is not valid`, Why: `Sometimes a station will have more than one CRS - for example Filton Abbey Wood has FIT and FAW however schedules are only available when looking up with FIT - this is how the National Rail Enquiries systems work.`};
    }
};

module.exports = {
    get
}