<?php
session_start();
require_once __DIR__ . '/../config/db.php';

// Set timezone
date_default_timezone_set('Asia/Kolkata');

function isLoggedIn() {
    return isset($_SESSION['user_id']);
}

function isAdmin() {
    return isLoggedIn() && $_SESSION['role'] === 'admin';
}

function requireLogin() {
    if (!isLoggedIn()) {
        header("Location: ../login.php");
        exit();
    }
}

function requireAdmin() {
    if (!isAdmin()) {
        header("Location: ../login.php");
        exit();
    }
}

function generateRegistrationNumber() {
    // MCDOLIR-XXXX-XXXXXXX
    // Let's generate random numbers to fill the format.
    $part1 = rand(1000, 9999);
    $part2 = rand(1000000, 9999999);
    return "MCDOLIR-$part1-$part2";
}

function getSettings($pdo) {
    $stmt = $pdo->query("SELECT * FROM settings WHERE id = 1");
    return $stmt->fetch();
}
?>
