// Twillo management toosl
#include "clog.h"
#include "cgi.h"
#include "input.h"
#include "wrenchboard_api.h"
#include "reco_engine.h"  //error in file name
#include "email.h"
#include "safestring.h"
#include <string>
#include "pgsql.h"
#include "pgsql_wrapper.h"
#include <curl/curl.h>
#include "account.h"
#include "cards.h"
#include "twilo.h"
#include "mobile.h"

/*
#define RECOMMEND_COUPON     1000
#define RECOMMEND_BLOG       1005
#define RECOMMEND_NEWTASK    1010
#define RECOMMEND_TOKEN      1015
#define RECOMMEND_OFFERS     1020
//========================
*/
long sendOffersRecommendation( CVars in, CVars &out);
long sendCouponRecommendation( CVars in, CVars &out);

long RecoCheckOffers( int itm_count, CVars in, CVars &out  );
long RecoCheckFamilyLogin( int itm_count, CVars in, CVars &out  );
long RecoCheckFamilyCount( int itm_count, CVars in, CVars &out  );
long RecoCheckCoupons( int itm_count, CVars in, CVars &out  );
long RecoCheckTaskDue( int itm_count, CVars in, CVars &out  );

long RecoCheckActiveTask( int itm_count, CVars in, CVars &out  );

long RecoCheckTaskReview( int itm_count, CVars in, CVars &out  );
long RecoCheckBlog( int itm_count, CVars in, CVars &out  );
long RecoOffersInterest( int itm_count, CVars in, CVars &out  );
long RecoReferAFreind(int itm_count, CVars in, CVars &out );
long RecoPendingInterestCount( int itm_count, CVars in, CVars &out  );
long RecoStartMarketCard( int itm_count, CVars in, CVars &out  );
long RecoStartMarketCardUS( int itm_count, CVars in, CVars &out  );

long RecoLearnMore( int itm_count, CVars in, CVars &out  );
long RecoJobOwner( int itm_count, CVars in, CVars &out  );
long RecoFamilyWaiting( int itm_count, CVars in, CVars &out  );
long RecoRecentPastDueTask( int itm_count, CVars in, CVars &out  );
long RecoPlayGround( int itm_count, CVars in, CVars &out  );

// family banners
long RecoCheckFamilyOffers( int itm_count, CVars in, CVars &out  );
long RecoCheckFamilyActive( int itm_count, CVars in, CVars &out  );
long RecoFamilyPastDueTask( int itm_count, CVars in, CVars &out  );
long RecoFamilyKidsWaiting( int itm_count, CVars in, CVars &out  );
long RecoRecomendToParent( int itm_count, CVars in, CVars &out  );

long recommendation_engine(long action , CVars in, CVars &out) {
    try{

        switch (action) {
            case RECOMMEND_COUPON:
             return  sendCouponRecommendation( in, out);
            break;

            case RECOMMEND_BLOG:
             return 0;
            break;

            case RECOMMEND_NEWTASK:
             return 0;
            break;

            case RECOMMEND_TOKEN:
             return 0;
            break;

            case RECOMMEND_OFFERS:
             return  sendOffersRecommendation( in, out);
            break;
        }

    } catch (bad_parameter) {
        logfmt(logINFO, "ERROR CALL long recommendation_engine");
    }
return 0;
}

long WrenchFamilyHomeBanners(CVars in, CVars &out){

  out["status"]  = "long WrenchFamilyHomeBanners(CVars in, CVars &out)";
  long ret = PHP_API_BAD_PARAM;
  CVars offV;
  char vname[30];
            long member_id = REQ_LONG(in, "member_id", 1, -1);
            int itm_count= 0;
            if(  RecoCheckFamilyOffers(  itm_count,  in,  out  ) !=  PHP_API_BAD_PARAM ) { itm_count++; }  /*ONLY IF OFFER IS GREATER THAN 2 */
            if(  RecoCheckFamilyActive(  itm_count,  in,  out  ) !=  PHP_API_BAD_PARAM ) { itm_count++; }
            if(  RecoFamilyPastDueTask(  itm_count,  in,  out  ) !=  PHP_API_BAD_PARAM ) { itm_count++; }
            if(  RecoFamilyKidsWaiting(  itm_count,  in,  out  ) !=  PHP_API_BAD_PARAM ) { itm_count++; }
            if(  RecoRecomendToParent(  itm_count,  in,  out  ) !=  PHP_API_BAD_PARAM ) { itm_count++; }


            if(itm_count < 3){
//                if(  RecoLearnMore(  itm_count,  in,  out  ) !=  PHP_API_BAD_PARAM ) { itm_count++; }
                if(  RecoPlayGround(  itm_count,  in,  out  ) !=  PHP_API_BAD_PARAM ) { itm_count++; }
//                  if(  RecoReferAFreind(  itm_count,  in,  out  ) !=  PHP_API_BAD_PARAM ) { itm_count++; }
//                if(  RecoCheckBlog(  itm_count,  in,  out  ) !=  PHP_API_BAD_PARAM ) { itm_count++; }
            }

    out["total_record"] = itm_count;
    logfmt(logINFO, "/WrenchFamilyHomeBanners()");
    ret = PHP_API_OK;
	return ret;
}

long RecoCheckFamilyOffers( int itm_count, CVars in, CVars &out  ){
   logfmt(logINFO, "RecoCheckFamilyOffers()");
    long ret = PHP_API_BAD_PARAM;
    const PGresult *res;
    char vname[30];
    try {
            int ic = itm_count;
             long member_id = REQ_LONG(in, "member_id", 1, -1);
    		res = pgsql_query("SELECT * FROM members_jobs_offer WHERE expire > now() AND status = 1 AND client_id > 0 AND client_id = %lu ",member_id);
    		if (res != NULL && pgsql_num_rows(res) > 2) {

                    snprintf(vname, sizeof (vname), "title_%05d", ic);
                    out[vname] = "Ready to Start ?";

                    snprintf(vname, sizeof (vname), "description_%05d", ic);
                    out[vname] = "You have received some offers. View, accept, or reject before the offer(s)  expire.";

                    snprintf(vname, sizeof (vname), "card_type_%05d", ic);
                    out[vname] = "OFFERS";

                    snprintf(vname, sizeof (vname), "offer_id_%05d", ic);
                    out[vname] = "0";

                    snprintf(vname, sizeof (vname), "blog_id_%05d", ic);
                    out[vname] = "0";

                    snprintf(vname, sizeof (vname), "card_icon_%05d", ic);
                    out[vname] = "icon1 imp_offers";

                    snprintf(vname, sizeof (vname), "banner_%05d", ic);
                    //out[vname] ="banner-waiting.jpg";
                    out[vname] ="https://www.wrenchboard.com/assets/images/apps/banners/kids/banner-start.jpg";

                    snprintf(vname, sizeof (vname), "banner_location_%05d", ic);
                    out[vname] ="URL";

                    snprintf(vname, sizeof (vname), "link_path_%05d", ic);
                    out[vname] = "pending";

                    snprintf(vname, sizeof (vname), "action_%05d", ic);
                    out[vname] = "pending";

                    snprintf(vname, sizeof (vname), "button_text_%05d", ic);
                    out[vname] = "Continue";

                    snprintf(vname, sizeof (vname), "short_button_text_%05d", ic);
                    out[vname] = "View now";

                    snprintf(vname, sizeof (vname), "short_title_%05d", ic);
                    out[vname] = "Ready to Start ?";

                    snprintf(vname, sizeof (vname), "short_description_%05d", ic);
                    out[vname] = " View, accept, or reject before the offer(s) expire";

                    snprintf(vname, sizeof (vname), "short_style_%05d", ic);
                    out[vname] = "short_style";

                ret = PHP_API_OK;
    		}


    } catch (bad_parameter) {
        out["status"] = "ERROR";
    }
    logfmt(logINFO, "/RecoCheckFamilyOffers()");
	return ret;
}
long RecoCheckFamilyActive( int itm_count, CVars in, CVars &out  ){
       logfmt(logINFO, "RecoCheckFamilyActive()");
         long ret = PHP_API_BAD_PARAM;
         const PGresult *res;
         char vname[30];
         try {
           int ic = itm_count;
           long member_id = REQ_LONG(in, "member_id", 1, -1);
                 res = pgsql_query("SELECT * FROM members_jobs_contract WHERE client_id = %lu AND status =1  AND delivery_date > ( now() - interval '1440 minutes' ) ",member_id);
                 if (res != NULL && pgsql_num_rows(res) > 0) {

                         snprintf(vname, sizeof (vname), "title_%05d", ic);
                         out[vname] = "Current Activities";

                         snprintf(vname, sizeof (vname), "description_%05d", ic);
                         out[vname] = "View you current task(s) ";

                         snprintf(vname, sizeof (vname), "card_type_%05d", ic);
                         out[vname] = "ACTIVEJOB";

                         snprintf(vname, sizeof (vname), "offer_id_%05d", ic);
                         out[vname] = "0";

                         snprintf(vname, sizeof (vname), "blog_id_%05d", ic);
                         out[vname] = "0";

                         snprintf(vname, sizeof (vname), "card_icon_%05d", ic);
                         out[vname] = "icon1";

                         snprintf(vname, sizeof (vname), "banner_%05d", ic);
                         out[vname] ="https://www.wrenchboard.com/assets/images/apps/banners/kids/banner-active.jpg";

                         snprintf(vname, sizeof (vname), "banner_location_%05d", ic);
                         out[vname] ="URL";

                         snprintf(vname, sizeof (vname), "link_path_%05d", ic);
                         out[vname] = "mytask"; //Active Task Page //"my-active-jobs";

                        snprintf(vname, sizeof (vname), "action_%05d", ic);
                        out[vname] = "mytask";


                         snprintf(vname, sizeof (vname), "button_text_%05d", ic);
                         out[vname] = "View Tasks";

                         snprintf(vname, sizeof (vname), "short_button_text_%05d", ic);
                         out[vname] = "View";

                         snprintf(vname, sizeof (vname), "short_title_%05d", ic);
                         out[vname] = "Current Activities";

                         snprintf(vname, sizeof (vname), "short_description_%05d", ic);
                         out[vname] = "View you current task(s) ";

                         snprintf(vname, sizeof (vname), "short_style_%05d", ic);
                         out[vname] = "short_style lr";

                     ret = PHP_API_OK;
                 }
         } catch (bad_parameter) {
             out["status"] = "ERROR";
         }
         logfmt(logINFO, "/RecoCheckFamilyActive()");
      return ret;
 }

