<?php
session_start();
include_once('../../includes/config.php');

header('Content-Type: application/json');

if (!isset($_SESSION['uid'])) {
    echo json_encode(['success' => false, 'message' => 'Unauthorized']);
    exit();
}

if (!isset($_POST['masterType']) || !isset($_POST['id'])) {
    echo json_encode(['success' => false, 'message' => 'Missing required parameters']);
    exit();
}

$masterType = $_POST['masterType'];
$id = (int)$_POST['id'];

$success = false;
$message = '';

switch($masterType) {
    case 'category':
        // Check if category is in use
        $checkQuery = "SELECT COUNT(*) as count FROM tblitems WHERE CategoryID = $id";
        $checkResult = mysqli_query($con, $checkQuery);
        $checkRow = mysqli_fetch_assoc($checkResult);
        
        if ($checkRow['count'] > 0) {
            $message = "Cannot delete category. It is being used by " . $checkRow['count'] . " item(s).";
        } else {
            $deleteQuery = "DELETE FROM tblcategories WHERE ID = $id";
            if (mysqli_query($con, $deleteQuery)) {
                $success = true;
                $message = "Category deleted successfully!";
            } else {
                $message = "Error deleting category: " . mysqli_error($con);
            }
        }
        break;

    case 'brand':
        // Check if brand is in use
        $checkQuery = "SELECT COUNT(*) as count FROM tblitems WHERE BrandID = $id";
        $checkResult = mysqli_query($con, $checkQuery);
        $checkRow = mysqli_fetch_assoc($checkResult);
        
        if ($checkRow['count'] > 0) {
            $message = "Cannot delete brand. It is being used by " . $checkRow['count'] . " item(s).";
        } else {
            $deleteQuery = "DELETE FROM tblbrands WHERE ID = $id";
            if (mysqli_query($con, $deleteQuery)) {
                $success = true;
                $message = "Brand deleted successfully!";
            } else {
                $message = "Error deleting brand: " . mysqli_error($con);
            }
        }
        break;

    case 'supplier':
        // Check if supplier is in use
        $checkQuery = "SELECT COUNT(*) as count FROM tblitems WHERE SupplierID = $id";
        $checkResult = mysqli_query($con, $checkQuery);
        $checkRow = mysqli_fetch_assoc($checkResult);
        
        if ($checkRow['count'] > 0) {
            $message = "Cannot delete supplier. It is being used by " . $checkRow['count'] . " item(s).";
        } else {
            $deleteQuery = "DELETE FROM tblsuppliers WHERE ID = $id";
            if (mysqli_query($con, $deleteQuery)) {
                $success = true;
                $message = "Supplier deleted successfully!";
            } else {
                $message = "Error deleting supplier: " . mysqli_error($con);
            }
        }
        break;

    case 'unit':
        // Check if unit is in use
        $checkQuery = "SELECT COUNT(*) as count FROM tblitems WHERE UnitID = $id";
        $checkResult = mysqli_query($con, $checkQuery);
        $checkRow = mysqli_fetch_assoc($checkResult);
        
        if ($checkRow['count'] > 0) {
            $message = "Cannot delete unit. It is being used by " . $checkRow['count'] . " item(s).";
        } else {
            $deleteQuery = "DELETE FROM tblunits WHERE ID = $id";
            if (mysqli_query($con, $deleteQuery)) {
                $success = true;
                $message = "Unit deleted successfully!";
            } else {
                $message = "Error deleting unit: " . mysqli_error($con);
            }
        }
        break;

    case 'tax':
        // Check if tax is in use
        $checkQuery = "SELECT COUNT(*) as count FROM tblitems WHERE TaxID = $id";
        $checkResult = mysqli_query($con, $checkQuery);
        $checkRow = mysqli_fetch_assoc($checkResult);
        
        if ($checkRow['count'] > 0) {
            $message = "Cannot delete tax type. It is being used by " . $checkRow['count'] . " item(s).";
        } else {
            $deleteQuery = "DELETE FROM tbltaxtypes WHERE ID = $id";
            if (mysqli_query($con, $deleteQuery)) {
                $success = true;
                $message = "Tax type deleted successfully!";
            } else {
                $message = "Error deleting tax type: " . mysqli_error($con);
            }
        }
        break;

    case 'location':
        // Check if location is in use
        $checkQuery = "SELECT COUNT(*) as count FROM tblitems WHERE LocationID = $id";
        $checkResult = mysqli_query($con, $checkQuery);
        $checkRow = mysqli_fetch_assoc($checkResult);
        
        if ($checkRow['count'] > 0) {
            $message = "Cannot delete location. It is being used by " . $checkRow['count'] . " item(s).";
        } else {
            // Also check if location is used by racks
            $checkRackQuery = "SELECT COUNT(*) as count FROM tblracks WHERE LocationID = $id";
            $checkRackResult = mysqli_query($con, $checkRackQuery);
            $checkRackRow = mysqli_fetch_assoc($checkRackResult);
            
            if ($checkRackRow['count'] > 0) {
                $message = "Cannot delete location. It is being used by " . $checkRackRow['count'] . " rack(s).";
            } else {
                $deleteQuery = "DELETE FROM tbllocations WHERE ID = $id";
                if (mysqli_query($con, $deleteQuery)) {
                    $success = true;
                    $message = "Location deleted successfully!";
                } else {
                    $message = "Error deleting location: " . mysqli_error($con);
                }
            }
        }
        break;

    case 'rack':
        // Check if rack is in use
        $checkQuery = "SELECT COUNT(*) as count FROM tblitems WHERE RackID = $id";
        $checkResult = mysqli_query($con, $checkQuery);
        $checkRow = mysqli_fetch_assoc($checkResult);
        
        if ($checkRow['count'] > 0) {
            $message = "Cannot delete rack. It is being used by " . $checkRow['count'] . " item(s).";
        } else {
            $deleteQuery = "DELETE FROM tblracks WHERE ID = $id";
            if (mysqli_query($con, $deleteQuery)) {
                $success = true;
                $message = "Rack deleted successfully!";
            } else {
                $message = "Error deleting rack: " . mysqli_error($con);
            }
        }
        break;

    default:
        $message = "Invalid master type";
}

echo json_encode([
    'success' => $success,
    'message' => $message
]);
?>
