<h2 class="mb-4"><i class="bi bi-plug"></i> Plugin Configuratie: <?= htmlspecialchars($pluginName) ?></h2>

<form method="post" class="card shadow-sm">
    <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrf) ?>">

    <div class="card-body">
        <?php if (empty($pluginConfig)): ?>
            <div class="alert alert-info">Deze plugin heeft geen configureerbare instellingen.</div>
        <?php else: ?>
            <?php foreach ($pluginConfig as $key => $value): ?>
                <?= renderConfigField($key, $value) ?>
            <?php endforeach; ?>
        <?php endif; ?>
    </div>

    <?php if (!empty($pluginConfig)): ?>
        <div class="card-footer text-end">
            <a href="/admin/plugins" class="btn btn-secondary">Annuleren</a>
            <button type="submit" class="btn btn-primary"><i class="bi bi-check-lg"></i> Opslaan</button>
        </div>
    <?php endif; ?>
</form>

<div class="mt-3">
    <a href="/admin/plugins" class="btn btn-outline-secondary btn-sm">
        <i class="bi bi-arrow-left"></i> Terug naar plugins
    </a>
</div>

<?php
function renderConfigField(string $key, $value, string $prefix = ''): string
{
    $name = $prefix ? $prefix . '[' . $key . ']' : 'config[' . $key . ']';
    $id = 'cfg_' . str_replace(['.', '['], '_', rtrim($name, ']'));
    $label = ucwords(str_replace('_', ' ', $key));
    $html = '';

    if (is_bool($value)) {
        $checked = $value ? 'checked' : '';
        $html .= '<div class="mb-3 form-check form-switch">';
        $html .= '<input type="hidden" name="' . htmlspecialchars($name) . '" value="0">';
        $html .= '<input class="form-check-input" type="checkbox" id="' . htmlspecialchars($id) . '" name="' . htmlspecialchars($name) . '" value="1" ' . $checked . '>';
        $html .= '<label class="form-check-label" for="' . htmlspecialchars($id) . '">' . htmlspecialchars($label) . '</label>';
        $html .= '</div>';
    } elseif (is_numeric($value)) {
        $step = is_float($value) ? 'step="0.01"' : 'step="1"';
        $html .= '<div class="mb-3">';
        $html .= '<label for="' . htmlspecialchars($id) . '" class="form-label">' . htmlspecialchars($label) . '</label>';
        $html .= '<input type="number" class="form-control" id="' . htmlspecialchars($id) . '" name="' . htmlspecialchars($name) . '" value="' . htmlspecialchars($value) . '" ' . $step . '>';
        $html .= '</div>';
    } elseif (is_array($value)) {
        $html .= '<div class="mb-3">';
        $html .= '<label class="form-label fw-bold">' . htmlspecialchars($label) . '</label>';
        $html .= '<div class="card bg-light">';
        $html .= '<div class="card-body">';
        foreach ($value as $subKey => $subValue) {
            $html .= renderConfigField($subKey, $subValue, $name);
        }
        $html .= '</div></div></div>';
    } else {
        $html .= '<div class="mb-3">';
        $html .= '<label for="' . htmlspecialchars($id) . '" class="form-label">' . htmlspecialchars($label) . '</label>';

        if (strlen($value) > 80 || str_contains($value, "\n")) {
            $html .= '<textarea class="form-control font-monospace" id="' . htmlspecialchars($id) . '" name="' . htmlspecialchars($name) . '" rows="4">' . htmlspecialchars($value) . '</textarea>';
        } else {
            $html .= '<input type="text" class="form-control" id="' . htmlspecialchars($id) . '" name="' . htmlspecialchars($name) . '" value="' . htmlspecialchars($value) . '">';
        }

        $html .= '</div>';
    }

    return $html;
}