/*
Family past due tasks
*/
long RecoFamilyPastDueTask( int itm_count, CVars in, CVars &out  ){
         logfmt(logINFO, "RecoFamilyPastDueTask()");
         long ret = PHP_API_BAD_PARAM;
         const PGresult *res;
         char vname[30];
         try {
           int ic = itm_count;
           long member_id = REQ_LONG(in, "member_id", 1, -1);
                 res = pgsql_query("SELECT * FROM members_jobs_contract WHERE client_id = %lu AND status =1  AND delivery_date< now() AND delivery_date >  ( now() - interval '2880 minutes' ) ",member_id);
                 if (res != NULL && pgsql_num_rows(res) > 0) {

                         snprintf(vname, sizeof (vname), "title_%05d", ic);
                         out[vname] = "Past Due Tasks";

                         snprintf(vname, sizeof (vname), "description_%05d", ic);
                         out[vname] = "Request an extension before the task gets canceled.";

                         snprintf(vname, sizeof (vname), "card_type_%05d", ic);
                         out[vname] = "PASTDUEJOB";

                         snprintf(vname, sizeof (vname), "offer_id_%05d", ic);
                         out[vname] = "0";

                         snprintf(vname, sizeof (vname), "blog_id_%05d", ic);
                         out[vname] = "0";

                         snprintf(vname, sizeof (vname), "card_icon_%05d", ic);
                         out[vname] = "icon1";

                         snprintf(vname, sizeof (vname), "banner_%05d", ic);
                         out[vname] ="https://www.wrenchboard.com/assets/images/apps/banners/kids/banner-pastdue.jpg";

                         snprintf(vname, sizeof (vname), "banner_location_%05d", ic);
                         out[vname] ="URL";

                         snprintf(vname, sizeof (vname), "link_path_%05d", ic);
                         out[vname] = "pastdue"; //Active Task Page //"my-active-jobs";

                        snprintf(vname, sizeof (vname), "action_%05d", ic);
                        out[vname] = "pastdue";

                         snprintf(vname, sizeof (vname), "button_text_%05d", ic);
                         out[vname] = "View Tasks";

                         snprintf(vname, sizeof (vname), "short_button_text_%05d", ic);
                         out[vname] = "View";

                         snprintf(vname, sizeof (vname), "short_title_%05d", ic);
                         out[vname] = "Past Due Tasks";

                         snprintf(vname, sizeof (vname), "short_description_%05d", ic);
                         out[vname] = "You have some past due task(s) ";

                         snprintf(vname, sizeof (vname), "short_style_%05d", ic);
                         out[vname] = "short_style lr";

                     ret = PHP_API_OK;
                 }
         } catch (bad_parameter) {
             out["status"] = "ERROR";
         }
         logfmt(logINFO, "/RecoFamilyPastDueTask()");
 return ret;
 }

/*
Task sent to parent , waiting to start
*/
long RecoFamilyKidsWaiting( int itm_count, CVars in, CVars &out  ){
    logfmt(logINFO, "RecoFamilyKidsWaiting()");
    long ret = PHP_API_BAD_PARAM;
    const PGresult *res;
    char vname[30];
    try {
            int ic = itm_count;
             long member_id = REQ_LONG(in, "member_id", 1, -1);
    		res = pgsql_query("SELECT * FROM members_family_suggesttask WHERE member_id = %lu AND status = 1 LIMIT 5", member_id);
    		if (res != NULL && pgsql_num_rows(res) > 0) {

                    snprintf(vname, sizeof (vname), "title_%05d", ic);
                    out[vname] = "Waiting for Parent";

                    snprintf(vname, sizeof (vname), "description_%05d", ic);
                    out[vname] = "Waiting for parent to get started.";

                    snprintf(vname, sizeof (vname), "card_type_%05d", ic);
                    out[vname] = "KIDS_SUGGEST";

                    snprintf(vname, sizeof (vname), "offer_id_%05d", ic);
                    out[vname] = "0";

                    snprintf(vname, sizeof (vname), "blog_id_%05d", ic);
                    out[vname] = "0";

                    snprintf(vname, sizeof (vname), "card_icon_%05d", ic);
                    out[vname] = "icon1 imp_offers";

                    snprintf(vname, sizeof (vname), "banner_%05d", ic);
                    out[vname] ="https://www.wrenchboard.com/assets/images/apps/banners/kids/banner-suggested.jpg";

                    snprintf(vname, sizeof (vname), "banner_location_%05d", ic);
                    out[vname] ="URL";

                    snprintf(vname, sizeof (vname), "link_path_%05d", ic);
                    out[vname] = "parentwaiting";

                    snprintf(vname, sizeof (vname), "action_%05d", ic);
                    out[vname] = "parentwaiting";

                    snprintf(vname, sizeof (vname), "button_text_%05d", ic);
                    out[vname] = "Continue";

                    snprintf(vname, sizeof (vname), "short_button_text_%05d", ic);
                    out[vname] = "View";

                    snprintf(vname, sizeof (vname), "short_title_%05d", ic);
                    out[vname] = "Waiting for Parent.";

                    snprintf(vname, sizeof (vname), "short_description_%05d", ic);
                    out[vname] = "Waiting for parent to get started.";

                    snprintf(vname, sizeof (vname), "short_style_%05d", ic);
                    out[vname] = "short_style";

                ret = PHP_API_OK;
    		}


    } catch (bad_parameter) {
        out["status"] = "ERROR";
    }
    logfmt(logINFO, "/RecoFamilyKidsWaiting()");
  return ret;
}

/*
Show the banner for the kids to recommend to parent
*/
long RecoRecomendToParent( int itm_count, CVars in, CVars &out  ){

    logfmt(logINFO, "RecoRecomendToParent()");
    long ret = PHP_API_BAD_PARAM;
    const PGresult *res;
    char vname[30];
    try {
                    int ic = itm_count;

                    snprintf(vname, sizeof (vname), "title_%05d", ic);
                    out[vname] = "Recommend to Parent";

                    snprintf(vname, sizeof (vname), "description_%05d", ic);
                    out[vname] = "Send to parent to approve";

                    snprintf(vname, sizeof (vname), "card_type_%05d", ic);
                    out[vname] = "LEARN_MORE";

                    snprintf(vname, sizeof (vname), "offer_id_%05d", ic);
                    out[vname] = "0";

                    snprintf(vname, sizeof (vname), "blog_id_%05d", ic);
                    out[vname] = "0";

                    snprintf(vname, sizeof (vname), "card_icon_%05d", ic);
                    out[vname] = "icon1 imp_offers";

                    snprintf(vname, sizeof (vname), "banner_%05d", ic);
                    out[vname] ="https://www.wrenchboard.com/assets/images/apps/banners/kids/banner-recommend.jpg";

                    snprintf(vname, sizeof (vname), "banner_location_%05d", ic);
                    out[vname] ="URL";

                    snprintf(vname, sizeof (vname), "link_path_%05d", ic);
                    out[vname] = "familymarket";

                    snprintf(vname, sizeof (vname), "action_%05d", ic);
                    out[vname] = "familymarket";


                    snprintf(vname, sizeof (vname), "button_text_%05d", ic);
                    out[vname] = "View";

                    snprintf(vname, sizeof (vname), "short_button_text_%05d", ic);
                    out[vname] = "View";

                    snprintf(vname, sizeof (vname), "short_title_%05d", ic);
                    out[vname] = "Recommend to Parent";

                    snprintf(vname, sizeof (vname), "short_description_%05d", ic);
                    out[vname] = "Suggest Task to the Parents";

                    snprintf(vname, sizeof (vname), "short_style_%05d", ic);
                    out[vname] = "short_style";

                ret = PHP_API_OK;

    } catch (bad_parameter) {
        out["status"] = "ERROR";
    }
    logfmt(logINFO, "/RecoRecomendToParent()");
  return ret;
   return 0;
}



