// Topup management toosl
#include "clog.h"
#include "cgi.h"
#include "input.h"
#include "mermsemr_api.h"
#include "safestring.h"
#include <string>
#include "pgsql.h"
#include "pgsql_wrapper.h"
#include <curl/curl.h>
#include "medTEmails.h"
/* -- */
#include "function_driver.h"
#include "function_agents.h"
#include "medTrTransporter.h"


long medTrDriverFunctions(CVars in, CVars &out) {
    long ret = PHP_API_BAD_PARAM;

    try {
        REQ_LONG(in, "agent_id", 0, -1);
        long mode = REQ_LONG(in, "mode", 0, -1);

        if (mode == MODE_ADD) {
            return medTrCreateDriver(in, out);
        }

        if (mode == MODE_LIST) {
            return medTrDriverList(in, out);
        }

        if (mode == MODE_UPDATE) {
            return medTrDriverUpdate(in, out);
        }


    } catch (bad_parameter) {
        logfmt(logINFO, "ERROR CALL long medTrDriverFunctions(CVars in, CVars &out)");
    }
    return ret;
}

long medTrDriverUpdate(CVars in, CVars &out) {

    long ret = PHP_API_BAD_PARAM;
    logfmt(logINFO, "medTrDriverUpdate()");


    try {
        REQ_LONG(in, "pid", 0, -1);
        long agent_id = REQ_LONG(in, "agent_id", 0, -1);
        long driver_id = REQ_LONG(in, "driver_id", 0, -1);
        REQ_STRING(in, "lic_state", 1, 4, "(.*)");
        REQ_STRING(in, "firstname", 2, 49, "(.*)");
        REQ_STRING(in, "lastname", 2, 49, "(.*)");
        REQ_STRING(in, "lic_number", 2, 49, "(.*)"); // note same as email
        OPTIONAL(in, "login") REQ_LONG(in, "login", 0, -1);
        REQ_STRING(in, "street", 5, 49, "(.*)");
        REQ_STRING(in, "zipcode", 3, 12, "(.*)");
        OPTIONAL(in, "phone") REQ_STRING(in, "phone", 5, 23, "(.*)");
        REQ_STRING(in, "city", 5, 49, "(.*)");
        REQ_STRING(in, "state", 1, 4, "(.*)");


        //REQ_STRING (in, "sessionid",  4, 40, "(.*)");
        const char * loc = getenv("REMOTE_ADDR");



        ret = load_db_record(out, "SELECT *,id AS driver_id FROM transport_drivers WHERE id = %lu AND agent_id=%lu ", driver_id, in["agent_id"].Long());
        logfmt(logINFO, "~~~~ **** medTrCreateAgent(CVars in, CVars &out) ret==>%d", ret);

        if (ret > 0 && driver_id > 0) {

            // LETS CREATE THE REQUEST NOW
            CVars x;
            x["pid"] = in["pid"];
            x["pid"].set_valid(true);
            x["lic_number"] = in["lic_number"];
            x["lic_number"].set_valid(true);

            x["email"] = in["email"];
            x["email"].set_valid(true);
            x["firstname"] = in["firstname"];
            x["firstname"].set_valid(true);
            x["lastname"] = in["lastname"];
            x["lastname"].set_valid(true);
            x["street"] = in["street"];
            x["street"].set_valid(true);
            x["expr_month"] = in["expr_month"];
            x["expr_month"].set_valid(true);

            x["expr_year"] = in["expr_year"];
            x["expr_year"].set_valid(true);

            x["lic_state"] = in["lic_state"];
            x["lic_state"].set_valid(true);

            x["phone"] = in["phone"];
            x["phone"].set_valid(true);

            x["zipcode"] = in["zipcode"];
            x["zipcode"].set_valid(true);
            x["phone"] = in["phone"];
            x["phone"].set_valid(true);
            x["city"] = in["city"];
            x["city"].set_valid(true);

            x["state"] = in["state"];
            x["state"].set_valid(true);

            x["loc"] = loc;
            x["loc"].set_valid(true);
            x["country"] = "US";
            x["country"].set_valid(true);

            update_db_record(DBS_VALID, "transport_drivers", x, driver_id);


            if (driver_id > 0) {

                load_db_record(out, "SELECT *,id AS driver_id FROM transport_drivers WHERE id = %lu  ", driver_id);
                // ==============
                ret = PHP_API_OK;
            }

        }

    } catch (bad_parameter) {
        logfmt(logINFO, "ERROR CALL long medTrDriverUpdate(CVars in, CVars &out)");
    }


    logfmt(logINFO, "/medTrDriverUpdate()");
    return ret;

}

