<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: GET");
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Pragma: no-cache");
header("Expires: 0");

date_default_timezone_set("Asia/Qatar");

$data = json_decode(file_get_contents("php://input"));

include_once '../config.php';
$database = new Database();
$db = $database->getConnection();

$sqlQueryCheck0 = "SELECT value FROM ahqa_core_config_data WHERE path='delivery_date/first_available/min_delay'";
$stmtExist0 = $db->prepare($sqlQueryCheck0);
$stmtExist0->execute();
$countEx0 = $stmtExist0->rowCount();

$sqlQueryCheck1 = "SELECT value FROM ahqa_core_config_data WHERE path='delivery_date/last_available/max_delay'";
$stmtExist1 = $db->prepare($sqlQueryCheck1);
$stmtExist1->execute();
$countEx1 = $stmtExist1->rowCount();

$sqlQueryCheck2 = "SELECT value FROM ahqa_core_config_data WHERE path='delivery_date/main/time_options'";
$stmtExist2 = $db->prepare($sqlQueryCheck2);
$stmtExist2->execute();
$countEx2 = $stmtExist2->rowCount();

$results = array();

if ($countEx2 > 0 && $countEx1 > 0) {
    $rowExist0 = $stmtExist0->fetch(PDO::FETCH_ASSOC);
    $rowExist1 = $stmtExist1->fetch(PDO::FETCH_ASSOC);
    $rowExist2 = $stmtExist2->fetch(PDO::FETCH_ASSOC);

    $currentDate = date('Y-m-d');
    $startdate = (isset($rowExist0['value']) && $rowExist0['value'] == 0) ? 0 : $rowExist0['value'];
    $rowExist1End = (isset($rowExist1['value']) && $rowExist1['value'] > 0) ? intval($rowExist1['value']) : 1;

    for ($i = $startdate; $i <= $rowExist1End; $i++) {
        $createDate = date('Y-m-d', strtotime($currentDate . ' +' . $i . ' day'));

        if ($i == 0) {
            // For today's date, add all time slots
            $timslotList = json_decode($rowExist2['value'], true);

            foreach ($timslotList as $vData) {
                $results['data'][$createDate][] = $vData['from']['hour'] . ':' . $vData['from']['minute'] . ' — ' . $vData['to']['hour'] . ':' . $vData['to']['minute'];
            }
        } elseif ($i == 1 && date('Y-m-d', strtotime($currentDate . ' +1 day')) === '2024-04-10') {
            // For tomorrow, if it's April 10th, disable the first three time slots
            $timslotList = json_decode($rowExist2['value'], true);

            $index = 0;
            foreach ($timslotList as $vData) {
                if ($index < 3) {
                    // $results['data'][$createDate][] = 'Disabled';
                } else {
                    $results['data'][$createDate][] = $vData['from']['hour'] . ':' . $vData['from']['minute'] . ' — ' . $vData['to']['hour'] . ':' . $vData['to']['minute'];
                }
                $index++;
            }
        } else {
            // For all other days, add all time slots
            $timslotList = json_decode($rowExist2['value'], true);

            foreach ($timslotList as $vData) {
                $results['data'][$createDate][] = $vData['from']['hour'] . ':' . $vData['from']['minute'] . ' — ' . $vData['to']['hour'] . ':' . $vData['to']['minute'];
            }
        }
    }

    http_response_code(200);
    $results['success'] = true;
    echo json_encode($results);
} else {
    http_response_code(404);
    $results['success'] = false;
    $results['message'] = 'No time slots available.';
    echo json_encode($results);
}

$db = null;
?>