//long RecoStartMarketCard( int itm_count, CVars in, CVars &out  );
//long RecoStartMarketCardUS( int itm_count, CVars in, CVars &out  );

// also used from accounts
long WrenchHomeBanners(CVars in, CVars &out) {
  out["status"]  = "long WrenchHomeBanners(CVars in, CVars &out)";
  long ret = PHP_API_BAD_PARAM;
  CVars offV;
  char vname[30];
            long member_id = REQ_LONG(in, "member_id", 1, -1);
            int itm_count= 0;
            if(  RecoRecentPastDueTask(  itm_count,  in,  out  ) !=  PHP_API_BAD_PARAM ) { itm_count++; }
            if(  RecoFamilyWaiting(  itm_count,  in,  out  ) !=  PHP_API_BAD_PARAM ) { itm_count++; }
            if(  RecoCheckOffers(  itm_count,  in,  out  ) !=  PHP_API_BAD_PARAM ) { itm_count++; }  /*ONLY IF OFFER IS GREATER THAN 2 */
            if(  RecoCheckActiveTask(  itm_count,  in,  out  ) !=  PHP_API_BAD_PARAM ) { itm_count++; }
            if(  RecoStartMarketCardUS(  itm_count,  in,  out  ) !=  PHP_API_BAD_PARAM ) { itm_count++; }
            if(  RecoStartMarketCard(  itm_count,  in,  out  ) !=  PHP_API_BAD_PARAM ) { itm_count++; }
            if(  RecoPendingInterestCount(  itm_count,  in,  out  ) !=  PHP_API_BAD_PARAM ) { itm_count++; }
            if(  RecoCheckFamilyLogin(  itm_count,  in,  out  ) !=  PHP_API_BAD_PARAM ) { itm_count++; }
            if(  RecoCheckFamilyCount(  itm_count,  in,  out  ) !=  PHP_API_BAD_PARAM ) { itm_count++; }

             if(itm_count < 3){  // if you dont have 3 cards  let ckeck
              // active task
           //   if(  RecoCheckActiveTask(  itm_count,  in,  out  ) !=  PHP_API_BAD_PARAM ) { itm_count++; }
              // reward family

              }

            if(  RecoCheckCoupons(  itm_count,  in,  out  ) !=  PHP_API_BAD_PARAM ) { itm_count++; }
            if(  RecoCheckTaskReview(  itm_count,  in,  out  ) !=  PHP_API_BAD_PARAM ) { itm_count++; }
            if(  RecoCheckTaskDue(  itm_count,  in,  out  ) !=  PHP_API_BAD_PARAM ) { itm_count++; } // - temporary for test
            if(  RecoOffersInterest(  itm_count,  in,  out  ) !=  PHP_API_BAD_PARAM ) { itm_count++; }




            if(itm_count < 3){
                if(  RecoLearnMore(  itm_count,  in,  out  ) !=  PHP_API_BAD_PARAM ) { itm_count++; }
                if(  RecoPlayGround(  itm_count,  in,  out  ) !=  PHP_API_BAD_PARAM ) { itm_count++; }
                if(  RecoReferAFreind(  itm_count,  in,  out  ) !=  PHP_API_BAD_PARAM ) { itm_count++; }
                if(  RecoCheckBlog(  itm_count,  in,  out  ) !=  PHP_API_BAD_PARAM ) { itm_count++; }
            }

            out["home_dash_type"]="DEFAULT_HOME_DASH";
            LoginWrenchBoardSetDashType(in, out);
/*
DEFAULT_HOME_DASH
FAMILY_PARENT_DASH
WORKER_HOME_DASH
JOBOWNER_HOME_DASH

*/
//
//                    snprintf(vname, sizeof (vname), "button_text_%05d", ic);
//                    out[vname] = "Continue";
//
//                    snprintf(vname, sizeof (vname), "short_button_text_%05d", ic);
//                    out[vname] = "View now";
//
//                    snprintf(vname, sizeof (vname), "short_title_%05d", ic);
//                    out[vname] = "Interest Pending";
//
//                    snprintf(vname, sizeof (vname), "short_description_%05d", ic);
//                    out[vname] = "Some users are interested in your job(s)";
//
//                    snprintf(vname, sizeof (vname), "short_style_%05d", ic);
//                    out[vname] = "short_style ly";
//
//
//    		}
//
//
//    } catch (bad_parameter) {
//        out["status"] = "ERROR";
//    }
    out["total_record"] = itm_count;
    logfmt(logINFO, "/WrenchHomeBanners()");
    ret = PHP_API_OK;
	return ret;
}

/*
family_uid::text IN (SELECT uid::text FROM members_family  WHERE member_id = %lu )
*/
long RecoLearnMore( int itm_count, CVars in, CVars &out  ){
    logfmt(logINFO, "RecoLearnMore()");
    long ret = PHP_API_BAD_PARAM;
    const PGresult *res;
    char vname[30];
    try {
                    int ic = itm_count;

                    snprintf(vname, sizeof (vname), "title_%05d", ic);
                    out[vname] = "Learn more";

                    snprintf(vname, sizeof (vname), "description_%05d", ic);
                    out[vname] = "Need more information how it works ?";

                    snprintf(vname, sizeof (vname), "card_type_%05d", ic);
                    out[vname] = "LEARN_MORE";

                    snprintf(vname, sizeof (vname), "offer_id_%05d", ic);
                    out[vname] = "0";

                    snprintf(vname, sizeof (vname), "blog_id_%05d", ic);
                    out[vname] = "0";

                    snprintf(vname, sizeof (vname), "card_icon_%05d", ic);
                    out[vname] = "icon1 imp_offers";

                    snprintf(vname, sizeof (vname), "banner_%05d", ic);
                    out[vname] ="https://www.wrenchboard.com/assets/images/apps/banners/learnmore.jpg";

                    snprintf(vname, sizeof (vname), "banner_location_%05d", ic);
                    out[vname] ="URL";

                    snprintf(vname, sizeof (vname), "link_path_%05d", ic);
                    out[vname] = "learnmore";

                    snprintf(vname, sizeof (vname), "button_text_%05d", ic);
                    out[vname] = "View";

                    snprintf(vname, sizeof (vname), "short_button_text_%05d", ic);
                    out[vname] = "View";

                    snprintf(vname, sizeof (vname), "short_title_%05d", ic);
                    out[vname] = "Learn more";

                    snprintf(vname, sizeof (vname), "short_description_%05d", ic);
                    out[vname] = "Need more information how it works ?";

                    snprintf(vname, sizeof (vname), "short_style_%05d", ic);
                    out[vname] = "short_style";

                ret = PHP_API_OK;



    } catch (bad_parameter) {
        out["status"] = "ERROR";
    }
    logfmt(logINFO, "/RecoLearnMore()");
  return ret;
}

long RecoFamilyWaiting( int itm_count, CVars in, CVars &out  ){
    logfmt(logINFO, "RecoFamilyWaiting()");
    long ret = PHP_API_BAD_PARAM;
    const PGresult *res;
    char vname[30];
    try {
            int ic = itm_count;
             long member_id = REQ_LONG(in, "member_id", 1, -1);
    		res = pgsql_query("SELECT * FROM members_family_suggesttask WHERE family_uid::text IN (SELECT uid::text FROM members_family  WHERE member_id = %lu ) AND status IN ( 1,2,3,4,5)  ", member_id);
    		if (res != NULL && pgsql_num_rows(res) > 0) {

                    snprintf(vname, sizeof (vname), "title_%05d", ic);
                    out[vname] = "The family is waiting";

                    snprintf(vname, sizeof (vname), "description_%05d", ic);
                    out[vname] = "Your kids have recommended some tasks and await your approval to start.";

                    snprintf(vname, sizeof (vname), "card_type_%05d", ic);
                    out[vname] = "KIDS_SUGGEST";

                    snprintf(vname, sizeof (vname), "offer_id_%05d", ic);
                    out[vname] = "0";

                    snprintf(vname, sizeof (vname), "blog_id_%05d", ic);
                    out[vname] = "0";

                    snprintf(vname, sizeof (vname), "card_icon_%05d", ic);
                    out[vname] = "icon1 imp_offers";

                    snprintf(vname, sizeof (vname), "banner_%05d", ic);
                    out[vname] ="https://www.wrenchboard.com/assets/images/apps/banners/family-waiting.jpg";

                    snprintf(vname, sizeof (vname), "banner_location_%05d", ic);
                    out[vname] ="URL";

                    snprintf(vname, sizeof (vname), "link_path_%05d", ic);
                    out[vname] = "acc-family/activities?tab=waiting";

                    snprintf(vname, sizeof (vname), "button_text_%05d", ic);
                    out[vname] = "Continue";

                    snprintf(vname, sizeof (vname), "short_button_text_%05d", ic);
                    out[vname] = "View";

                    snprintf(vname, sizeof (vname), "short_title_%05d", ic);
                    out[vname] = "Your family is waiting.";

                    snprintf(vname, sizeof (vname), "short_description_%05d", ic);
                    out[vname] = "Kids are awaiting your approval to start.";

                    snprintf(vname, sizeof (vname), "short_style_%05d", ic);
                    out[vname] = "short_style";

                ret = PHP_API_OK;
    		}


    } catch (bad_parameter) {
        out["status"] = "ERROR";
    }
    logfmt(logINFO, "/RecoFamilyWaiting()");
  return ret;
//return 0;
}

