
// History Listing
#include "clog.h"
#include "cgi.h"
#include "input.h"
#include "wrenchboard_api.h"
#include "history.h"
#include "email.h"
#include "payments.h"
#include "safestring.h"
#include <string>
#include "pgsql.h"
#include "pgsql_wrapper.h"
#include "cfg.h"
#include <curl/curl.h>

/*
CREATE TABLE members_myfiles (
	id SERIAL,
	member_id INT REFERENCES members(id) NOT NULL,
	uid  uuid  DEFAULT uuid_generate_v4(),
	file_name VARCHAR(35),
	saved_file_name VARCHAR(35) UNIQUE NOT NULL,
	file_size INT DEFAULT 0,
	file_type VARCHAR(15),
	title VARCHAR(35),
	description VARCHAR(100),
	status INT DEFAULT 1,
    added timestamp without time zone DEFAULT now(),
    updated timestamp without time zone DEFAULT now()
	);
 ALTER TABLE ONLY members_myfiles
    ADD CONSTRAINT members_myfiles_id_key UNIQUE (id);
*/
long  WrenchMyFilesList( CVars in, CVars &out )
{
    char vname[30];
    long ret = PHP_API_BAD_PARAM;

    logfmt( logINFO, "WrenchMyFilesList()" );
    out["result"] = "Yes i go to this back end";

    try {
        long member_id = REQ_LONG( in, "member_id", 1, -1 );
        long limit = REQ_LONG( in, "limit", 1, -1 );
        REQ_LONG( in, "page", 1, -1 );
        REQ_STRING (in, "uid",   10, 100, "(.*)");

        out["total_record"] = "0";
        const PGresult *res;
        res = pgsql_query("SELECT * FROM members_myfiles "
            "WHERE member_id = %lu AND status = 1 ORDER BY id DESC LIMIT %lu",member_id,limit);

        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), "added_%05d", i);
                out[vname] = rec["added"];

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

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

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

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

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

         //       snprintf(vname, sizeof (vname), "id_%05d", i);
         //       out[vname] = rec["id"];

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

            }
        }
        ret = PHP_API_OK;
        out["status"] = "OK";
    } catch (bad_parameter) {
        logfmt(logINFO, "ERROR CALL long WrenchMyFilesList(CVars in, CVars &out)");
    }

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


long  WrenchPurchaseHx( CVars in, CVars &out )
{
    char vname[30];
    long ret = PHP_API_BAD_PARAM;

    logfmt( logINFO, "WrenchContractStatus()" );
    out["result"] = "Yes i go to this back end";

    try {
        long member_id = REQ_LONG( in, "member_id", 1, -1 );
        long limit = REQ_LONG( in, "limit", 1, -1 );
        REQ_LONG( in, "page", 1, -1 );
        REQ_STRING (in, "uid",   10, 100, "(.*)");

        out["total_record"] = "0";
        const PGresult *res;
        res = pgsql_query("SELECT id, uid,added::date AS added_date,currency,"
            " (CASE WHEN code ='MNCCD' THEN 'New Card Payment' "
            "WHEN code ='MRCCD' THEN 'Repeat Card Payment' "
            "ELSE '' END) AS description,amount*0.01 AS amount,fee*0.01 as fee,confirmation "
            "FROM members_payments "
            "WHERE member_id = %lu AND status = 1 ORDER BY id DESC LIMIT %lu",member_id,limit);

        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), "currency_%05d", i);
                out[vname] = rec["currency"];

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

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

                // amount  | id | code  | description | symbol |  action_type  | lorder | current_balance
                snprintf(vname, sizeof (vname), "amount_%05d", i);
                out[vname] = rec["amount"];

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

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

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

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

            }
        }
        ret = PHP_API_OK;
        out["status"] = "OK";
    } catch (bad_parameter) {
        logfmt(logINFO, "ERROR CALL long WrenchPurchaseHx(CVars in, CVars &out)");
    }

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

long  WrenchRefferHx( CVars in, CVars &out )
{
    char vname[30];
    long ret = PHP_API_BAD_PARAM;

    logfmt( logINFO, "WrenchRefferHx()" );
    out["result"] = "Yes i go to this back end";

    try {
        long member_id = REQ_LONG( in, "member_id", 1, -1 );
        long limit = REQ_LONG( in, "limit", 1, -1 );
        long offset = REQ_LONG( in, "offset", 1, -1 );
        REQ_STRING (in, "uid",   10, 100, "(.*)");

        const PGresult *res;
        res = pgsql_query("SELECT count(id) FROM members_refer_friend WHERE member_id = %lu", member_id);
        out["total_record"] = pgsql_num_rows(res);

         res = pgsql_query("SELECT added::date AS added_date, firstname, lastname,email,"
                            " (CASE WHEN status=1 THEN 'Pending' WHEN status=5 THEN 'Completed' ElSE '' END) As status "
                            " FROM members_refer_friend "
                            " WHERE member_id = %lu AND status > 0 ORDER BY id DESC LIMIT %lu OFFSET %lu ",member_id,limit,offset);

        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), "added_date_%05d", i);
                out[vname] = rec["added_date"];

                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), "email_%05d", i);
                out[vname] = rec["email"];

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

            }
        }
        ret = PHP_API_OK;
        out["status"] = "OK";
    } catch (bad_parameter) {
        logfmt(logINFO, "ERROR CALL long WrenchRefferHx(CVars in, CVars &out)");
    }

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

