<?php 
/**
 * Model for Notifications
 *
 * @author Mills ABeiku Godfred
 */
class Notification
{
	
	public $conn;
	
	function __construct()
	{
		$this->conn = Database::connect();
	}

    public function update_notification(array $key_col_vals, array $values) {
        $sql = "UPDATE notification SET ";
        $fields = array_keys($values);
        $vals = array_values($values);
        $key_cols = array_keys($key_col_vals);
        $key_vals = array_values($key_col_vals);
        foreach ($fields as $i=>$f) {
            $fields[$i] .= " = ? ";
        }

        $sql .= implode(",", $fields);
        $sql .= " WHERE " ;
        
        foreach ($key_cols as $i=>$f) {
            $key_cols[$i] .= " = ? ";
        }
        
        $sql .= implode(" AND ", $key_cols);

        $statement = $this->conn->prepare($sql);
        foreach ($vals as $i=>$v) {
            $statement->bindValue($i+1, $v);
        }
        
        foreach ($key_vals as $i=>$v) {
           if($i==0){
               $statement->bindValue((count($fields)+$i+1), $v);
            }else{
               $statement->bindValue((count($fields)+$i), $v);
            }
        }
        
       return  $statement->execute();

	}


	public function add_notification(array $data){
		$sql = "INSERT INTO notification ";
		$fields = array_keys($data);
		$vals = array_values($data);

		$sql .= "(".implode(",", $fields).")";
		$arr = array();
		foreach ($fields as $f) {
			$arr[] = "?";
		}
		$sql .= " VALUES(".implode(",", $arr).")";

		$statement = $this->conn->prepare($sql);

		foreach ($vals as $i => $v) {
			$statement->bindValue($i + 1, $v);
		}

		return $statement->execute();

	}
  
	
	public function read_all_notifications(){
		$sql = "SELECT * FROM notification";
		$statement = $this->conn->prepare($sql);
		$statement->execute();
		if ($statement->rowCount() > 0) {
			return $statement->fetchAll(PDO::FETCH_ASSOC);
		}
		return false;
    }

    public function read_all_request_notifications(){
		$sql = "SELECT a.*,b.* FROM notification a
        JOIN item_request b ON a.type_id=b.item_request_id
         WHERE a.type='Request' AND b.item_request_approved='No'
		 OR a.type='Return'
          ORDER BY notification_id DESC";
		$statement = $this->conn->prepare($sql);
		$statement->execute();
		if ($statement->rowCount() > 0) {
			return $statement->fetchAll(PDO::FETCH_ASSOC);
		}
		return false;
    }

    // public function check_username($username){
	// 	$sql = "SELECT a.*,b.* FROM notification a JOIN notification b 
    //     ON a.phone=b.notification_phone || a.email=b.notification_email 
    //     JOIN role c ON b.notification_role_id=c.role_id
    //      WHERE a.email=:username || a.phone=:username";
	// 	$statement = $this->conn->prepare($sql);
	// 	$statement->bindParam(":username",$username);
	// 	$statement->execute();
	// 	if ($statement->rowCount() > 0) {
	// 		return $statement->fetchAll(PDO::FETCH_ASSOC);
	// 	}
	// 	return false;
    // }DELETE FROM moviesdone 


    public function remove_notification($type_id){
		$sql = "DELETE FROM notification WHERE type_id=:type_id";
		$statement = $this->conn->prepare($sql);
        $statement->bindParam(":type_id",$type_id);
		$statement->execute();
		if ($statement->rowCount() > 0) {
            $statement->fetchAll(PDO::FETCH_ASSOC);
		}
		return false;
    }

	public function remove_notification_after_a_week(){
		$sql = "DELETE FROM notification WHERE notification_created_at < NOW() - INTERVAL 7 DAY 
		AND type='Return'";
		$statement = $this->conn->prepare($sql);
		$statement->execute();
		if ($statement->rowCount() > 0) {
            $statement->fetchAll(PDO::FETCH_ASSOC);
		}
		return false;
    }
    

}