//long RecoLearnMore( int itm_count, CVars in, CVars &out  ){
//
//return 0;
//}

long RecoPlayGround( int itm_count, CVars in, CVars &out  ){

        logfmt(logINFO, "RecoPlayGround()");
        long ret = PHP_API_BAD_PARAM;
        const PGresult *res;
        char vname[30];
        int ic = itm_count;

                snprintf(vname, sizeof (vname), "title_%05d", ic);
                out[vname] = "Visit Playground";

                snprintf(vname, sizeof (vname), "description_%05d", ic);
                out[vname] = "Interact with our AI agent, find, upload, and share media.";

                snprintf(vname, sizeof (vname), "card_type_%05d", ic);
                out[vname] = "PLAYGROUND";

                snprintf(vname, sizeof (vname), "offer_id_%05d", ic);
                out[vname] = "0";

                snprintf(vname, sizeof (vname), "blog_id_%05d", ic);
                out[vname] = "0";

                snprintf(vname, sizeof (vname), "card_icon_%05d", ic);
                out[vname] = "icon1";

                snprintf(vname, sizeof (vname), "banner_%05d", ic);
                out[vname] ="https://www.wrenchboard.com/assets/images/apps/banners/playground.jpg";

                snprintf(vname, sizeof (vname), "banner_location_%05d", ic);
                out[vname] ="URL";

                snprintf(vname, sizeof (vname), "link_path_%05d", ic);
                out[vname] = "resources";

                snprintf(vname, sizeof (vname), "action_%05d", ic);
                out[vname] = "resources";

                snprintf(vname, sizeof (vname), "button_text_%05d", ic);
                out[vname] = "View Tasks";

                snprintf(vname, sizeof (vname), "short_button_text_%05d", ic);
                out[vname] = "View";

                snprintf(vname, sizeof (vname), "short_title_%05d", ic);
                out[vname] = "Playground";

                snprintf(vname, sizeof (vname), "short_description_%05d", ic);
                out[vname] = "Interact with our AI agent,share media ";

                snprintf(vname, sizeof (vname), "short_style_%05d", ic);
                out[vname] = "short_style lr";

                ret = PHP_API_OK;

  return ret;
}

long RecoJobOwner( int itm_count, CVars in, CVars &out  ){

return 0;
}

long RecoRecentPastDueTask( int itm_count, CVars in, CVars &out  ){
        logfmt(logINFO, "RecoRecentPastDueTask()");
        long ret = PHP_API_BAD_PARAM;
        const PGresult *res;
        char vname[30];
        try {
          int ic = itm_count;
          long member_id = REQ_LONG(in, "member_id", 1, -1);
                res = pgsql_query("SELECT * FROM members_jobs_contract WHERE client_id = %lu AND status =1  AND delivery_date< now() AND delivery_date >  ( now() - interval '1440 minutes' ) ",member_id);
                if (res != NULL && pgsql_num_rows(res) > 0) {

                        snprintf(vname, sizeof (vname), "title_%05d", ic);
                        out[vname] = "You recently past due task(s) ";

                        snprintf(vname, sizeof (vname), "description_%05d", ic);
                        out[vname] = "Request an extension of 12 hours before the task gets canceled.";

                        snprintf(vname, sizeof (vname), "card_type_%05d", ic);
                        out[vname] = "PASTDUEJOB";

                        snprintf(vname, sizeof (vname), "offer_id_%05d", ic);
                        out[vname] = "0";

                        snprintf(vname, sizeof (vname), "blog_id_%05d", ic);
                        out[vname] = "0";

                        snprintf(vname, sizeof (vname), "card_icon_%05d", ic);
                        out[vname] = "icon1";

                        snprintf(vname, sizeof (vname), "banner_%05d", ic);
                        out[vname] ="https://www.wrenchboard.com/assets/images/apps/banners/pastdue-jobs.jpg";

                        snprintf(vname, sizeof (vname), "banner_location_%05d", ic);
                        out[vname] ="URL";

                        snprintf(vname, sizeof (vname), "link_path_%05d", ic);
                        out[vname] = "mytask?tab=pastdue"; //Active Task Page //"my-active-jobs";

                        snprintf(vname, sizeof (vname), "button_text_%05d", ic);
                        out[vname] = "View Tasks";

                        snprintf(vname, sizeof (vname), "short_button_text_%05d", ic);
                        out[vname] = "View";

                        snprintf(vname, sizeof (vname), "short_title_%05d", ic);
                        out[vname] = "Past Due Tasks";

                        snprintf(vname, sizeof (vname), "short_description_%05d", ic);
                        out[vname] = "View your past due task(s) ";

                        snprintf(vname, sizeof (vname), "short_style_%05d", ic);
                        out[vname] = "short_style lr";

                    ret = PHP_API_OK;
                }
        } catch (bad_parameter) {
            out["status"] = "ERROR";
        }
        logfmt(logINFO, "/RecoRecentPastDueTask()");
return ret;
}

long RecoStartMarketCardUS( int itm_count, CVars in, CVars &out  ){
    logfmt(logINFO, "RecoStartMarketCardUS()");
    long ret = PHP_API_BAD_PARAM;
    const PGresult *res;
    char vname[30];
    try {
            int ic = itm_count;
             long member_id = REQ_LONG(in, "member_id", 1, -1);
    		res = pgsql_query("SELECT * FROM members WHERE uid::text = '%s' AND added::date > now() - interval '6000 minutes'  AND country='US' ", in["uid"].c_str());
    		if (res != NULL && pgsql_num_rows(res) > 0) {

                    snprintf(vname, sizeof (vname), "title_%05d", ic);
                    out[vname] = "Find new  task in the market.";

                    snprintf(vname, sizeof (vname), "description_%05d", ic);
                    out[vname] = "You have received some offers. View, accept, or reject before the offer(s)  expire.";

                    snprintf(vname, sizeof (vname), "card_type_%05d", ic);
                    out[vname] = "OFFERS";

                    snprintf(vname, sizeof (vname), "offer_id_%05d", ic);
                    out[vname] = "0";

                    snprintf(vname, sizeof (vname), "blog_id_%05d", ic);
                    out[vname] = "0";

                    snprintf(vname, sizeof (vname), "card_icon_%05d", ic);
                    out[vname] = "icon1 imp_offers";

                    snprintf(vname, sizeof (vname), "banner_%05d", ic);
                    //out[vname] ="https://www.wrenchboard.com/assets/images/apps/banners/family-login.jpg";
                    out[vname] ="https://www.wrenchboard.com/assets/images/apps/banners/dash-family.jpg";

                    snprintf(vname, sizeof (vname), "banner_location_%05d", ic);
                    out[vname] ="URL";

                    snprintf(vname, sizeof (vname), "link_path_%05d", ic);
                    out[vname] = "acc-family";

                    snprintf(vname, sizeof (vname), "button_text_%05d", ic);
                    out[vname] = "Continue";

                    snprintf(vname, sizeof (vname), "short_button_text_%05d", ic);
                    out[vname] = "View";

                    snprintf(vname, sizeof (vname), "short_title_%05d", ic);
                    out[vname] = "Find new  task in the market.";

                    snprintf(vname, sizeof (vname), "short_description_%05d", ic);
                    out[vname] = "Find new  task in the market";

                    snprintf(vname, sizeof (vname), "short_style_%05d", ic);
                    out[vname] = "short_style";

                ret = PHP_API_OK;
    		}


    } catch (bad_parameter) {
        out["status"] = "ERROR";
    }
    logfmt(logINFO, "/RecoStartMarketCardUS()");
	return ret;
}

