<?php
/**
 * whp-accordion.php — reusable, accessible Bootstrap-5 accordion helper.
 *
 * Turns a list of sections into a themed, keyboard- and screen-reader-friendly
 * accordion so multi-section pages can collapse content consistently.
 *
 * Usage:
 *   require_once('/docker/whp/web/includes/whp-accordion.php');
 *   echo whp_accordion('server-settings', [
 *       ['title' => 'Network & SSL', 'body' => $networkHtml, 'open' => true],
 *       ['title' => 'Nameservers',   'body' => $nsHtml],
 *       ['title' => 'Mail',          'body' => $mailHtml],
 *   ]);
 *
 * Section keys:
 *   title (string, required)  — escaped header text
 *   body  (string, required)  — section HTML (TRUSTED page markup; not escaped)
 *   open  (bool, optional)    — expanded on load (default false)
 *   id    (string, optional)  — explicit collapse id (else derived)
 *
 * $opts:
 *   always_open (bool) — allow multiple sections open at once (omits data-bs-parent)
 *   flush       (bool) — edge-to-edge style (accordion-flush)
 *
 * Bootstrap's bundle handles the collapse behavior; no page JS needed.
 */

if (!function_exists('whp_accordion')) {

    function whp_accordion(string $id, array $sections, array $opts = []): string {
        $accId = whp_accordion_slug($id);
        $alwaysOpen = !empty($opts['always_open']);
        $classes = 'accordion' . (!empty($opts['flush']) ? ' accordion-flush' : '');

        $html = '<div class="' . $classes . '" id="' . htmlspecialchars($accId) . '">';
        $i = 0;
        foreach ($sections as $s) {
            if (!is_array($s) || !isset($s['title'])) { continue; }
            $i++;
            $secId = !empty($s['id'])
                ? whp_accordion_slug($s['id'])
                : $accId . '-sec-' . $i;
            $headId = $secId . '-head';
            $open = !empty($s['open']);
            $title = htmlspecialchars((string) $s['title']);
            $body = (string) ($s['body'] ?? '');
            $parentAttr = $alwaysOpen ? '' : ' data-bs-parent="#' . htmlspecialchars($accId) . '"';

            $html .= '<div class="accordion-item">'
                . '<h2 class="accordion-header" id="' . htmlspecialchars($headId) . '">'
                . '<button class="accordion-button' . ($open ? '' : ' collapsed') . '" type="button"'
                . ' data-bs-toggle="collapse" data-bs-target="#' . htmlspecialchars($secId) . '"'
                . ' aria-expanded="' . ($open ? 'true' : 'false') . '"'
                . ' aria-controls="' . htmlspecialchars($secId) . '">'
                . $title
                . '</button></h2>'
                . '<div id="' . htmlspecialchars($secId) . '" class="accordion-collapse collapse' . ($open ? ' show' : '') . '"'
                . ' aria-labelledby="' . htmlspecialchars($headId) . '"' . $parentAttr . '>'
                . '<div class="accordion-body">' . $body . '</div>'
                . '</div></div>';
        }
        $html .= '</div>';
        return $html;
    }

    /** Derive a safe DOM id fragment from an arbitrary string. */
    function whp_accordion_slug(string $s): string {
        $s = strtolower(preg_replace('/[^a-zA-Z0-9_-]+/', '-', $s));
        $s = trim($s, '-');
        return $s !== '' ? $s : 'acc';
    }
}
