<?php
include('../include/headscript.php');
include('../class/company.php');
$conn = new mysqli($servername, $username, $password, $database);

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $idlab = $_POST['idlab'];

    // Generate new API Key and Secret Key
    $new_api_key = bin2hex(random_bytes(16));
    $new_api_secret = bin2hex(random_bytes(16));

    // Hash secret key before saving
    $hashed_secret = password_hash($new_api_secret, PASSWORD_BCRYPT);

    $stmt = $conn->prepare("UPDATE laboratories SET api_key = ?, api_secret = ? WHERE idlab = ?");
    $stmt->bind_param("ssi", $new_api_key, $hashed_secret, $idlab);

    if ($stmt->execute()) {
        // Show success message and display the new keys
        echo json_encode(["status" => "success", "message" => "Keys regenerated successfully.", "api_key" => $new_api_key, "api_secret" => $new_api_secret]);
    } else {
        echo json_encode(["status" => "error", "message" => "Failed to regenerate keys."]);
    }
    $stmt->close();
}
$conn->close();
