<?php
session_start();
include '../session.php';

if (empty($_POST['key_id'])) {
    echo json_encode(['success' => false, 'message' => 'Missing key ID']);
    exit;
}

$key_id = intval($_POST['key_id']);

// Verify key belongs to user
$verify_sql = "SELECT id FROM user_api_keys WHERE id = '$key_id' AND user_id = '$user_id'";
$verify_result = mysqli_query($conn, $verify_sql);

if (!$verify_result || mysqli_num_rows($verify_result) === 0) {
    echo json_encode(['success' => false, 'message' => 'API key not found']);
    exit;
}

// Revoke key
$update_sql = "UPDATE user_api_keys SET status = 'revoked', revoked_at = NOW() 
               WHERE id = '$key_id' AND user_id = '$user_id'";

if (mysqli_query($conn, $update_sql)) {
    echo json_encode(['success' => true, 'message' => 'API key revoked successfully']);
} else {
    echo json_encode(['success' => false, 'message' => 'Failed to revoke API key: ' . mysqli_error($conn)]);
}
?>