<?php ob_start();
require_once '../files/guardiankey.php'; 

ini_set('display_errors', 0);
ini_set('log_errors', 1);
ini_set('error_log', __DIR__ . '/../logs/php_errors.log');
error_reporting(E_ALL);

function syncClientServices(PDO $pdo, int $clientId, array $newServiceIds): bool
{
    try {
        $stmtDelete = $pdo->prepare("DELETE FROM client_services WHERE client_id = ?");
        $stmtDelete->execute([$clientId]);

        if (!empty($newServiceIds)) {
            $stmtInsert = $pdo->prepare("INSERT INTO client_services (client_id, service_id) VALUES (?, ?)");
            foreach ($newServiceIds as $serviceId) {
                if (filter_var($serviceId, FILTER_VALIDATE_INT) !== false) {
                    $stmtInsert->execute([$clientId, (int)$serviceId]);
                } else {
                    error_log("ID de servicio inválido detectado durante syncClientServices para cliente ID " . $clientId . ": " . $serviceId);
                }
            }
        }
        return true; 
    } catch (PDOException $e) {
        error_log("Error de PDO al sincronizar servicios para cliente ID " . $clientId . ": " . $e->getMessage());
        return false; 
    } catch (Exception $e) {
        error_log("Error general al sincronizar servicios para cliente ID " . $clientId . ": " . $e->getMessage());
        return false; 
    }
}
?>