<?php
session_start();

require_once '../database.php';

if(!isset($_SESSION['id']) || $_SESSION['id']=='')
{
	http_response_code(401);
    echo json_encode(['error_text' => 'Authentication Problem']);
    exit;
}
if(!isset($_SESSION['user_type']) || ($_SESSION['user_type'] == 'chef'))
{
	http_response_code(401);
    echo json_encode(['error_text' => 'Authentication Problem']);
    exit;
}


class insertUserTicket extends database
{
    public function index($noOfUsers, $date, $operator)
    {
        $this->dbh = $this->connectPDO1();
        $this->dbh->beginTransaction();
        try
        {
            $userCount  = 0;
            $result = $this->fetchRow('user_ticket', ['date' => $date]);
            if($result)
            {
                $userCount = $result['user_count'];
            }
            
            if($operator == '+')
            {
                $userCount = $userCount + $noOfUsers;
            }
            else
            {
                $userCount = $userCount - $noOfUsers;
            }
            
            if($userCount < 0)
            {
                $this->dbh->rollback();
                return 0;
            }
            
            $data['user_count'] = $userCount;
            $data['date'] = $date;
            
            if(!$result)
            {
                $res = $this->insert('user_ticket', $data);
                if(!$res)
                {
                    $this->dbh->rollback();
                    return 0;
                }
            }
            else
            {
                $res = $this->update('user_ticket', $data, ['id' => $result['id']]);
                if(!$res)
                {
                    $this->dbh->rollback();
                    return 0;
                }
            }
            
            
            $return['count'] = $userCount;
            
            $this->dbh->commit();
            return $return;
        }
        catch (PDOException $e) {
            // roll back transaction
            $this->dbh->rollback();
            return 0;
        }  
        
        
    }
}


if(!isset($_POST['noOfUsers']) || !isset($_POST['date']) || !isset($_POST['operator']))
{
    http_response_code(400);
    echo json_encode(['error_text' => 'Please add number of users, date and operator']);
    exit;
}

$noOfUsers = $_POST['noOfUsers'];
$date = $_POST['date'];
$operator = $_POST['operator'];

if(trim($noOfUsers) == '' || !is_numeric($noOfUsers))
{
    http_response_code(400);
    echo json_encode(['error_text' => 'Please enter a numeric value for noOfUsers']);
    exit;
}

if(trim($date) == '')
{
    http_response_code(400);
    echo json_encode(['error_text' => 'Please enter date']);
    exit;
}

if(trim($operator) == '' || ($operator != '+' && $operator != '-'))
{
    http_response_code(400);
    echo json_encode(['error_text' => 'Please enter a correct operator']);
    exit;
}

$date = date('Y-m-d', strtotime($date));


$obj = new insertUserTicket();
$res = $obj->index($noOfUsers, $date, $operator);
if(!$res)
{
    http_response_code(500);
    echo json_encode(['error_text' => 'Something went Wrong. <br/>Hint:Total users cannot be negative']);
    exit;
}

http_response_code(200);
    echo json_encode(['count' => $res['count']]);
    exit;