<?php

header('content-type:application/json');
header("Access-Control-Allow-Origin: *");
$timezone = new DateTimeZone('Africa/Accra');
$datetime = new DateTime('now', $timezone);
$formattedDateTime = $datetime->format('Y-m-d H:i:sP');
if ($_SERVER["REQUEST_METHOD"] !== "GET") {
    $timestamp = $formattedDateTime;
    $status = 405;
    $error = "Method Not Allowed";
    $message = "Request method is not valid";
    $resultData = array('timestamp' => $timestamp, 'status' => $status, 'error' => $error, 'message' => $message);
} else {
    session_start();
    if (isset($_SERVER['HTTP_AUTHORIZATION'])) {
        if ($authorizationHeader == 0) {
            $headers = getallheaders();
            $token = $headers['Authorization'];
            list($header, $payload, $signature) = explode('.', $token);
            $decodedHeader = json_decode(base64_decode($header), true);
            $decodedPayload = json_decode(base64_decode($payload), true);
            // $servername = "localhost";
            // $username = "schoolro_maker";
            // $password = "JesusChristisLord";
            // $dbname = "schoolro_schoolrobot_schools_account";
            // $school_conn = mysqli_connect($servername, $username, $password, $dbname) or die('Could not connect the database : Username or password incorrect');
            $query = mysqli_query($school_conn, "SELECT * FROM attendance_device_list WHERE token = '$token'");
            $affectedRow = mysqli_affected_rows($school_conn);
            if ($affectedRow > 0) {
                    if (isset($decodedPayload['exp']) && $decodedPayload['exp'] < time()) {
                        $resultData = array('code' => 1, 'msg' => "Token has expired");
                    } else {
                        while ($row = mysqli_fetch_assoc($query)) {
                            $macId = $row["macId"];
                        }
                        $secretKey = $macId;
                        $isValidSignature = hash_equals(
                            hash_hmac('sha256', "$header.$payload", $yourSecretKey),
                            $signature
                        );

                        if (!$isValidSignature) {
                            $query = mysqli_query($school_conn, "SELECT * FROM attendance_device_list WHERE macId = '$macId'");
                            while ($row = mysqli_fetch_assoc($query)) {
                                $getUuid = $row['updated_on'];
                            }
                            $resultData = array('code' => 1, 'msg' => "success", 'data' => $getUuid);
                        }
                    }
            } else {
                $resultData = array('code' => 8003, 'error' => "Token is invalid");
            }
        } else {
            $resultData = array('code' => 8001, 'error' => "Invalid Authorization header format");
        }
    } else {
        $resultData = array('code' => 8002, 'error' => "Authorization header not found");
    }
}
echo json_encode($resultData);
