<?php

declare(strict_types=1);

$result = is_array($result ?? null) ? $result : [];
$media = is_array($result['media'] ?? null) ? $result['media'] : [];
$image = is_array($media['image'] ?? null) ? $media['image'] : [];
$imageUrl = trim((string) ($image['url'] ?? ''));
$imageFileId = trim((string) ($image['fileId'] ?? $image['file_id'] ?? ''));
if ($imageUrl === '' && $imageFileId !== '') {
    $imageUrl = '/media/files/stream?id=' . rawurlencode($imageFileId);
}
$imageAlt = trim((string) ($image['alt'] ?? ($result['title'] ?? 'Quiz result')));
$archetype = trim((string) ($media['archetype'] ?? ''));
$taxName = trim((string) ($media['taxName'] ?? $media['tax_name'] ?? ''));
$immediateAction = trim((string) ($media['immediateAction'] ?? $media['immediate_action'] ?? ''));
$categoryRanking = is_array($result['categoryRanking'] ?? null) ? array_values($result['categoryRanking']) : (is_array($result['category_ranking'] ?? null) ? array_values($result['category_ranking']) : []);
$categoryCopy = is_array($result['categoryCopy'] ?? null) ? $result['categoryCopy'] : (is_array($result['category_copy'] ?? null) ? $result['category_copy'] : []);
$challengesShown = max(0, (int) ($result['challengesShown'] ?? $result['challenges_shown'] ?? 0));
$challengeRows = [];
foreach ($categoryRanking as $entry) {
    if (!is_array($entry)) {
        continue;
    }
    $slug = trim((string) ($entry['slug'] ?? ''));
    if ($slug === '') {
        continue;
    }
    $copy = is_array($categoryCopy[$slug] ?? null) ? $categoryCopy[$slug] : [];
    $name = trim((string) ($copy['name'] ?? $slug));
    $challenge = trim((string) ($copy['challenge'] ?? ''));
    if ($name === '' && $challenge === '') {
        continue;
    }
    $challengeRows[] = ['name' => $name, 'challenge' => $challenge];
    if ($challengesShown > 0 && count($challengeRows) >= $challengesShown) {
        break;
    }
}
?>
<section class="myron-quiz__result">
  <?php if ($imageUrl !== ''): ?>
    <figure class="myron-quiz__result-hero">
      <img class="myron-quiz__result-image" src="<?= myronEscape($imageUrl) ?>" alt="<?= myronEscape($imageAlt) ?>" loading="lazy" decoding="async">
    </figure>
  <?php endif; ?>
  <h2 class="myron-quiz__result-title"><?= myronEscape((string) ($result['title'] ?? 'Your Result')) ?></h2>
  <?php if ($archetype !== ''): ?>
    <p class="myron-quiz__result-archetype"><?= myronEscape($archetype) ?></p>
  <?php endif; ?>
  <?php if ($taxName !== ''): ?>
    <p class="myron-quiz__result-tax"><?= myronEscape($taxName) ?></p>
  <?php endif; ?>
  <div class="myron-quiz__result-copy"><?= nl2br(myronEscape((string) ($result['description'] ?? ''))) ?></div>
  <?php if ($immediateAction !== ''): ?>
    <div class="myron-quiz__result-action"><?= nl2br(myronEscape($immediateAction)) ?></div>
  <?php endif; ?>
  <?php if ($challengeRows !== []): ?>
    <div class="myron-quiz__challenges">
      <?php foreach ($challengeRows as $challengeRow): ?>
        <article class="myron-quiz__challenge">
          <h3 class="myron-quiz__challenge-name"><?= myronEscape($challengeRow['name']) ?></h3>
          <?php if ($challengeRow['challenge'] !== ''): ?>
            <p class="myron-quiz__challenge-copy"><?= myronEscape($challengeRow['challenge']) ?></p>
          <?php endif; ?>
        </article>
      <?php endforeach; ?>
    </div>
  <?php endif; ?>
  <?php if (trim((string) ($result['cta_url'] ?? '')) !== ''): ?>
    <a class="myron-quiz__button" href="<?= myronEscape((string) $result['cta_url']) ?>"><?= myronEscape((string) (($result['cta_text'] ?? '') ?: 'Continue')) ?></a>
  <?php endif; ?>
</section>