long RecoOffersInterest(int itm_count, CVars in, CVars &out ){
    logfmt(logINFO, "RecoOffersInterest()");
    long ret = PHP_API_BAD_PARAM;
    const PGresult *res;
    char vname[30];
    try {
            int ic = itm_count;
             long member_id = REQ_LONG(in, "member_id", 1, -1);
    		res = pgsql_query(" SELECT mo.* FROm members_offer_interest mo LEFT JOIN members_jobs_offer mj ON mj.id= mo.offer_id"
    		                  " WHERE mj.member_id = %lu AND mj.expire > now()",member_id);
    		if (res != NULL && pgsql_num_rows(res) > 0) {

                    snprintf(vname, sizeof (vname), "title_%05d", ic);
                    out[vname] = "Users are interested  in some of your  job(s)";

                    snprintf(vname, sizeof (vname), "description_%05d", ic);
                    out[vname] = "Some users are interested in your job(s).";

                    snprintf(vname, sizeof (vname), "card_type_%05d", ic);
                    out[vname] = "OFFERS";

                    snprintf(vname, sizeof (vname), "offer_id_%05d", ic);
                    out[vname] = "0";

                    snprintf(vname, sizeof (vname), "blog_id_%05d", ic);
                    out[vname] = "0";

                    snprintf(vname, sizeof (vname), "card_icon_%05d", ic);
                    out[vname] = "icon1";

                    snprintf(vname, sizeof (vname), "banner_%05d", ic);
                    //out[vname] ="https://blog.wrenchboard.com/wp-content/uploads/2022/01/wrechboard-sample.jpg";
                    out[vname] ="https://www.wrenchboard.com/assets/images/apps/banners/interest-pending.jpg";

                    snprintf(vname, sizeof (vname), "banner_location_%05d", ic);
                    out[vname] ="URL";

                    snprintf(vname, sizeof (vname), "link_path_%05d", ic);
                    out[vname] = "offer-interest";

                    snprintf(vname, sizeof (vname), "button_text_%05d", ic);
                    out[vname] = "Continue";

                    snprintf(vname, sizeof (vname), "short_button_text_%05d", ic);
                    out[vname] = "View now";

                    snprintf(vname, sizeof (vname), "short_title_%05d", ic);
                    out[vname] = "Interest Pending";

                    snprintf(vname, sizeof (vname), "short_description_%05d", ic);
                    out[vname] = "Some users are interested in your job(s)";

                    snprintf(vname, sizeof (vname), "short_style_%05d", ic);
                    out[vname] = "short_style ly";

                ret = PHP_API_OK;
    		}


    } catch (bad_parameter) {
        out["status"] = "ERROR";
    }
    logfmt(logINFO, "/RecoOffersInterest()");
	return ret;
}

long RecoStartMarketCard( int itm_count, CVars in, CVars &out  ){
    logfmt(logINFO, "RecoStartMarketCard()");
    long ret = PHP_API_BAD_PARAM;
    const PGresult *res;
    char vname[30];
    try {
            int ic = itm_count;
             long member_id = REQ_LONG(in, "member_id", 1, -1);
    		res = pgsql_query("SELECT * FROM members WHERE uid::text = '%s' AND added::date > now() - interval '6000 minutes' ", in["uid"].c_str());
    		if (res != NULL && pgsql_num_rows(res) > 0) {

                    snprintf(vname, sizeof (vname), "title_%05d", ic);
                    out[vname] = "Find new  task in the market.";

                    snprintf(vname, sizeof (vname), "description_%05d", ic);
                    out[vname] = "You have received some offers. View, accept, or reject before the offer(s)  expire.";

                    snprintf(vname, sizeof (vname), "card_type_%05d", ic);
                    out[vname] = "OFFERS";

                    snprintf(vname, sizeof (vname), "offer_id_%05d", ic);
                    out[vname] = "0";

                    snprintf(vname, sizeof (vname), "blog_id_%05d", ic);
                    out[vname] = "0";

                    snprintf(vname, sizeof (vname), "card_icon_%05d", ic);
                    out[vname] = "icon1 imp_offers";

                    snprintf(vname, sizeof (vname), "banner_%05d", ic);
                    //out[vname] ="https://www.wrenchboard.com/assets/images/apps/banners/family-login.jpg";
                    out[vname] ="https://www.wrenchboard.com/assets/images/apps/banners/dash-market.jpg";

                    snprintf(vname, sizeof (vname), "banner_location_%05d", ic);
                    out[vname] ="URL";

//                    snprintf(vname, sizeof (vname), "banner_%05d", ic);
//                    out[vname] ="banner-waiting.jpg";
//
//                    snprintf(vname, sizeof (vname), "banner_location_%05d", ic);
//                    out[vname] ="LOCAL";

                    snprintf(vname, sizeof (vname), "link_path_%05d", ic);
                    out[vname] = "market";

                    snprintf(vname, sizeof (vname), "button_text_%05d", ic);
                    out[vname] = "Continue";

                    snprintf(vname, sizeof (vname), "short_button_text_%05d", ic);
                    out[vname] = "View";

                    snprintf(vname, sizeof (vname), "short_title_%05d", ic);
                    out[vname] = "Find new  task in the market.";

                    snprintf(vname, sizeof (vname), "short_description_%05d", ic);
                    out[vname] = "Find new  task in the market";

                    snprintf(vname, sizeof (vname), "short_style_%05d", ic);
                    out[vname] = "short_style";

                ret = PHP_API_OK;
    		}


    } catch (bad_parameter) {
        out["status"] = "ERROR";
    }
    logfmt(logINFO, "/RecoStartMarketCard()");
	return ret;
}
/*ONLY IF OFFER IS GREATER THAN 2 */
long RecoCheckOffers(int itm_count, CVars in, CVars &out ){
    logfmt(logINFO, "RecoCheckOffers()");
    long ret = PHP_API_BAD_PARAM;
    const PGresult *res;
    char vname[30];
    try {
            int ic = itm_count;
             long member_id = REQ_LONG(in, "member_id", 1, -1);
    		res = pgsql_query("SELECT * FROM members_jobs_offer WHERE expire > now() AND status = 1 AND client_id > 0 AND client_id = %lu ",member_id);
    		if (res != NULL && pgsql_num_rows(res) > 2) {

                    snprintf(vname, sizeof (vname), "title_%05d", ic);
                    out[vname] = "Some offers are waiting for you.";

                    snprintf(vname, sizeof (vname), "description_%05d", ic);
                    out[vname] = "You have received some offers. View, accept, or reject before the offer(s)  expire.";

                    snprintf(vname, sizeof (vname), "card_type_%05d", ic);
                    out[vname] = "OFFERS";

                    snprintf(vname, sizeof (vname), "offer_id_%05d", ic);
                    out[vname] = "0";

                    snprintf(vname, sizeof (vname), "blog_id_%05d", ic);
                    out[vname] = "0";

                    snprintf(vname, sizeof (vname), "card_icon_%05d", ic);
                    out[vname] = "icon1 imp_offers";

                    snprintf(vname, sizeof (vname), "banner_%05d", ic);
                    //out[vname] ="banner-waiting.jpg";
                    out[vname] ="https://www.wrenchboard.com/assets/images/apps/banners/banner-waiting.jpg";

                    snprintf(vname, sizeof (vname), "banner_location_%05d", ic);
                    out[vname] ="URL";

                    snprintf(vname, sizeof (vname), "link_path_%05d", ic);
                    out[vname] = "mytask";

                    snprintf(vname, sizeof (vname), "button_text_%05d", ic);
                    out[vname] = "Continue";

                    snprintf(vname, sizeof (vname), "short_button_text_%05d", ic);
                    out[vname] = "View now";

                    snprintf(vname, sizeof (vname), "short_title_%05d", ic);
                    out[vname] = "Some offers are waiting for you.";

                    snprintf(vname, sizeof (vname), "short_description_%05d", ic);
                    out[vname] = " View, accept, or reject before the offer(s) expire";

                    snprintf(vname, sizeof (vname), "short_style_%05d", ic);
                    out[vname] = "short_style";

                ret = PHP_API_OK;
    		}


    } catch (bad_parameter) {
        out["status"] = "ERROR";
    }
    logfmt(logINFO, "/RecoCheckOffers()");
	return ret;
}

