<?php
require_once 'config/database.php';
require_once 'utils/helpers.php';

if ($_SERVER['REQUEST_METHOD'] !== 'DELETE') {
    Helpers::sendResponse(false, 'Method not allowed', null, 405);
}

// Get and verify token
$token = Helpers::getBearerToken();
$userData = Helpers::verifyJWT($token);

if (!$userData || $userData['role'] !== 'admin') {
    Helpers::sendResponse(false, 'Unauthorized. Admin access required.', null, 403);
}

$serviceId = isset($_GET['service_id']) ? $_GET['service_id'] : null;

if (!$serviceId) {
    Helpers::sendResponse(false, 'Service ID is required', null, 400);
}

$database = new Database();
$db = $database->getConnection();

// Soft delete by setting is_active to FALSE
$query = "UPDATE services SET is_active = FALSE WHERE id = :service_id";
$stmt = $db->prepare($query);
$stmt->bindParam(':service_id', $serviceId);

if ($stmt->execute()) {
    // Log admin activity
    $query = "INSERT INTO admin_logs (admin_id, action, target_type, target_id, details) 
              VALUES (:admin_id, 'delete_service', 'service', :service_id, NULL)";

    $stmt = $db->prepare($query);
    $stmt->bindParam(':admin_id', $userData['user_id']);
    $stmt->bindParam(':service_id', $serviceId);
    $stmt->execute();

    Helpers::sendResponse(true, 'Service deleted successfully');
} else {
    Helpers::sendResponse(false, 'Service deletion failed', null, 500);
}
