<?php
session_start();
$conn = mysqli_connect("localhost", "root", "", "login_app");
function setActiveClass($pageName)
{
    $current_page = basename($_SERVER['PHP_SELF']);
    return ($current_page === $pageName) ? "active" : "";
}
function getPageClass()
{
    return basename($_SERVER['PHP_SELF'], ".php");
}
function user_exists($conn, $username)
{
    $sql = "SELECT * FROM users WHERE username='$username' LIMIT  1";
    $result = mysqli_query($conn, $sql);
    return mysqli_num_rows($result) > 0;
}
function isloggedin()
{
    return (isset($_SESSION['logged_in']) && $_SESSION['logged_in'] === true) ||
        (isset($_COOKIE['logged_in']) && $_COOKIE['logged_in'] === 'true');
}

function isAdmin($conn)
{
    if (isset($_SESSION['username']) && !empty($_SESSION['username'])) {
        $username = $_SESSION['username'];
    } elseif (isset($_COOKIE['username']) && !empty($_COOKIE['username'])) {
        $username = $_COOKIE['username'];
    } else {
        return false;
    }

    $query = "SELECT role FROM users WHERE username = '$username'";
    $result = mysqli_query($conn, $query);

    if (mysqli_num_rows($result) > 0) {
        $row = mysqli_fetch_assoc($result);
        return ($row['role'] === 'admin');
    }

    return false;
}
function redirect($location)
{
    header("Location: $location");
    exit;
}
function full_month_date($date)
{
    return date("F j, Y", strtotime($date));
}

$title = '';
function page_title($func_title)
{
    global $title;
    $title = (string) $func_title;
}
function page_title_output()
{
    global $title;
    return $title;
}

/*
function enqueue_file_v2($file, $type = 'js', $location = 'header', $options = [])
{
    if (!in_array($type, ['js', 'css'])) return;

    $defer = !empty($options['defer']) ? ' defer' : '';
    $async = !empty($options['async']) ? ' async' : '';

    if ($type === 'js') {
        $output = "<script src=\"$file\"$defer$async></script>\n";
    } elseif ($type === 'css') {
        $output = "<link rel=\"stylesheet\" href=\"$file\">\n";
    } else {
        return;
    }

    if ($location === 'header' && function_exists('enqueue_file_header')) {
        enqueue_file_header($output);
    } elseif ($location === 'footer' && function_exists('enqueue_file_footer')) {
        enqueue_file_footer($output);
    }
}
*/
$enqueue_registry = [];
function enqueue_file($file, $type = 'js', $location = 'header', $options = [])
{
    global $enqueue_registry;

    if (!in_array($type, ['js', 'css'])) return;

    $defer = false;
    $async = false;

    if ($type === 'js') {
        if (!empty($options['defer'])) $defer = true;
        if (!empty($options['async'])) $async = true;
    }

    if ($file && $type && $location) {
        foreach ($enqueue_registry as $existing_file) {
            if (
                $existing_file['file_path'] === $file &&
                $existing_file['location'] === $location &&
                $existing_file['file_type'] === $type
            ) {
                return;
            }
        }

        $count = count($enqueue_registry) + 1;
        $key = "file_{$count}";
        $enqueue_registry[$key] = [
            'location'  => $location,
            'file_type' => $type,
            'file_path' => $file,
            'options'   => array_filter([
                'defer' => $defer ? true : null,
                'async' => $async ? true : null,
            ]),
        ];
    }
}

function enqueue_file_outputer($location)
{
    global $enqueue_registry;

    $output = '';

    foreach ($enqueue_registry as $file) {
        if ($file['location'] !== $location) continue;

        $file_type = $file['file_type'];
        $file_path = $file['file_path'];
        $options   = $file['options'] ?? [];

        if ($file_type === 'js') {
            $defer = !empty($options['defer']) ? ' defer' : '';
            $async = !empty($options['async']) ? ' async' : '';
            $output .= "<script src=\"$file_path\"$defer$async></script>\n";
        } elseif ($file_type === 'css') {
            $output .= "<link rel=\"stylesheet\" href=\"$file_path\">\n";
        }
    }

    return $output;
}
