<?php
/**
 * Delete Staff User
 * LandSales Pro - Delete a staff user
 */
define('APP_RUNNING', true);
require_once '../../db_connect.php';
require_once '../../includes/auth.php';
require_once '../../includes/subscription.php';
require_once '../../includes/functions.php';

initSecureSession();
requireAuth();
requireSubscription();

$userId = $_SESSION['user_id'];
$staffId = intval($_GET['id'] ?? 0);

// Get staff user
$staff = db_fetch_one("SELECT * FROM staff_users WHERE id = ? AND owner_id = ?", 'ii', [$staffId, $userId]);
if (!$staff) {
    setFlashMessage('Staff user not found', 'error');
    header('Location: index.php');
    exit;
}

// Delete staff user (permissions will be deleted by cascade)
if (db_execute("DELETE FROM staff_users WHERE id = ? AND owner_id = ?", 'ii', [$staffId, $userId])) {
    logActivity('staff_deleted', "Deleted staff user: {$staff['full_name']}");
    setFlashMessage('Staff user deleted successfully', 'success');
} else {
    setFlashMessage('Failed to delete staff user', 'error');
}

header('Location: index.php');
exit;
