<?php
session_start();

if (!isset($_SESSION['user_id'])) {
    header("Location: /beauty_pos/login.php");
    exit;
}

include_once __DIR__ . "/../config/database.php";

if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {

    $_SESSION['error'] = "Invalid customer.";

    header("Location: /beauty_pos/customers/index.php");
    exit;
}

$id = (int)$_GET['id'];

$checkCustomer = mysqli_query(
    $conn,
    "SELECT id
     FROM customers
     WHERE id='$id'
     LIMIT 1"
);

if (mysqli_num_rows($checkCustomer) == 0) {

    $_SESSION['error'] = "Customer not found.";

    header("Location: /beauty_pos/customers/index.php");
    exit;
}

/*
|--------------------------------------------------------------------------
| Delete Restriction
|--------------------------------------------------------------------------
| Sales table structure is required before enabling production restriction.
| If your sales table contains customer_id, replace this block with:
|
| SELECT id FROM sales WHERE customer_id='$id' LIMIT 1
|
*/

$delete = mysqli_query(
    $conn,
    "DELETE FROM customers
     WHERE id='$id'
     LIMIT 1"
);

if ($delete) {

    $_SESSION['success'] = "Customer deleted successfully.";

} else {

    $_SESSION['error'] = "Unable to delete customer.";

}

header("Location: /beauty_pos/customers/index.php");
exit;