// Account management toosl
#include "clog.h"
#include "cgi.h"
#include "input.h"
#include "wrenchboard_api.h"
#include "groups.h"
#include "email.h"
#include "safestring.h"
#include <string>
#include "pgsql.h"
#include "pgsql_wrapper.h"
#include "cfg.h"
#include <curl/curl.h>



long groups_calls(CVars in, CVars &out)
{
	logfmt( logINFO, "groups_calls()" );
	out["result"] = "YES I GET TO BACK END";
    long action = REQ_LONG( in, "action", 0, -1);
	switch( action )
    { 

		
		case WRENCHBOARD_GROUP_ACCEPTGROUP:
			//return LoginWrenchBoardAccount( in, out);		
		break;		
		
		case WRENCHBOARD_GROUP_INVITEGROUP:
			//return CreateWrenchBoardAccountPending(in, out);
		break;
		
		case WRENCHBOARD_GROUP_CREATEGROUP:
			return CreateWrenchBoardGroup( in, out);		
		break;
   }
	logfmt( logINFO, "/groups_calls()" );
	return 0;
}

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

	REQ_STRING (in, "group_name",   5, 99, "(.*)");
	OPTIONAL( in, "description" ) REQ_STRING (in, "description",   1, 249, "(.*)");
	REQ_LONG( in, "contribute", 1, -1 );
	REQ_LONG( in, "member_id", 1, -1 );		
	OPTIONAL( in, "loc" ) REQ_STRING (in, "loc",   3, 15, "(.*)");	
	
	CVars x;
	x["group_name"] = in["group_name"]; x["group_name"].set_valid( true );
	x["description"] = in["description"]; x["description"].set_valid( true );
	x["invite"] = "1"; x["invite"].set_valid( true );
	x["member_id"] = in["member_id"]; x["member_id"].set_valid( true );
	x["loc"] = in["loc"]; x["loc"].set_valid( true );
	ret = insert_db_record( DBS_VALID, "members_groups", "members_groups_id_seq", x );

	if ( ret > 0 )
	{   
         if ( in["contribute"].Long() > 0 )
		 {
		    pgsql_exec("UPDATE members_groups SET contribute=NOW() WHERE id=%lu ", ret);
		 }
     	 out["group_id"] = ret; out["group_id"].set_valid( true );
		 x["group_id"] = ret; x["group_id"].set_valid( true );
		 CVars y;
		 y["member_id"] = in["member_id"]; y["member_id"].set_valid( true );
		 y["admin_status"] = in["member_id"]; y["admin_status"].set_valid( true );
		 y["group_id"] = out["group_id"]; y["group_id"].set_valid( true );
		 y["loc"] = in["loc"]; y["loc"].set_valid( true );
		 //ALTER TABLE group_members ADD admin_status  INT REFERENCES members(id);
		 WrenchBoardGroupCreateMember(y,out);
		 CreateWrenchBoardGroupMail(y);
	}	
	return ret;
}

long WrenchBoardGroupCreateMember(CVars in, CVars &out)
{
	long ret = PHP_API_BAD_PARAM;
	out =in;	
    REQ_LONG( in, "member_id", 1, -1 );	
    REQ_LONG( in, "group_id", 1, -1 );	
	OPTIONAL( in, "loc" ) REQ_STRING (in, "loc",   3, 15, "(.*)");	
	
	CVars x;
	x["group_id"] = in["group_id"]; x["group_id"].set_valid( true );	
	x["member_id"] = in["member_id"]; x["member_id"].set_valid( true );
	x["loc"] = in["loc"]; x["loc"].set_valid( true );
	if (in["admin_status"].Long() > 0)
	{
	 x["admin_status"] = in["admin_status"]; x["admin_status"].set_valid( true );
	}
	ret = insert_db_record( DBS_VALID, "group_members", "group_members_id_seq", x );

	if ( ret > 0 )
	{    out["group_member_id"] = ret; out["group_member_id"].set_valid( true );
		 x["group_member_id"] = ret; x["group_member_id"].set_valid( true );
		 GroupCreateMemberMail(x);		 
		 pgsql_exec("UPDATE members_groups SET accepted = (SELECT count(*) FROM group_members WHERE group_id = %lu ) WHERE id=%lu",x["group_id"].Long(),x["group_id"].Long() );
	}	
	return ret;
}
//******************************************************************************


	/*
CREATE TABLE group_members (
    id SERIAL,
	member_id INT REFERENCES members(id),
	group_name VARCHAR(100) UNIQUE NOT NULL,
	description VARCHAR(250),
	added timestamp without time zone DEFAULT now(),	
	balance   integer DEFAULT 0, 
	contribute  timestamp without time zone,  
	alert    integer DEFAULT 0, 
    status   integer DEFAULT 1,  
	invite    integer DEFAULT 0,
	accepted  integer DEFAULT 0,
	rejected  integer DEFAULT 0,	
    loc INET	
	);
ALTER TABLE ONLY members_groups
    ADD CONSTRAINT members_groups_id_key UNIQUE (id);

	*/