long RecoCheckFamilyLogin(int itm_count, CVars in, CVars &out  ){
	//SELECT count(id) AS family_count FROM members_family WHERE member_id =1 AND status = 1  AND last_login IS NULL ;
	    logfmt(logINFO, "RecoCheckFamilyLogin()");
        long ret = PHP_API_BAD_PARAM;
        const PGresult *res;
        char vname[30];
        try {
          int ic = itm_count;
          long member_id = REQ_LONG(in, "member_id", 1, -1);
        		res = pgsql_query("SELECT id  FROM members_family WHERE member_id = %lu AND status = 1  AND last_login IS NULL ",member_id);
        		if (res != NULL && pgsql_num_rows(res) > 0) {

                        snprintf(vname, sizeof (vname), "title_%05d", ic);
                        out[vname] = "Some family accounts are yet to log in.";

                        snprintf(vname, sizeof (vname), "description_%05d", ic);
                        out[vname] = "Some family members have not logged in - a little nudge might help.";

                        snprintf(vname, sizeof (vname), "card_type_%05d", ic);
                        out[vname] = "FAMILY_NOLOGIN";

                        snprintf(vname, sizeof (vname), "offer_id_%05d", ic);
                        out[vname] = "0";

                        snprintf(vname, sizeof (vname), "blog_id_%05d", ic);
                        out[vname] = "0";

                        snprintf(vname, sizeof (vname), "card_icon_%05d", ic);
                        out[vname] = "icon1";

                        snprintf(vname, sizeof (vname), "banner_%05d", ic);
                        out[vname] ="https://www.wrenchboard.com/assets/images/apps/banners/family-login.jpg";

                        snprintf(vname, sizeof (vname), "banner_location_%05d", ic);
                        out[vname] ="URL";

                        snprintf(vname, sizeof (vname), "link_path_%05d", ic);
                        out[vname] = "acc-family";

                        snprintf(vname, sizeof (vname), "button_text_%05d", ic);
                        out[vname] = "Continue";

                        snprintf(vname, sizeof (vname), "short_button_text_%05d", ic);
                        out[vname] = "View Now";

                        snprintf(vname, sizeof (vname), "short_title_%05d", ic);
                        out[vname] = "Some family accounts are yet to log in";

                        snprintf(vname, sizeof (vname), "short_description_%05d", ic);
                        out[vname] = "Help your family member get started.";

                        snprintf(vname, sizeof (vname), "short_style_%05d", ic);
                        out[vname] = "short_style";

                    ret = PHP_API_OK;
        		}
        } catch (bad_parameter) {
            out["status"] = "ERROR";
        }
        logfmt(logINFO, "/RecoCheckFamilyLogin()");
	return ret;
}

long RecoReferAFreind(int itm_count, CVars in, CVars &out ){
    logfmt(logINFO, "RecoReferAFreind()");
    long ret = PHP_API_BAD_PARAM;
    const PGresult *res;
    char vname[30];
    try {
            int ic = itm_count;
             long member_id = REQ_LONG(in, "member_id", 1, -1);
    		res = pgsql_query(" SELECT id FROm members "
    		                  " WHERE id = %lu ",member_id);
    		if (res != NULL && pgsql_num_rows(res) > 0) {

                    snprintf(vname, sizeof (vname), "title_%05d", ic);
                    out[vname] = "Share WrenchBoard with a friend.";

                    snprintf(vname, sizeof (vname), "description_%05d", ic);
                    out[vname] = "Show WrenchBoard to a friend and earn free coupons. Get started.";

                    snprintf(vname, sizeof (vname), "card_type_%05d", ic);
                    out[vname] = "INVITE";

                    snprintf(vname, sizeof (vname), "offer_id_%05d", ic);
                    out[vname] = "0";

                    snprintf(vname, sizeof (vname), "blog_id_%05d", ic);
                    out[vname] = "0";

                    snprintf(vname, sizeof (vname), "card_icon_%05d", ic);
                    out[vname] = "icon1";

					snprintf(vname, sizeof (vname), "banner_%05d", ic);
					out[vname] ="https://www.wrenchboard.com/assets/images/apps/banners/friend-invite.jpg";

					snprintf(vname, sizeof (vname), "banner_location_%05d", ic);
					out[vname] ="URL";

                    snprintf(vname, sizeof (vname), "link_path_%05d", ic);
                    out[vname] = "referral";

                    snprintf(vname, sizeof (vname), "button_text_%05d", ic);
                    out[vname] = "Continue";

                    snprintf(vname, sizeof (vname), "short_button_text_%05d", ic);
                    out[vname] = "View now";

                    snprintf(vname, sizeof (vname), "short_title_%05d", ic);
                    out[vname] = "Invite a Friend - get Rewards";

                    snprintf(vname, sizeof (vname), "short_description_%05d", ic);
                    out[vname] = "Share WrenchBoard with a friend.";

                    snprintf(vname, sizeof (vname), "short_style_%05d", ic);
                    out[vname] = "short_style by";

                ret = PHP_API_OK;
    		}


    } catch (bad_parameter) {
        out["status"] = "ERROR";
    }
    logfmt(logINFO, "/RecoReferAFreind()");
	return ret;
}

//

long RecoPendingInterestCount( int itm_count, CVars in, CVars &out  ){
	//SELECT count(id) AS family_count FROM members_family WHERE member_id =1 AND status = 1 AND last_login IS NULL ;
		    logfmt(logINFO, "RecoPendingInterestCount()");
            long ret = PHP_API_BAD_PARAM;
            const PGresult *res;
            char vname[30];
            try {
              int ic = itm_count;
              long member_id = REQ_LONG(in, "member_id", 1, -1);
            		res = pgsql_query("SELECT moi.*,mjo.expire FROM members_offer_interest moi LEFT JOIN members_jobs_offer mjo ON mjo.id = moi.offer_id "
            		                  " LEFT JOIN members_jobs j ON j.id= mjo.job_id "
            		                  " WHERE moi.member_id = %lu AND mjo.expire > now() AND j.status= 1 AND moi.status = 1",member_id);
            		if (res != NULL && pgsql_num_rows(res) > 0) {

                            snprintf(vname, sizeof (vname), "title_%05d", ic);
                            out[vname] = "You are waiting for feedback.";

                            snprintf(vname, sizeof (vname), "description_%05d", ic);
                            out[vname] = "You have shown interest in a few tasks and are waiting for confirmation to start.";

                            snprintf(vname, sizeof (vname), "card_type_%05d", ic);
                            out[vname] = "PEND_INTEREST";

                            snprintf(vname, sizeof (vname), "offer_id_%05d", ic);
                            out[vname] = "0";

                            snprintf(vname, sizeof (vname), "blog_id_%05d", ic);
                            out[vname] = "0";

                            snprintf(vname, sizeof (vname), "card_icon_%05d", ic);
                            out[vname] = "icon1";

                            snprintf(vname, sizeof (vname), "banner_%05d", ic);
                            out[vname] ="https://www.wrenchboard.com/assets/images/apps/banners/task-waiting.jpg";

                            snprintf(vname, sizeof (vname), "banner_location_%05d", ic);
                            out[vname] ="URL";

                            snprintf(vname, sizeof (vname), "link_path_%05d", ic);
                            out[vname] = "pend-interest";

                            snprintf(vname, sizeof (vname), "button_text_%05d", ic);
                            out[vname] = "Continue";

                            snprintf(vname, sizeof (vname), "short_button_text_%05d", ic);
                            out[vname] = "Details";

                            snprintf(vname, sizeof (vname), "short_title_%05d", ic);
                            out[vname] = "You are waiting for feedback.";

                            snprintf(vname, sizeof (vname), "short_description_%05d", ic);
                            out[vname] = "You are waiting for confirmation to start.";

                            snprintf(vname, sizeof (vname), "short_style_%05d", ic);
                            out[vname] = "short_style lb";

                        ret = PHP_API_OK;
            		}
            } catch (bad_parameter) {
                out["status"] = "ERROR";
            }
            logfmt(logINFO, "/RecoPendingInterestCount()");
	return ret;
}

