<?php
require_once __DIR__ . '/../../app/helpers/functions.php';
require_once __DIR__ . '/../../app/middleware/auth.php';

session_boot();
require_login();

if (!isset($_SESSION['admin_impersonator'])) {
    redirect('/admin/dashboard.php');
}

$conn = require __DIR__ . '/../../app/config/database.php';

/* Restore admin */
$adminId = (int)$_SESSION['admin_impersonator'];

$st = $conn->prepare("SELECT * FROM users WHERE id=? LIMIT 1");
$st->bind_param("i", $adminId);
$st->execute();
$admin = $st->get_result()->fetch_assoc();

unset($_SESSION['admin_impersonator']);
session_regenerate_id(true);
$_SESSION['user'] = $admin;

redirect('/admin/dashboard.php');
