<?php

declare(strict_types=1);

$quizData = is_array($quizData ?? null) ? $quizData : (is_array($quiz ?? null) ? $quiz : []);
$items = is_array($quizData['items'] ?? null) ? array_values($quizData['items']) : [];
$resultPayload = is_array($resultPayload ?? null) ? $resultPayload : ['locked' => false, 'result' => null, 'teaser' => null, 'mode' => 'open'];
$quizFormExtrasHtml = is_string($quizFormExtrasHtml ?? null) ? $quizFormExtrasHtml : '';
$submittedResponsesByItem = is_array($submittedResponsesByItem ?? null) ? $submittedResponsesByItem : [];
$quizSubmissionEndpoint = is_string($quizSubmissionEndpoint ?? null) ? $quizSubmissionEndpoint : '/api/v1/content/quizzes/' . rawurlencode((string) ($quizData['slug'] ?? '')) . '/attempts';
?>
<div class="myron-quiz myron-quiz--classic-form" data-quiz-mode="classic-form">
  <header class="myron-quiz__header"><h2 class="myron-quiz__title"><?= myronEscape((string) ($quizData['title'] ?? 'Quiz')) ?></h2></header>
  <form class="myron-quiz__form" method="post" action="<?= myronEscape($quizSubmissionEndpoint) ?>" data-quiz-player data-quiz-endpoint="<?= myronEscape($quizSubmissionEndpoint) ?>">
    <?= $quizFormExtrasHtml ?>
    <?php foreach ($items as $item): ?>
      <?php require __DIR__ . '/partials/item.php'; ?>
    <?php endforeach; ?>
    <button class="myron-quiz__button" type="submit">Submit</button>
  </form>
  <?php
    if (!empty($resultPayload['locked']) && ($resultPayload['mode'] ?? '') === 'hard_gate') {
        require __DIR__ . '/partials/hard-gate.php';
    } elseif (!empty($resultPayload['locked'])) {
        $teaser = is_array($resultPayload['teaser'] ?? null) ? $resultPayload['teaser'] : [];
        require __DIR__ . '/partials/result-gate.php';
    } elseif (is_array($resultPayload['result'] ?? null)) {
        $result = $resultPayload['result'];
        require __DIR__ . '/partials/result.php';
    }
  ?>
</div>