long RecoCheckFamilyCount( int itm_count, CVars in, CVars &out  ){
	//SELECT count(id) AS family_count FROM members_family WHERE member_id =1 AND status = 1 AND last_login IS NULL ;
		    logfmt(logINFO, "RecoCheckFamilyCount()");
            long ret = PHP_API_BAD_PARAM;
            const PGresult *res;
            char vname[30];
            try {
              int ic = itm_count;
              long member_id = REQ_LONG(in, "member_id", 1, -1);
            		res = pgsql_query("SELECT count(id) AS family_count FROM members_family WHERE member_id = %lu AND status = 1  ",member_id);
            		if (res != NULL && pgsql_num_rows(res) == 0) {

                            snprintf(vname, sizeof (vname), "title_%05d", ic);
                            out[vname] = "Activate your family account.";

                            snprintf(vname, sizeof (vname), "description_%05d", ic);
                            out[vname] = "Add Kids, family, set, manage goals, and reward them.";

                            snprintf(vname, sizeof (vname), "card_type_%05d", ic);
                            out[vname] = "FAMILY_ADD";

                            snprintf(vname, sizeof (vname), "offer_id_%05d", ic);
                            out[vname] = "0";

                            snprintf(vname, sizeof (vname), "blog_id_%05d", ic);
                            out[vname] = "0";

                            snprintf(vname, sizeof (vname), "card_icon_%05d", ic);
                            out[vname] = "icon1";

                            snprintf(vname, sizeof (vname), "banner_%05d", ic);
                            //out[vname] ="https://blog.wrenchboard.com/wp-content/uploads/2022/01/wrechboard-sample.jpg";
                            out[vname] ="https://www.wrenchboard.com/assets/images/apps/banners/family-invite.jpg";

                            snprintf(vname, sizeof (vname), "banner_location_%05d", ic);
                            out[vname] ="URL";

                            snprintf(vname, sizeof (vname), "link_path_%05d", ic);
                            out[vname] = "acc-family";

                            snprintf(vname, sizeof (vname), "button_text_%05d", ic);
                            out[vname] = "Continue";

                            snprintf(vname, sizeof (vname), "short_button_text_%05d", ic);
                            out[vname] = "View now";

                            snprintf(vname, sizeof (vname), "short_title_%05d", ic);
                            out[vname] = "Activate your family account.";

                            snprintf(vname, sizeof (vname), "short_description_%05d", ic);
                            out[vname] = "Add Kids, family, set, manage goals, and reward them. ";

                            snprintf(vname, sizeof (vname), "short_style_%05d", ic);
                            out[vname] = "short_style lb";

                        ret = PHP_API_OK;
            		}
            } catch (bad_parameter) {
                out["status"] = "ERROR";
            }
            logfmt(logINFO, "/RecoCheckFamilyCount()");
	return ret;
}

long RecoCheckCoupons( int itm_count, CVars in, CVars &out  ){
	//SELECT id AS coupons_count FROM coupons_allocation WHERE member_id =1 AND amount> 0 AND active IS NULL;
		    logfmt(logINFO, "RecoCheckCoupons()");
            long ret = PHP_API_BAD_PARAM;
            const PGresult *res;
            char vname[30];
            try {
              int ic = itm_count;
              long member_id = REQ_LONG(in, "member_id", 1, -1);
            		res = pgsql_query("SELECT id AS coupons_count FROM coupons_allocation WHERE member_id = %lu AND amount> 0 AND active IS NULL ",member_id);
            		if (res != NULL && pgsql_num_rows(res) > 0) {

                            snprintf(vname, sizeof (vname), "title_%05d", ic);
                            out[vname] = "Yo man , you have Coupons to play with";

                            snprintf(vname, sizeof (vname), "description_%05d", ic);
                            out[vname] = "You have received some cash coupons waiting to be redeemed.";

                            snprintf(vname, sizeof (vname), "card_type_%05d", ic);
                            out[vname] = "COUPONS";

                            snprintf(vname, sizeof (vname), "offer_id_%05d", ic);
                            out[vname] = "0";

                            snprintf(vname, sizeof (vname), "blog_id_%05d", ic);
                            out[vname] = "0";

                            snprintf(vname, sizeof (vname), "card_icon_%05d", ic);
                            out[vname] = "icon1";

                            snprintf(vname, sizeof (vname), "banner_%05d", ic);
                            out[vname] ="https://www.wrenchboard.com/assets/images/apps/banners/banner-coupons.jpg";

                            snprintf(vname, sizeof (vname), "banner_location_%05d", ic);
                            out[vname] ="URL";

                            snprintf(vname, sizeof (vname), "link_path_%05d", ic);
                            out[vname] = "my-coupon";

                            snprintf(vname, sizeof (vname), "button_text_%05d", ic);
                            out[vname] = "Continue";

                            snprintf(vname, sizeof (vname), "short_button_text_%05d", ic);
                            out[vname] = "Redeem";

                            snprintf(vname, sizeof (vname), "short_title_%05d", ic);
                            out[vname] = "Redeem your Cash Coupons!";

                            snprintf(vname, sizeof (vname), "short_description_%05d", ic);
                            out[vname] = "You have received some cash coupons waiting to be redeemed.";

                            snprintf(vname, sizeof (vname), "short_style_%05d", ic);
                            out[vname] = "short_style lg";

                        ret = PHP_API_OK;
            		}
            } catch (bad_parameter) {
                out["status"] = "ERROR";
            }
            logfmt(logINFO, "/RecoCheckCoupons()");
	return ret;
}


long RecoCheckActiveTask( int itm_count, CVars in, CVars &out  ){
        logfmt(logINFO, "RecoCheckActiveTask()");
        long ret = PHP_API_BAD_PARAM;
        const PGresult *res;
        char vname[30];
        try {
          int ic = itm_count;
          long member_id = REQ_LONG(in, "member_id", 1, -1);
                res = pgsql_query("SELECT * FROM members_jobs_contract WHERE client_id = %lu AND status =1  AND delivery_date > ( now() - interval '1440 minutes' ) ",member_id);
                if (res != NULL && pgsql_num_rows(res) > 0) {

                        snprintf(vname, sizeof (vname), "title_%05d", ic);
                        out[vname] = "View you current task(s) ";

                        snprintf(vname, sizeof (vname), "description_%05d", ic);
                        out[vname] = "View your current tasks, manage deliveries and updates";

                        snprintf(vname, sizeof (vname), "card_type_%05d", ic);
                        out[vname] = "ACTIVEJOB";

                        snprintf(vname, sizeof (vname), "offer_id_%05d", ic);
                        out[vname] = "0";

                        snprintf(vname, sizeof (vname), "blog_id_%05d", ic);
                        out[vname] = "0";

                        snprintf(vname, sizeof (vname), "card_icon_%05d", ic);
                        out[vname] = "icon1";

                        snprintf(vname, sizeof (vname), "banner_%05d", ic);
                        out[vname] ="https://www.wrenchboard.com/assets/images/apps/banners/active-jobs.jpg";

                        snprintf(vname, sizeof (vname), "banner_location_%05d", ic);
                        out[vname] ="URL";

                        snprintf(vname, sizeof (vname), "link_path_%05d", ic);
                        out[vname] = "mytask"; //Active Task Page //"my-active-jobs";

                        snprintf(vname, sizeof (vname), "button_text_%05d", ic);
                        out[vname] = "View Tasks";

                        snprintf(vname, sizeof (vname), "short_button_text_%05d", ic);
                        out[vname] = "View";

                        snprintf(vname, sizeof (vname), "short_title_%05d", ic);
                        out[vname] = "Current Tasks";

                        snprintf(vname, sizeof (vname), "short_description_%05d", ic);
                        out[vname] = "View you current task(s) ";

                        snprintf(vname, sizeof (vname), "short_style_%05d", ic);
                        out[vname] = "short_style lr";

                    ret = PHP_API_OK;
                }
        } catch (bad_parameter) {
            out["status"] = "ERROR";
        }
        logfmt(logINFO, "/RecoCheckActiveTask()");
return ret;
}


long RecoCheckTaskDue( int itm_count, CVars in, CVars &out  ){
        logfmt(logINFO, "RecoCheckTaskDue()");
        long ret = PHP_API_BAD_PARAM;
        const PGresult *res;
        char vname[30];
        try {
          int ic = itm_count;
          long member_id = REQ_LONG(in, "member_id", 1, -1);
                res = pgsql_query("SELECT * FROM members_jobs_contract WHERE member_id = %lu AND status =1 AND delivery_date < now() ",member_id);
                if (res != NULL && pgsql_num_rows(res) > 0) {

                        snprintf(vname, sizeof (vname), "title_%05d", ic);
                        out[vname] = "Some of your task as owner of the tasks are due ";

                        snprintf(vname, sizeof (vname), "description_%05d", ic);
                        out[vname] = "Some of your task as owner of the tasks are due, Some of your task as owner of the tasks are due";

                        snprintf(vname, sizeof (vname), "card_type_%05d", ic);
                        out[vname] = "PASTDUEJOB";

                        snprintf(vname, sizeof (vname), "offer_id_%05d", ic);
                        out[vname] = "0";

                        snprintf(vname, sizeof (vname), "blog_id_%05d", ic);
                        out[vname] = "0";

                        snprintf(vname, sizeof (vname), "card_icon_%05d", ic);
                        out[vname] = "icon1";

                        snprintf(vname, sizeof (vname), "banner_%05d", ic);
                        out[vname] ="https://www.wrenchboard.com/assets/images/apps/banners/past-due.jpg";

                        snprintf(vname, sizeof (vname), "banner_location_%05d", ic);
                        out[vname] ="URL";

                        snprintf(vname, sizeof (vname), "link_path_%05d", ic);
                        out[vname] = "my-pastdue-jobs";

                        snprintf(vname, sizeof (vname), "button_text_%05d", ic);
                        out[vname] = "View Tasks";

                        snprintf(vname, sizeof (vname), "short_button_text_%05d", ic);
                        out[vname] = "View";

                        snprintf(vname, sizeof (vname), "short_title_%05d", ic);
                        out[vname] = "Past Due Tasks";

                        snprintf(vname, sizeof (vname), "short_description_%05d", ic);
                        out[vname] = "Some of your task are now past due";

                        snprintf(vname, sizeof (vname), "short_style_%05d", ic);
                        out[vname] = "short_style lr";

                    ret = PHP_API_OK;
                }
        } catch (bad_parameter) {
            out["status"] = "ERROR";
        }
        logfmt(logINFO, "/RecoCheckTaskDue()");
return ret;
}

