<?php
error_reporting(E_ERROR | E_PARSE);
ini_set('display_errors', '1');
include('../user/extension/connect.php');

$con = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
if (mysqli_connect_error()) {
    echo 'connect to database failed';
} else {

    if (isset($_GET['username'], $_GET['password'], $_GET['device_id'], $_GET['device_model'])) {
        $username = strip_tags(trim($_GET['username']));
        $password = strip_tags(trim($_GET['password']));
        $device_id = strip_tags(trim($_GET['device_id']));
        $device_model = strip_tags(trim($_GET['device_model']));

        if (empty($username)) {
            $LenzData = ['value' => '', 'msg' => 'Error occurred.', 'param' => 'username', 'location' => 'query'];
            $data[] = $LenzData;
            $json_data = ["status" => 'invalid', "errors" => $data];
            echo json_encode($json_data);
        } elseif (empty($password)) {
            $LenzData = ['value' => '', 'msg' => 'Error occurred.', 'param' => 'password', 'location' => 'query'];
            $data[] = $LenzData;
            $json_data = ["status" => 'invalid', "errors" => $data];
            echo json_encode($json_data);
        } elseif (empty($device_id)) {
            $LenzData = ['value' => '', 'msg' => 'Error occurred.', 'param' => 'device_id', 'location' => 'query'];
            $data[] = $LenzData;
            $json_data = ["status" => 'invalid', "errors" => $data];
            echo json_encode($json_data);
        } elseif (empty($device_model)) {
            $LenzData = ['value' => '', 'msg' => 'Error occurred.', 'param' => 'device_model', 'location' => 'query'];
            $data[] = $LenzData;
            $json_data = ["status" => 'invalid', "errors" => $data];
            echo json_encode($json_data);
        } else {
            // Retrieve user information including is_freeze status
            $qry = mysqli_query($con, "SELECT user_duration, is_freeze FROM users WHERE user_name='$username' LIMIT 1");
            $result = mysqli_fetch_array($qry);

            if ($result) {
                // Check if user is frozen
                if ($result['is_freeze'] == '1') {
                    $json_data = ["auth" => false, "expiry" => 'User blocked', "device_match" => 'blocked'];
                    echo json_encode($json_data);
                } else {
                    // User is not frozen, continue with the existing authentication logic
                    function calc_time($minutes)
                    {
                        $seconds = $minutes * 60;
                        $days = (int)($seconds / 86400);
                        $seconds -= ($days * 86400);
                        $hours = (int)($seconds / 3600);
                        $seconds -= ($hours * 3600);
                        $minutes = (int)($seconds / 60);
                        $seconds -= ($minutes * 60);
                        return ['days' => $days, 'hours' => $hours, 'minutes' => $minutes, 'seconds' => $seconds];
                    }

                    $dur = calc_time($result['user_duration']);
                    $pdays = $dur['days'] . " days";
                    $phours = $dur['hours'] . " hours";
                    $pminutes = $dur['minutes'] . " minutes";
                    $pseconds = $dur['seconds'] . " seconds";

                    $user_duration = strtotime($pdays . $phours . $pminutes . $pseconds);
                    $user_duration = date('Y-m-d h:i:s', $user_duration);

                    $result2 = mysqli_query($con, "SELECT user_name FROM users WHERE user_name='$username' AND user_pass='$password'");

                    if (mysqli_num_rows($result2) > 0) {
                        $sql2 = mysqli_query($con, "SELECT device_id, user_duration FROM users WHERE user_name='$username' AND user_pass='$password'");
                        $result3 = mysqli_fetch_array($sql2);

                        $dev_id = $result3['device_id'];
                        $dev_model = $result3['device_model'];

                        if ($result3['user_duration'] == 0) {
                            $json_data = ["auth" => false, "expiry" => 'User already expired', "device_match" => 'expired'];
                        } else {
                            $json_data = ["auth" => true, "expiry" => $user_duration, "device_match" => true];
                            mysqli_query($con, "UPDATE users SET device_id='$device_id', device_model='$device_model', device_connected=1 WHERE user_name='$username'");
                        }
                        echo json_encode($json_data);
                    } else {
                        $json_data = ["auth" => false, "expiry" => 'none', "device_match" => 'none'];
                        echo json_encode($json_data);
                    }
                }
            } else {
                $json_data = ["auth" => false, "expiry" => 'none', "device_match" => 'none'];
                echo json_encode($json_data);
            }
        }
    } else {
        $json_data = ["auth" => false, "expiry" => 'none', "device_match" => 'none'];
        echo json_encode($json_data);
    }
}
?>
