<?php
if (session_status() == PHP_SESSION_NONE) {
    session_start();
}
require_once __DIR__ . '/../includes/db.php';
require_once __DIR__ . '/../includes/auth.php';

check_role('administrador');

// Only allow POST requests
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    http_response_code(405);
    exit('Method not allowed');
}

$data = json_decode(file_get_contents('php://input'), true);

if (!$data || !isset($data['job_id']) || !isset($data['status'])) {
    http_response_code(400);
    exit('Invalid data');
}

$job_id = (int)$data['job_id'];
$status = $data['status'];

if (!in_array($status, ['active', 'inactive', 'filled'])) {
    http_response_code(400);
    exit('Invalid status');
}

// Update job status
$stmt = $pdo->prepare("UPDATE jobs SET status = ? WHERE id = ?");
$result = $stmt->execute([$status, $job_id]);

if ($result) {
    echo json_encode(['success' => true]);
} else {
    http_response_code(500);
    echo json_encode(['success' => false, 'error' => 'Database update failed']);
}
?>