
// History Listing
#include "clog.h"
#include "cgi.h"
#include "input.h"
#include "wrenchboard_api.h"
#include "history.h"
#include "market.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>

long WrenchMarketImport(CVars in, CVars &out);
long WrenchPromoLoad(CVars in, CVars &out);

long market_calls(CVars in, CVars &out) {
    logfmt(logINFO, "market_calls()");
    long ret = PHP_API_BAD_PARAM;
    char vname[700];
    char file_section[50];
        try {
        long action = REQ_LONG( in, "action", 1, -1 );
            switch(action){
                case WRENCHBOARD_MARKET_PLAN01_START:
                    return  WrenchMarketImport(in, out);
                break;

                case WRENCHBOARD_MARKET_PLAN01_NEXT1:
                     return  WrenchPromoLoad(in, out);
                break;
            }

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


long WrenchMarketImport(CVars in, CVars &out) {
logfmt(logINFO, "WrenchMarketImport()");
    char vname[30];
    long ret = PHP_API_BAD_PARAM;

    try {
         out["total_record"] = "0";
        const PGresult *res;
        res = pgsql_query("SELECT id AS  import_id,* FROM market_import WHERE status =1 ORDER BY ecount ASC LIMIT 10");

        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);
                market_email(WRENCHBOARD_MARKET_PLAN01_START, rec, rec);
                pgsql_query("UPDATE  market_import SET ecount=ecount+1 WHERE uid::text='%s'", rec["uid"].c_str());
            }
        }
        ret = PHP_API_OK;
        out["status"] = "OK";
    } catch (bad_parameter) {
        logfmt(logINFO, "ERROR CALL long WrenchMarketImport(CVars in, CVars &out)");
    }
    logfmt(logINFO, "/WrenchMarketImport()");

    return ret;
}

long WrenchPromoLoad(CVars in, CVars &out) {
     logfmt(logINFO, "WrenchPromoLoad()");
      long ret = PHP_API_BAD_PARAM;
        try {
            REQ_STRING(in, "promo_owner", 2, 149, "(.*)");
            REQ_STRING(in, "promo", 2, 49, "(.*)");

            load_db_record(out, "SELECT to_char(now(), 'yyyy') AS YYYY");

            if ( load_db_record(out, "SELECT uid AS promo_owner, firstname , lastname , middlename , status,email FROM market_import  WHERE uid::text ='%s' AND status = 1", in["promo_owner"].c_str())> 0){
                if ( load_db_record(out, "SELECT  id FROM members   WHERE LOWER(username)  = LOWER('%s')", out["email"].c_str())> 0){
                  pgsql_query("UPDATE  market_import SET status = 4 WHERE uid::text = '%s' AND status = 1 ", out["promo_owner"].c_str());
                  out["status_message"] = "VALID_LINK_NOT_FOUND";
                }
                else
                {
                   out["status_message"] = "VALID_LINK_FOUND";
                }
            }
            else{
            out["status_message"] = "VALID_LINK_NOT_FOUND";
            }

            if ( load_db_record(out, "SELECT  * FROM market_promo  WHERE promo::text ='%s' ", in["promo"].c_str())> 0){

            }
            ret = PHP_API_OK;
            out["status"] = "OK";
        } catch (bad_parameter) {
             logfmt(logINFO, "ERROR CALL long WrenchPromoLoad(CVars in, CVars &out)");
        }
    logfmt(logINFO, "/WrenchPromoLoad()");
    return ret ;
}
/*
#define WRENCHBOARD_MARKET_PLAN01_START   220001
#define WRENCHBOARD_MARKET_PLAN01_NEXT1   220003
*/