<?php
    include_once("CUserSession.php");
    include_once("helper.php");
    include_once("CUtil.php");
    include_once("CEnums.php");
    include_once("CProjectSolutions.php");

    class CNotification{

        public $sl_no;
        public $notification_type_id;
        public $origin;
        public $to_user;
        public $message;
        public $is_email_sent;
        public $is_read;
        public $context;
        public $sub_context;
        public $timestamp;

        
        function populateFromDBRow( $row ){
            $this->sl_no = $row["sl_no"];
            $this->notification_type_id = $row["notification_type_id"];
            $this->origin = $row["origin"];
            $this->to_user = $row["to_user"];
            $this->message = $row["message"];
            $this->is_email_sent = $row["is_email_sent"];
            $this->is_read  = $row["is_read"];
            $this->context = $row["context"];
            $this->sub_context = $row["sub_context"];
            $this->timestamp = $row["timestamp"];
        }

        function store_to_db()
        {
            //$user_id = $_SESSION['user']->_id;
            //$user_id = "avishkar-f6dd5d0c2b";
            //$sql = "REPLACE into Notifications_user_settings(user_id, notification_type_id, email, web_page) values('".$user_id."',".$this->sl_no.",'".$this->email."','".$this->web_page."') ";
            $sql = "INSERT INTO Notifications (notification_type_id, to_user, message,context,sub_context) VALUES (".$this->notification_type_id.",'".$this->to_user."','".$this->message."','".$this->context."','".$this->sub_context."')";
            return write_db($sql);
        }

        function read()
        {
            $sql = "UPDATE Notifications set is_read = '".$this->is_read."' where sl_no = ".$this->sl_no;
            return write_db($sql);
        }
    }

    function get_notifications( $type="unread" )
    {
        $sql_conn = mysqli_connection();
        $ret["success"] = true;

        //$user_id = $_SESSION['user']->_id;
        $user_id = "avishkar-f6dd5d0c2b";
        $sql = "SELECT * FROM `Notifications` WHERE to_user LIKE '".$user_id."' AND is_read='no' ORDER BY timestamp";
        
        try
        {
            $result = mysqli_query($sql_conn, $sql);
            $res = array();
            while($row = mysqli_fetch_array($result)) {
                $notification = new CNotification();
                $notification->populateFromDBRow($row);
                $res[] = $notification;
            }
        }
        catch (Exception $e){
            $ret["success"] = false;
            $ret['message'] = "Error : ".$e->getMessage();
        }

        $ret["notifications"] = $res;
        mysqli_close($sql_conn);
        return $ret;
    }

    function set_notifications()
    {
        $user_id = "avishkar-f6dd5d0c2b";

        $notification = new CNotification();
        $notification->notification_type_id = 3;
        $notification->to_user =  $user_id;
        $notification->message = "New notification through code";
        $notification->context = "project_id";
        $notification->sub_context = "solution_no";
        return $notification->store_to_db();
    }

    function read_notifications($sl_no)
    {
        $notification = new CNotification();
        $notification->sl_no = $sl_no;
        $notification->is_read = "yes";
        return $notification->read();
    }

?>