<?php
session_start();
$pageSequence = $_SESSION['sequence'];
$toDiv = "contentZone" . $pageSequence;
$resultZoneDiv = "resultZone" . $pageSequence;
$tableZone = "#tableZone" . $pageSequence;
$_SESSION['sequence']++;

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

include($_SESSION['basedir'] . "core.php");

$campaignId = isset($_POST['campaignId']) ? intval($_POST['campaignId']) : 0;
$newStatus = isset($_POST['status']) ? intval($_POST['status']) : -99;

// Autoriser uniquement les transitions valides
if ($campaignId <= 0 || !in_array($newStatus, [4, 6])) {
    echo json_encode([
        "errorCode" => 1,
        "errorMessage" => "Paramètres invalides."
    ]);
    exit;
}

$success = $dba->updateSmsCampaignStatus($campaignId, $newStatus);

if ($success) {
    $message = ($newStatus === 6) ? "Campagne démarrée avec succès." : "Campagne mise en pause.";
    echo json_encode([
        "errorCode" => 0,
        "errorMessage" => $message
    ]);
} else {
    echo json_encode([
        "errorCode" => 2,
        "errorMessage" => "Erreur lors de la mise à jour du statut."
    ]);
}
