<?php
//namespace models;
//use helpers\session as Session;
//use helpers\cookie as Cookie;
//use \PDO; //uncomment for PDOStatement::fetchObjects

/**
* Users Model : The Database object
* Handles the user's display / search / id and etc.
**/
class Users_Model extends Model{
	/**
	* Constructor, expects a Database connection
	*/
	public function __construct(){
		parent::__construct();
	}

	/**
	* Returns user all parameters from user_information table
	* @param INT the id of user to look for
	* @return ARRAY or false BOOL if none found
	**/
	public function get_User($userId){
		$query = $this->_db->select("SELECT *
									FROM ".PREFIX."usr_i 
									WHERE user_id = :user_id"
									,array(':user_id' => $userId));
		// if there's NOT one result but only first row fetched (we only have one result)
		if($query){
			return $query[0];
		}else{
			return false;
		}
	}

	/** 
	* Returns all users by decreasing ID
	* @param INT current page considering with users limit per page
	* @return ARRAY of users with information or BOOL on false or none meet criteria
	**/
	public function get_Users($pageId=0,$limit=DISPLAY_USERS){
		$data['usr'] = $this->_db->select("SELECT SQL_CALC_FOUND_ROWS * 
								FROM ".PREFIX."usr_i
								ORDER BY ".PREFIX."usr_i.user_id DESC LIMIT ".$limit * ($pageId)." , ".$limit." ");
		if($data){
			$data['add'] = $this->_db->select("SELECT FOUND_ROWS() AS `total_found`");
			return $data;
		}else{
			return false;
		}
	}

	/** 
	* Returns users found by search limit, the search performed will return users by decreasing ID
	* @param INT current page considering with users limit per page
	* @params MIX as optional: gender , date(age) from/to 
	* @return ARRAY of users with information or BOOL on false or none meet criteria
	**/
	public function get_Search($pageId=0,$limit=DISPLAY_USERS,$searchParams){
		if(!empty($searchParams)){
			/** This array will allow to expand dynamic search **/
			$push_array=[];
			$push_string = " WHERE 1=1 ";
			if(array_key_exists('user_gender',$searchParams)){
				$push_array[':user_gender'] = $searchParams['user_gender'];
				$push_string .= " AND user_gender = :user_gender ";
			}
			if(!empty($searchParams['user_age_from'])){
				$push_array[':user_birth_from'] = $searchParams['user_age_from'];
				$push_string .=  " AND user_birth <= :user_birth_from ";
			}
			if(!empty($searchParams['user_age_to'])){
				$push_array[':user_birth_to'] = $searchParams['user_age_to'];
				$push_string .=  " AND user_birth >= :user_birth_to ";
			}
			if(array_key_exists('user_district',$searchParams)){
				$push_array[':user_district'] = $searchParams['user_district'];
				$push_string .= " AND user_district = :user_district ";
			}
			$data['usr'] = $this->_db->select("SELECT SQL_CALC_FOUND_ROWS * 
									FROM ".PREFIX."usr_i
									 ".$push_string."
									ORDER BY ".PREFIX."usr_i.user_id DESC LIMIT ".$limit * ($pageId)." , ".$limit." "
									,$push_array);
		}else{
			$data['usr'] = $this->_db->select("SELECT SQL_CALC_FOUND_ROWS * 
									FROM ".PREFIX."usr_i
									ORDER BY ".PREFIX."usr_i.user_id DESC LIMIT ".$limit * ($pageId)." , ".$limit." ");
		}
		if($data){
			$data['add'] = $this->_db->select("SELECT FOUND_ROWS() AS `total_found`");
			return $data;
		}else{
			return false;
		}
	}

	/**
	* Get all user image paths and selected image path if such was set
	* @param INT user id 
	* @returns ARRAY with paths or BOOL false on none found
	*/
	public function get_UserImages($userId){
		$query = $this->_db->select("SELECT *
									FROM ".PREFIX."usr_img 
									WHERE user_id = :user_id"
									,array(':user_id' => $userId));
		// if there's NOT one result but only first row fetched (we only have one result)
		if($query){
			return $query;
		}else{
			return false;
		}
	}

	/** 
	* Returns conversation between two users and marks message as read
	* @param INT current (the sending) second (the receiving)
	* @return ARRAY with messages or BOOL false
	**/
	public function get_Conv($userId,$userTwo){
		$this->_db->update(PREFIX."usr_con",
							array('status' => '1'),
							array('user_one' => $userTwo,
									'user_two' => $userId));
		$query = $this->_db->select("SELECT user_one,msg,status,time
									FROM ".PREFIX."usr_con
									WHERE 
										(user_one = :user_id AND user_two = :user_two)
									OR
										(user_one = :user_two AND user_two = :user_id)
									ORDER BY con_id ASC"
									,array(':user_id' => $userId,
											':user_two' => $userTwo));
		// if there's NOT one result but only first row fetched (we only have one result)
		if ($query){
			return $query;
		}else{
			return false;
		}
	}

	/**
	* Insert/add new message to conversation between users 
	* @paramt INT sending/receiving user id
	* @param STRING message content 
	* @return BOOL success/fail
	**/
	public function set_ConvMsg($sending,$receiving,$message){
		$query = $this->_db->insert(PREFIX."usr_con"
								,array('user_one' => $sending,
									'user_two' => $receiving,
									'msg' => $message));
		if($query){
			return true;
		}else{
			return false;
		}
	}

	/** 
	* Returns array of newly added users using ID's no consideration on gender or whatsoever
	* @params INT limits from/to how many users to return 
	* @return ARRAY of users or BOOL if none found, ha no user activated or exists :)
	**/
	public function get_NewlyCreatedUsers($limit_from=0,$limit_to=DISPLAY_USERS){
		$query = $this->_db->select("SELECT * FROM ".PREFIX."usr_i 
									ORDER BY ".PREFIX."usr_i.user_id DESC LIMIT $limit_from, $limit_to");
		if($query){
			return $query;
		}else{
			return false;
		}
	}

	// public function get_Conv($userId,$userTwo){
		// // get real token from database (and all other data)
		// $query = $this->_db->select("SELECT U.user_id,C.con_id
									// FROM ".PREFIX."usr_i U,".PREFIX."con C,".PREFIX."con_rep R 
									// WHERE 
									// CASE
										// WHEN C.user_one = :user_id
										// THEN C.user_two = :user_two
										// WHEN C.user_two = :user_id
										// THEN C.user_one = :user_two
									// END
									// AND
										// C.con_id=R.con_id_fk
									// AND
										// (C.user_one = :user_id OR C.user_two = :user_id)
									// ORDER BY C.con_id DESC"
									// ,array(':user_id' => $userId,
											// ':user_two' => $userTwo));
		// // if there's NOT one result but only first row fetched (we only have one result)
		// if ($query){
			// return $query;
		// }else{
			// return false;
		// }
	// }
}