<?php
// Stands in for Ivalua/Docebo's
//    /learn/v1/filerepo/getCourseFile?file_id=..&course_id=..
//
// In the native capture this is the last request the opener tab makes before
// the download: an XHR that returns the file's metadata, taking 356 ms. The
// app has already called window.open('') by then; it uses this response to
// build the download URL and only then sets the blank tab's location. That
// gap is the interval the test needs to be able to vary.
//
//   ?delay_ms=<ms>   how long to take. Default 356, the value measured in
//                    ivalua.docebosaas.com_mac_native.har entry 257.

$delay_ms = isset($_GET['delay_ms']) ? intval($_GET['delay_ms']) : 356;
if ($delay_ms > 0) {
   usleep($delay_ms * 1000);
}

// Shape copied from the captured response body.
$body = json_encode(array(
   'data' => array(
      'Files' => array(
         'id_file' => 1352,
         'id_course' => 958,
         'title' => 'Create your PR (v182) - 20250523',
         'path' => 'b728dd0d1b134854bbd09276f8f1ecf5924365f5e42aab3627c7bd0d65fe3765.zip',
         'original_filename' => 'Create your PR (v182) - 20250523.zip',
         'item_type' => 5,
         'file_source' => 1,
      ),
      'collection' => 'coursedocs',
   ),
   'version' => '1.0.0',
   '_links' => array(),
));

header('HTTP/1.1 200 OK');
header('Content-Type: application/json; charset=UTF-8');
header('Cache-Control: no-cache');
header('Content-Length: ' . strlen($body));
echo $body;
?>