long RecoCheckTaskReview( int itm_count, CVars in, CVars &out  ){
        logfmt(logINFO, "RecoCheckTaskReview()");
        long ret = PHP_API_BAD_PARAM;
        const PGresult *res;
        char vname[30];
        try {
          int ic = itm_count;
          long member_id = REQ_LONG(in, "member_id", 1, -1);
                res = pgsql_query("SELECT * FROM members_jobs_contract WHERE member_id = %lu AND status = 4  ",member_id);
                if (res != NULL && pgsql_num_rows(res) > 0) {

                        snprintf(vname, sizeof (vname), "title_%05d", ic);
                        out[vname] = "Some of your task as owner Need Review now ";

                        snprintf(vname, sizeof (vname), "description_%05d", ic);
                        out[vname] = "Some of your task as owner Need Review now,Some of your task as owner Need Review now, S Need Review now";

                        snprintf(vname, sizeof (vname), "card_type_%05d", ic);
                        out[vname] = "REVIEWJOB";

                        snprintf(vname, sizeof (vname), "offer_id_%05d", ic);
                        out[vname] = "0";

                        snprintf(vname, sizeof (vname), "blog_id_%05d", ic);
                        out[vname] = "0";

                        snprintf(vname, sizeof (vname), "card_icon_%05d", ic);
                        out[vname] = "icon1";

                        snprintf(vname, sizeof (vname), "banner_%05d", ic);
                        //out[vname] ="banner-job-due.jpg";
                        out[vname] ="https://www.wrenchboard.com/assets/images/apps/banners/banner-job-due.jpg";


                        snprintf(vname, sizeof (vname), "banner_location_%05d", ic);
                        out[vname] ="URL";

                        snprintf(vname, sizeof (vname), "link_path_%05d", ic);
                        out[vname] = "my-review-jobs";

                        snprintf(vname, sizeof (vname), "button_text_%05d", ic);
                        out[vname] = "View Tasks";

                        snprintf(vname, sizeof (vname), "short_button_text_%05d", ic);
                        out[vname] = "Start Review";

                        snprintf(vname, sizeof (vname), "short_title_%05d", ic);
                        out[vname] = "Review Ready Tasks";

                        snprintf(vname, sizeof (vname), "short_description_%05d", ic);
                        out[vname] = "Some of your jobs are waiting for approval ";

                        snprintf(vname, sizeof (vname), "short_style_%05d", ic);
                        out[vname] = "short_style lr";

                    ret = PHP_API_OK;
                }
        } catch (bad_parameter) {
            out["status"] = "ERROR";
        }
        logfmt(logINFO, "/RecoCheckTaskReview()");
return ret;

}

long RecoCheckBlog( int itm_count, CVars in, CVars &out  ){
			    logfmt(logINFO, "RecoCheckBlog()");
                long ret = PHP_API_BAD_PARAM;
                const PGresult *res;
                char vname[30];
                try {
                  load_db_record( out, "SELECT title,id,banner,blog_id FROM blog_selections WHERE status = 1 ORDER BY RANDOM() LIMIT 1");
                  int ic = itm_count;
                  long member_id = REQ_LONG(in, "member_id", 1, -1);

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

                                snprintf(vname, sizeof (vname), "description_%05d", ic);
                                out[vname] =  out["title"]; //"The similarity index is a term you should be familiar with as an online writer. It is defined as the composite number that is highlighted after matching two texts to determine how similar the content from both pieces is.";

                                snprintf(vname, sizeof (vname), "card_type_%05d", ic);
                                out[vname] = "BLOG";

                                snprintf(vname, sizeof (vname), "offer_id_%05d", ic);
                                out[vname] =  out["id"];

                                snprintf(vname, sizeof (vname), "blog_id_%05d", ic);
                                out[vname] = out["blog_id"];

                                snprintf(vname, sizeof (vname), "card_icon_%05d", ic);
                                out[vname] = "icon1";

                                snprintf(vname, sizeof (vname), "banner_%05d", ic);
                                out[vname] =  out["banner"]; //"https://blog.wrenchboard.com/wp-content/uploads/2022/01/wrechboard-sample.jpg";

                                snprintf(vname, sizeof (vname), "banner_location_%05d", ic);
                                out[vname] ="URL";

                                snprintf(vname, sizeof (vname), "link_path_%05d", ic);
                                out[vname] = "blog-page";

                                snprintf(vname, sizeof (vname), "button_text_%05d", ic);
                                out[vname] = "View Article";

                                snprintf(vname, sizeof (vname), "short_button_text_%05d", ic);
                                out[vname] = "Read Now";

                                snprintf(vname, sizeof (vname), "short_title_%05d", ic);
                                out[vname] = out["title"]; //"Article - Similarity Index";

                                snprintf(vname, sizeof (vname), "short_description_%05d", ic);
                                out[vname] =out["title"]; // "Similarity index is a term you should be familiar with as an online writer";

                                snprintf(vname, sizeof (vname), "short_style_%05d", ic);
                                out[vname] = "short_style";


                            ret = PHP_API_OK;

                } catch (bad_parameter) {
                    out["status"] = "ERROR";
                }
                logfmt(logINFO, "/RecoCheckBlog()");
	return ret;
}


long sendOffersRecommendation( CVars in, CVars &out){

    try{
         long member_id = REQ_LONG(in, "member_id", 1, -1);
         REQ_LONG(in, "offer_id", 1, -1);

        if ( load_db_record(out, "SELECT client_id FROM members_jobs_offer WHERE id=%lu", in["offer_id"].Long())> 0){
                CVars xx;
                CVars xout;
                xx["action"] = WRENCHBOARD_CARDS_CREATECARD;   xx["action"].set_valid( true );
                xx["member_id"] = out["client_id"];       xx["member_id"].set_valid( true ); // the card is going to job recipient
                xx["title"] = "New job offer";     xx["title"].set_valid( true );
                xx["description"] = "You have received a new Task/Job offer";   xx["description"].set_valid( true );
                xx["card_type"] = "OFFERS";        xx["card_type"].set_valid( true );
                xx["card_style"] = "card2";        xx["card_style"].set_valid( true );
                xx["card_icon"] = "icon1";         xx["card_icon"].set_valid( true );
                xx["offer_id"] = in["offer_id"];   xx["offer_id"].set_valid( true );
                xx["blog_id"] = "0";               xx["blog_id"].set_valid( true );
                card_calls(xx, xout);
         }
    } catch (bad_parameter) {
        logfmt(logINFO, "ERROR CALL long sendCouponRecommendation");
    }
return 0;
}

long sendCouponRecommendation( CVars in, CVars &out){
/*
    CVars xx;
    xx["member_id"] = in["member_id"];  xx["member_id"].set_valid(true);
    xx["code"]      = rec["code"];      xx["code"].set_valid(true);
    xx["amount"]    = rec["amount"];    xx["amount"].set_valid(true);
    xx["status"]    = COUPON_ACTIVE;    xx["status"].set_valid(true);
    coupon_id = insert_db_record(DBS_VALID, "coupons_allocation", "coupons_allocation_id_seq", xx);

      ['title'=>'This is your coupon',
                        'description'=>'This is the description of the description',
                        'card_type'=>'COUPON',
                        'card_style' =>'card1',
                        'card_icon' =>'icon1',
                        'assign_id'=> 0,
*/

    try{
         long member_id = REQ_LONG(in, "member_id", 1, -1);
         REQ_LONG(in, "coupon_id", 1, -1);

         CVars xx;
         CVars xout;
            xx["action"] = WRENCHBOARD_CARDS_CREATECARD;   xx["action"].set_valid( true );
            xx["member_id"] = member_id;   xx["member_id"].set_valid( true );
            xx["title"] = "This is your coupon";   xx["title"].set_valid( true );
            xx["description"] = "Click to redeem your coupon to your account";   xx["description"].set_valid( true );
            xx["card_type"] = "COUPON";   xx["card_type"].set_valid( true );
            xx["card_style"] = "card1";   xx["card_style"].set_valid( true );
            xx["card_icon"] = "icon1";   xx["card_icon"].set_valid( true );
            xx["blog_id"] = "0";   xx["blog_id"].set_valid( true );
            xx["offer_id"] = "0";   xx["offer_id"].set_valid( true );
            card_calls(xx, xout);
    } catch (bad_parameter) {
        logfmt(logINFO, "ERROR CALL long sendCouponRecommendation");
    }
return 0;
}

