/**
 *  @module     Insurance
 *  @overview   Defines the `Insurance` class
 * 
 *  @author     Animesh Mishra <hello@animesh.ltd>
 *  @copyright  © 2018 Animesh Ltd. All Rights Reserved.
 */

import * as Request         from "request-promise-native"
import * as UtilityError    from "./UtilityError"
import { Provider }         from "./Provider"
import { RIPCreds }         from "./RIPCreds"
import { UtilityBill }      from "../index"
import { StatusResponse }   from "./StatusResponse"

/** Manages insurance premium payments. */
export class Insurance {
    public provider: Provider
    public policyNumber: string
    public amount: number
    /** In DD-MM-YYYY format */
    public dob: string

    public constructor(provider: Provider, policyNumber: string, dob: string, amount: number) {
        this.provider     = provider
        this.policyNumber = policyNumber
        this.amount       = amount
        this.dob          = dob
    }

    /**
     *  Makes the bill payment of Insurance policy.
     * 
     *  @param creds    Merchant credentials to access Rocket in Pocket API
     */
    public async Pay(creds: RIPCreds, live: boolean = false): Promise<UtilityBill> {
        let options = {
            method: "GET",
            uri: `https://${creds.baseURL}/pay/insurance`,
            headers: {
                Accept: "application/json"
            },
            qs: {
                client_id: creds.merchantID,
                client_key: creds.merchantKey,
                provider_code: this.provider.code,
                connection_provider: this.provider.name,
                policy_number: this.policyNumber,
                amount: this.amount,
                dob: this.dob,
                live: live
            }
        }

        let response = await Request(options)
        response = JSON.parse(response)
        let error = UtilityError.Check(response)
        if(error) { throw error }

        return new UtilityBill(this, response)
    }

    /**
     *  Returns a list of all insurance providers supported by Rocket in Pocket
     *  API.
     */
    public static async GetProviders(creds: RIPCreds): Promise<Array<Provider>> {
        let options = {
            method: "GET",
            uri: `https://${creds.baseURL}/providers/insurance`,
            headers: {
                Accept: "application/json"
            },
            qs: {
                client_id: creds.merchantID,
                client_key: creds.merchantKey
            }
        }

        let response = await Request(options)
        response = JSON.parse(response)
        let error = UtilityError.Check(response)
        if(error) { throw error }

        let providers = Provider.InitList(response)
        return providers
    }

    /**
     *  Checks status of a previously submitted bill payment request. We make use of Rocket in
     *  Pocket callbacks for status updates. So this is largely implemented as a backup in
     *  case RIP callback systems fail.
     *
     *  @param transactionID    Magic Batua transaction ID of the recharge request
     */
    public static async CheckStatus(creds: RIPCreds, transactionID: string): Promise<StatusResponse> {
        let options = {
            method: "GET",
            uri: `https://${creds.baseURL}/recharge/order`,
            headers: {
                Accept: "application/json"
            },
            qs: {
                client_order_id: transactionID
            }
        }

        let response = await Request(options)
        response = JSON.parse(response)
        let error = UtilityError.Check(response)
        if(error) { throw error }

        return {
            operatorReference: response.opr_transid,
            vendorReference: response.rocket_trans_id,
            status: response.status,
            date: response.datetime
        }
    }
}