<?php
header('Content-Type: application/json; charset=utf-8');

include 'connection.php';
include 'decrypt_and_validate.php';   // for API key validation

$response = [];

try {
    $api_key = $_POST['API_Key'] ?? '';

    // Validate API key
    $validation = validateApiKey($api_key);
    if (!$validation['status']) {
        throw new Exception($validation['message']);
    }

    // Fetch all Bond_Number values
    $stmt = $conn->prepare("SELECT Bond_Number FROM Last_Draw_Resault");
    $stmt->execute();
    $result = $stmt->get_result();

    $bondNumbers = [];
    while ($row = $result->fetch_assoc()) {
        $bondNumbers[] = $row['Bond_Number'];
    }
    $stmt->close();

    if (!empty($bondNumbers)) {
        $response = ["status" => "success", "bond_numbers" => $bondNumbers];
    } else {
        throw new Exception("No bond numbers found.");
    }

} catch (Exception $e) {
    $response = ["status" => "error", "message" => $e->getMessage()];
} finally {
    $conn->close();
}

echo json_encode($response);
?>
