<?php
session_start();
require_once "../models/Operations.php";
require_once "../models/globalFunctions.php";
$pdo = Databases::connect();

// ---------------- SESSION VARIABLES ----------------
$session_user_id       = $_SESSION["user_id"] ?? "";
$session_user_ent_id   = $_SESSION["user_ent_id"] ?? "";
$session_user_pos_id = $_SESSION["user_pos_id"] ?? "";

$session_user_firstname = $_SESSION["user_firstname"] ?? "";
$session_user_lastname  = $_SESSION["user_lastname"] ?? "";
$session_user_fullname  = $_SESSION["user_fullname"] ?? "";
$session_user_name      = $_SESSION["user_name"] ?? "";
$session_user_profile   = $_SESSION["user_profile"] ?? "";
$session_user_image     = $_SESSION["user_image"] ?? "";

$session_login_time    = $_SESSION["login_time"] ?? 0;
$session_expire_time   = $_SESSION["expire_time"] ?? 3600; // 1 hour;

if ($session_login_time && $session_expire_time && (time() - $session_login_time > $session_expire_time)) {
	session_unset();
	session_destroy();
	exit("Session expired");
}

$_SESSION['login_time'] = time(); // update activity

// ---------------- CSRF VALIDATION ----------------
function validateCsrf(): void
{
	if (
		empty($_POST['csrf_token']) || !isset($_SESSION['csrf_token']) ||
		!hash_equals($_SESSION['csrf_token'], $_POST['csrf_token'])
	) {
		http_response_code(400);
		exit("Invalid CSRF token");
	}
}

// Only validate CSRF if POST
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
	validateCsrf();
}

// ---------------- USER LOGIN CHECK ----------------
if (empty($session_user_id) || empty($session_user_ent_id) || empty($session_user_pos_id)) {
	session_unset();
	session_destroy();
	exit("Session expired");
}


// Build dynamic URLs (e.g., for local and production environments)
$scheme = 'http';
if ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ||
	$_SERVER['SERVER_PORT'] == 443
) {
	$scheme = 'https';
}
// $appURL = "https://evosyspos.com";
$appURL = $scheme . '://' . $_SERVER['HTTP_HOST'];


// DEFAULT USER LANGUAGE SETTINGS
$getUserLanguage = file_get_contents("../lang/en.json");

//DEFAULT BRANCH LANGUAGE SETTINGS
$getPosLanguage = file_get_contents("../lang/pos_en.json");

if ($session_user_id > 0) {

	$userInfo = getUserData($session_user_id);
	$entInfo = getEnterpriseData($session_user_ent_id);
	$posInfo = getBranchData($session_user_pos_id);

	$userLanguage = $userInfo['user_language'];

	if ($userLanguage == "es") {
		$getUserLanguage = file_get_contents("../lang/es.json");
	} else if ($userLanguage == "fr") {
		$getUserLanguage = file_get_contents("../lang/fr.json");
	} else {
		$getUserLanguage = file_get_contents("../lang/en.json");
	}

	//POS TRANSLATE
	$posLang = $posInfo['pos_lang'];

	if ($posLang == "es") {
		$getPosLanguage = file_get_contents("../lang/pos_es.json");
	} else if ($posLang == "fr") {
		$getPosLanguage = file_get_contents("../lang/pos_fr.json");
	} else {
		$getPosLanguage = file_get_contents("../lang/pos_en.json");
	}
} else {

	// For testing purposes only
	$session_user_id = 6;
	$session_user_ent_id = 2;
	$session_user_pos_id = 2;

	$userInfo = getUserData($session_user_id);
	$entInfo = getEnterpriseData($session_user_ent_id);
	$posInfo = getBranchData($session_user_pos_id);
}

$translate = json_decode($getUserLanguage, true);
$posTranslate = json_decode($getPosLanguage, true);


$timeZone = $posInfo['pos_time_zone'] ?? "America/New_York";
$currentDateTimeByZone = mdlGetDateTimeByZone($timeZone);
$currentTimeByZone = mdlGetTimeByZone($timeZone);

date_default_timezone_set($timeZone);

$currentDate = date('Y-m-d');
$currentTime  = date('H:i:s');
$yesterdayStart  = date('Y-m-d 07:00:00', strtotime("-1 day"));
$todayStart = date('Y-m-d 07:00:00');
$timeStart = $posInfo["pos_day_start_time"] ?? "07:00:00";
$timeEnd = $posInfo["pos_day_end_time"] ?? "06:59:59";
$todayDate = date('Y-m-d');
$todayEnd = date('Y-m-d 06:59:59');
$tomorrowEnd  = date('Y-m-d 06:59:59', strtotime("+1 day"));
$last7Days = date('Y-m-d', strtotime('-7 days'));
$next7Days = date('Y-m-d', strtotime('+7 days'));

$sale_date_close = "";

if ($currentTime <= date('06:59:59')) {
	$currentDayStart = $yesterdayStart;
	$currentDayEnd = $todayEnd;
	$sale_date_close = $todayEnd;
} else {
	$currentDayStart = $todayStart;
	$currentDayEnd = $tomorrowEnd;
	$sale_date_close = $tomorrowEnd;
}
