<?php     
class Session{

    public function logUserAdmin($admin_id){
		require "db.php";
	    $_SESSION['adminId'] = $admin_id;

        $selector = base64_encode(random_bytes(8));
        $token = bin2hex(random_bytes(32));

        $cookieValue = $selector.':'.base64_encode($token);
        $hashedToken = hash('sha256', $token);

        $timestamp = date("Y-m-d H:i:s");
	    $loginTime = date("Y-m-d H:i:s");
        setcookie('authadminToken1', $cookieValue, time()+3600, NULL, NULL, NULL, true);
		
		
		$query1 = "SELECT * FROM admin_session WHERE admin_id = $admin_id";
		$run1 = mysqli_query($conn, $query1);
		if(mysqli_num_rows($run1) >0){
			$query2 = "DELETE FROM admin_session WHERE admin_id = $admin_id";
			$run2 = mysqli_query($conn, $query2);					
		}
		
		
        $query3 = "INSERT INTO admin_session (selector, token, admin_id, login_time, login_expiry) VALUES ('$selector', '$hashedToken', '$admin_id', '$loginTime' , '$timestamp')";
		$run3 = mysqli_query($conn, $query3);
		if(mysqli_affected_rows($conn) == 1){
			return true;
		}else{
			return false;
		}
		
    }

    public function logUserStaff($employee_id){
		require "db.php";
	    $_SESSION['staffId'] = $employee_id;

        $selector = base64_encode(random_bytes(8));
        $token = bin2hex(random_bytes(32));

        $cookieValue = $selector.':'.base64_encode($token);
        $hashedToken = hash('sha256', $token);

        $timestamp = date("Y-m-d H:i:s");
	    $loginTime = date("Y-m-d H:i:s");
        setcookie('authstaffToken1', $cookieValue, time()+3600, NULL, NULL, NULL, true);
		
		
		$query1 = "SELECT * FROM employee_session WHERE employee_id = $employee_id";
		$run1 = mysqli_query($conn, $query1);
		if(mysqli_num_rows($run1) >0){
			$query2 = "DELETE FROM employee_session WHERE employee_id = $employee_id";
			$run2 = mysqli_query($conn, $query2);			
		}
		
		
        $query3 = "INSERT INTO employee_session (selector, token, employee_id, login_time, login_expiry) VALUES ('$selector', '$hashedToken', '$employee_id', '$loginTime' , '$timestamp')";
		$run3 = mysqli_query($conn, $query3);
		if(mysqli_affected_rows($conn)==1){
			return true;
		}else{
			return false;
		}		
    }	

    public function relogUserAdmin($admin_id){
        $_SESSION['adminId'] = $admin_id;
    }

    public function relogUserStaff($employee_id){
        $_SESSION['staffId'] = $employee_id;
    }

    public function isLoggedAdmin(){
		require "db.php";

        if(isset($_SESSION['adminId'])){
            return true;
        }else{
            if(isset($_COOKIE['authadminToken1'])){               

                list($selector, $token) = explode(':', $_COOKIE['authadminToken1']);

                $query = "SELECT * FROM admin_session WHERE selector = '$selector' ";
                $run = mysqli_query($conn, $query);
				$row = mysqli_fetch_array($run);

                if(mysqli_num_rows($run)>0){
                    if(hash_equals($row['token'], hash('sha256', base64_decode($token)))){
                        $relog = $this->relogUserAdmin($row['admin_id']);
						if($relog){
							return true;
						}else{
							return false;
						}
						
                    }else{
                        $this->logOutAdmin();
                        return false;
                    }
                }else{
                    return false;
                }
            }else{
                return false;
            }
        }
    }

    public function isLoggedStaff(){
        require "db.php";

        if(isset($_SESSION['staffId'])){
            return true;
        }else{
            if(isset($_COOKIE['authstaffToken1'])){                

                list($selector, $token) = explode(':', $_COOKIE['authstaffToken1']);

                $query = "SELECT * FROM employee_session WHERE selector = '$selector' ";
                $run = mysqli_query($conn, $query);
                $row = mysqli_fetch_array($run);

                if(mysqli_num_rows($run)>0){
                    if(hash_equals($row['token'], hash('sha256', base64_decode($token)))){
                        $relog = $this->relogUserStaff($row['employee_id']);
                        if($relog){
                            return true;
                        }else{
                            return false;
                        }
                        
                    }else{
                        $this->logOutStaff();
                        return false;
                    }
                }else{
                    return false;
                }
            }else{
                return false;
            }
        }
    }

    public function logOutAdmin(){              
        if(isset($_COOKIE['authadminToken1'])){
            require "db.php";
            list($selector, $token) = explode(':', $_COOKIE['authadminToken1']);
            $query1 = "DELETE FROM admin_session WHERE selector = '$selector'";
            $run1 = mysqli_query($conn, $query1);
            setcookie('authadminToken1', '', 1);
            unset($_COOKIE['authadminToken1']);
        }  
        if(isset($_SESSION['adminId'])){
            require "db.php";
            $admin_id = $_SESSION['adminId'];
            $query2 = "DELETE FROM admin_session WHERE admin_id = $admin_id" ;            
            $run2 = mysqli_query($conn, $query2);            
            unset($_SESSION['adminId']);
        }
        setcookie('authadminToken1', '', 1);
        unset($_COOKIE['authadminToken1']);
		return true;
    }


    public function logOutStaff(){              
        if(isset($_COOKIE['authstaffToken1'])){
            require "db.php";
            list($selector, $token) = explode(':', $_COOKIE['authstaffToken1']);
            $query1 = "DELETE FROM employee_session WHERE selector = '$selector'";
            $run1 = mysqli_query($conn, $query1);
            setcookie('authstaffToken1', '', 1);
            unset($_COOKIE['authstaffToken1']);
        }  
        if(isset($_SESSION['staffId'])){
            require "db.php";
            $employee_id = $_SESSION['staffId'];
            $query2 = "DELETE FROM employee_session WHERE employee_id = $employee_id" ;            
            $run2 = mysqli_query($conn, $query2);            
            unset($_SESSION['staffId']);
        }
        setcookie('authstaffToken1', '', 1);
        unset($_COOKIE['authstaffToken1']);
		return true;
    }

    
}
?>