<?php

declare(strict_types=1);

include 'db.php';
session_start();


/* =========================================================
   ADMIN LOGIN CHECK
========================================================= */

if (
    !isset($_SESSION['user_id']) ||
    !isset($_SESSION['role']) ||
    strtolower(trim((string)$_SESSION['role'])) !== 'admin'
) {

    header("Location: index.php?deleted=unauthorized");
    exit;

}


/* =========================================================
   DATABASE CONNECTION CHECK
========================================================= */

if (!isset($conn) || !$conn) {

    die("Database connection failed.");

}


/* =========================================================
   GET DOMAIN ID
   Supports POST + GET
========================================================= */

$id = 0;


/* New delete form */

if (isset($_POST['id'])) {

    $id = (int)$_POST['id'];

}


/* Old delete URL */

elseif (isset($_GET['id'])) {

    $id = (int)$_GET['id'];

}


/* =========================================================
   VALIDATE ID
========================================================= */

if ($id <= 0) {

    header("Location: index.php?deleted=invalid");
    exit;

}


/* =========================================================
   CHECK DOMAIN EXISTS
========================================================= */

$checkSql = "
    SELECT id
    FROM domains
    WHERE id = ?
    LIMIT 1
";

$checkStmt = mysqli_prepare(
    $conn,
    $checkSql
);


if (!$checkStmt) {

    die(
        "Database Prepare Error: " .
        htmlspecialchars(
            mysqli_error($conn),
            ENT_QUOTES,
            'UTF-8'
        )
    );

}


mysqli_stmt_bind_param(
    $checkStmt,
    "i",
    $id
);


if (!mysqli_stmt_execute($checkStmt)) {

    $error = mysqli_stmt_error($checkStmt);

    mysqli_stmt_close($checkStmt);

    die(
        "Database Execute Error: " .
        htmlspecialchars(
            $error,
            ENT_QUOTES,
            'UTF-8'
        )
    );

}


/* =========================================================
   GET RESULT WITHOUT mysqli_stmt_get_result()
========================================================= */

mysqli_stmt_store_result($checkStmt);


if (mysqli_stmt_num_rows($checkStmt) <= 0) {

    mysqli_stmt_free_result($checkStmt);
    mysqli_stmt_close($checkStmt);

    header("Location: index.php?deleted=notfound");
    exit;

}


mysqli_stmt_free_result($checkStmt);
mysqli_stmt_close($checkStmt);


/* =========================================================
   DELETE DOMAIN
========================================================= */

$deleteSql = "
    DELETE FROM domains
    WHERE id = ?
    LIMIT 1
";

$deleteStmt = mysqli_prepare(
    $conn,
    $deleteSql
);


if (!$deleteStmt) {

    die(
        "Delete Prepare Error: " .
        htmlspecialchars(
            mysqli_error($conn),
            ENT_QUOTES,
            'UTF-8'
        )
    );

}


mysqli_stmt_bind_param(
    $deleteStmt,
    "i",
    $id
);


/* =========================================================
   EXECUTE DELETE
========================================================= */

if (!mysqli_stmt_execute($deleteStmt)) {

    $error = mysqli_stmt_error($deleteStmt);

    mysqli_stmt_close($deleteStmt);

    die(
        "Delete Error: " .
        htmlspecialchars(
            $error,
            ENT_QUOTES,
            'UTF-8'
        )
    );

}


/* =========================================================
   CHECK AFFECTED ROWS
========================================================= */

$affectedRows =
    mysqli_stmt_affected_rows($deleteStmt);


mysqli_stmt_close($deleteStmt);


/* =========================================================
   SUCCESS
========================================================= */

if ($affectedRows > 0) {

    header("Location: index.php?deleted=1");
    exit;

}


/* =========================================================
   DELETE FAILED
========================================================= */

header("Location: index.php?deleted=0");
exit;

?>