<?php

if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

if (!isset($_SESSION['logged_in'])) {

    header("Location: ../login.php");
    exit();

}

if ($_SESSION['role'] != "admin") {

    header("Location: ../login.php");
    exit();

}

include "../config/database.php";

$userId = $_SESSION['user_id'];

$sql = "SELECT * FROM users WHERE id=? LIMIT 1";

$stmt = $pdo->prepare($sql);

$stmt->execute([$userId]);

$admin = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$admin) {

    session_destroy();

    header("Location: ../login.php");

    exit();

}

if ($admin['status'] != "active") {

    session_destroy();

    header("Location: ../login.php");

    exit();

}

?>