<?php
include "../config/database.php";
//$testInvitationId = 11577458; // Replace with dynamic ID from your DB
$testInvitationId = $_GET['test_invitation_id'];


$apiUrl = "https://apiv3.imocha.io/v3/reports/{$testInvitationId}/pdf";

$ch = curl_init($apiUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    "x-api-key: $iMocha_apiKey"
]);

$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

//if ($httpCode === 200) {
//    $result = json_decode($response, true);
//    $pdfUrl = $result['pdfReport'];
//
////    echo "<a href='{$pdfUrl}' target='_blank'>Download iMocha Test Report</a>";
////} else {
////    echo "Failed to get report. HTTP Code: $httpCode<br>";
////    echo "Response: $response";
////}
if ($httpCode === 200) {
    $result = json_decode($response, true);
    $pdfUrl = $result['pdfReport'];
    $viewUrl = "https://docs.google.com/gview?embedded=true&url=" . urlencode($pdfUrl);
    echo "<iframe src='{$viewUrl}' width='100%' height='600px'></iframe>";
} else {
    echo "Failed to get report. HTTP Code: $httpCode<br>";
    echo "Response: $response";
}
?>