long medTrDriverList(CVars in, CVars &out) {

    long ret = PHP_API_BAD_PARAM;
    char vname[20];
    try {

        REQ_LONG(in, "agent_id", 0, -1);
        long mode = REQ_LONG(in, "mode", 0, -1);
        long driver_id = REQ_LONG(in, "driver_id", 0, -1);

        const PGresult *res;
        out["total_record"] = "0";
        out["mode"] = in["mode"]; // send this back
        if (driver_id > 0) {
            res = pgsql_query("SELECT id as driver_id, * FROM transport_drivers WHERE agent_id = %lu AND id  = %lu", in["agent_id"].Long(), driver_id);

        } else {
            res = pgsql_query("SELECT id as driver_id, * FROM transport_drivers WHERE agent_id = %lu ORDER BY id DESC LIMIT 250", in["agent_id"].Long());
        }
        // const PGresult *res = pgsql_query("SELECT * FROM interpreters WHERE agent_id >0 ORDER BY id DESC LIMIT %lu", in["limit"].Long());
        if (res != NULL && pgsql_num_rows(res) > 0) {
            out["total_record"] = pgsql_num_rows(res);

            for (int i = 0, n = pgsql_num_rows(res); i < n; i++) {
                map<const char*, const char*>f = pgsql_fetch_assoc(res, i);
                if (f.empty()) continue;
                CVars rec;
                map_to_cvars(f, rec);

                snprintf(vname, sizeof (vname), "firstname_%05d", i);
                out[vname] = rec["firstname"];
                snprintf(vname, sizeof (vname), "lastname_%05d", i);
                out[vname] = rec["lastname"];


                snprintf(vname, sizeof (vname), "id_%05d", i);
                out[vname] = rec["id"];
                snprintf(vname, sizeof (vname), "driver_id_%05d", i);
                out[vname] = rec["driver_id"];
                snprintf(vname, sizeof (vname), "street_%05d", i);
                out[vname] = rec["street"];
                snprintf(vname, sizeof (vname), "city_%05d", i);
                out[vname] = rec["city"];
                snprintf(vname, sizeof (vname), "zipcode_%05d", i);
                out[vname] = rec["zipcode"];
                snprintf(vname, sizeof (vname), "state_%05d", i);
                out[vname] = rec["state"];

                snprintf(vname, sizeof (vname), "agent_id_%05d", i);
                out[vname] = rec["agent_id"];

                snprintf(vname, sizeof (vname), "lic_state_%05d", i);
                out[vname] = rec["lic_state"];

                snprintf(vname, sizeof (vname), "lic_number_%05d", i);
                out[vname] = rec["lic_number"];

                snprintf(vname, sizeof (vname), "phone_%05d", i);
                out[vname] = rec["phone"];

                snprintf(vname, sizeof (vname), "expr_month_%05d", i);
                out[vname] = rec["expr_month"];

                snprintf(vname, sizeof (vname), "expr_year_%05d", i);
                out[vname] = rec["expr_year"];

                snprintf(vname, sizeof (vname), "email_%05d", i);
                out[vname] = rec["email"];




            }
        }
        ret = PHP_API_OK;
        out["status"] = "OK";



    } catch (bad_parameter) {
        logfmt(logINFO, "ERROR CALL long medTrDriverList(CVars in, CVars &out)");
    }
    return ret;



}

long medTrCreateDriver(CVars in, CVars &out) {
    long ret = PHP_API_BAD_PARAM;
    logfmt(logINFO, "medTrCreateDriver()");


    try {
        REQ_LONG(in, "pid", 0, -1);
        long agent_id = REQ_LONG(in, "agent_id", 0, -1);
        REQ_STRING(in, "lic_state", 1, 4, "(.*)");
        REQ_STRING(in, "firstname", 2, 49, "(.*)");
        REQ_STRING(in, "lastname", 2, 49, "(.*)");
        REQ_STRING(in, "lic_number", 2, 49, "(.*)"); // note same as email
        OPTIONAL(in, "login") REQ_LONG(in, "login", 0, -1);
        REQ_STRING(in, "street", 5, 49, "(.*)");
        REQ_STRING(in, "zipcode", 3, 12, "(.*)");
        OPTIONAL(in, "phone") REQ_STRING(in, "phone", 5, 23, "(.*)");
        REQ_STRING(in, "city", 5, 49, "(.*)");
        REQ_STRING(in, "state", 1, 4, "(.*)");


        //REQ_STRING (in, "sessionid",  4, 40, "(.*)");
        const char * loc = getenv("REMOTE_ADDR");



        ret = load_db_record(out, "SELECT *,id AS agent_id FROM agents WHERE id = %lu  ", in["agent_id"].Long());
        logfmt(logINFO, "~~~~ **** medTrCreateAgent(CVars in, CVars &out) ret==>%d", ret);

        if (ret > 0 && agent_id > 0) {

            // LETS CREATE THE REQUEST NOW
            CVars x;
            x["pid"] = in["pid"];
            x["pid"].set_valid(true);
            x["lic_number"] = in["lic_number"];
            x["lic_number"].set_valid(true);

            x["email"] = in["email"];
            x["email"].set_valid(true);
            x["firstname"] = in["firstname"];
            x["firstname"].set_valid(true);
            x["lastname"] = in["lastname"];
            x["lastname"].set_valid(true);
            x["street"] = in["street"];
            x["street"].set_valid(true);
            x["expr_month"] = in["expr_month"];
            x["expr_month"].set_valid(true);

            x["expr_year"] = in["expr_year"];
            x["expr_year"].set_valid(true);

            x["lic_state"] = in["lic_state"];
            x["lic_state"].set_valid(true);

            x["phone"] = in["phone"];
            x["phone"].set_valid(true);


            x["agent_id"] = in["agent_id"];
            x["agent_id"].set_valid(true);


            x["zipcode"] = in["zipcode"];
            x["zipcode"].set_valid(true);
            x["phone"] = in["phone"];
            x["phone"].set_valid(true);
            x["city"] = in["city"];
            x["city"].set_valid(true);

            x["state"] = in["state"];
            x["state"].set_valid(true);

            x["loc"] = loc;
            x["loc"].set_valid(true);
            x["country"] = "US";
            x["country"].set_valid(true);

            out["driver_id"] = insert_db_record(DBS_VALID, "transport_drivers", "transport_drivers_id_seq", x);
            if (out["driver_id"].Long() > 0) {


                // ==============
            }

        }

    } catch (bad_parameter) {
        logfmt(logINFO, "ERROR CALL long medTrCreateAgent(CVars in, CVars &out)");
    }


    logfmt(logINFO, "/medTrCreateDriver()");
    return 0;
}