<?php
require("../includes/config.php");
header("Content-Type: application/json");

// Read the JSON data
$data = json_decode(file_get_contents("php://input"), true);

// Get IDs using the exact keys from the JS above
$id = isset($data['lesson_id']) ? (int)$data['lesson_id'] : 0;
$status = isset($data['target_status']) ? (int)$data['target_status'] : 0;

if ($id <= 0) {
    echo json_encode(["status" => "error", "message" => "Invalid Lesson ID"]);
    exit;
}

try {
    // Execute the update
    $stmt = $pdo->prepare("UPDATE lessons SET status = ? WHERE id = ?");
    $stmt->execute([$status, $id]);

    // Check if the update actually happened
    if ($stmt->rowCount() >= 0) { 
        // rowCount() might be 0 if you set status to 1 when it's already 1
        // but the query still "succeeded"
        echo json_encode([
            "status" => "success",
            "message" => "Status successfully " . ($status == 1 ? "Activated" : "Deactivated")
        ]);
    } else {
        echo json_encode(["status" => "error", "message" => "Database update failed"]);
    }
} catch (PDOException $e) {
    echo json_encode(["status" => "error", "message" => "DB Error: " . $e->getMessage()]);
}
?>