<?php $pageTitle = t('oidc_settings_title'); ?>

<div class="page-header">
    <div class="page-header-left">
        <a href="/admin/plugins" class="btn btn-ghost btn-sm">← <?= t('plugins') ?></a>
        <h1 class="page-title"><?= t('oidc_settings_title') ?></h1>
    </div>
</div>

<?php if ($success): ?><div class="alert alert-success"><?= Helper::e($success) ?></div><?php endif; ?>
<?php if ($error):   ?><div class="alert alert-error"><?= Helper::e($error) ?></div><?php endif; ?>
<?php if (!empty($errors)): ?>
<div class="alert alert-error"><?php foreach ($errors as $e): ?><div><?= Helper::e($e) ?></div><?php endforeach; ?></div>
<?php endif; ?>

<div class="settings-grid">
<div class="settings-col">

    <!-- Global toggle + redirect URI hint -->
    <form method="POST">
        <?= Helper::csrf() ?>
        <input type="hidden" name="action" value="save">
        <div class="card">
            <div class="card-header"><h2 class="card-title"><?= t('oidc_global_section') ?></h2></div>
            <div class="card-body">
                <div class="form-group">
                    <label class="checkbox-label">
                        <input type="checkbox" name="oidc_enabled" value="1" <?= $cfg['enabled'] ? 'checked' : '' ?>>
                        <?= t('oidc_enable') ?>
                    </label>
                    <p class="form-hint"><?= t('oidc_enable_hint') ?></p>
                </div>
                <div class="form-group">
                    <label class="form-label"><?= t('oidc_redirect_uri_label') ?></label>
                    <input type="text" class="form-input" value="<?= Helper::e($cfg['redirect_uri']) ?>" readonly data-select-on-focus>
                    <p class="form-hint"><?= t('oidc_redirect_uri_hint') ?></p>
                </div>
                <div class="form-group">
                    <label class="checkbox-label">
                        <input type="checkbox" name="oidc_takeover_existing_users" value="1" <?= $cfg['takeover'] ? 'checked' : '' ?>>
                        <?= t('oidc_takeover_existing_users') ?>
                    </label>
                    <p class="form-hint"><?= t('oidc_takeover_existing_users_hint') ?></p>
                </div>
                <button type="submit" class="btn btn-primary"><?= t('save') ?></button>
            </div>
        </div>
    </form>

    <!-- Existing providers -->
    <?php foreach ($cfg['providers'] as $p): ?>
    <form method="POST" class="card" style="margin-top:16px">
        <?= Helper::csrf() ?>
        <input type="hidden" name="action" value="update">
        <input type="hidden" name="id" value="<?= Helper::e($p['id']) ?>">
        <div class="card-header" style="display:flex;align-items:center;justify-content:space-between">
            <h2 class="card-title">
                <?= Helper::e($p['label']) ?>
                <span class="badge badge-neutral" style="font-weight:500;margin-left:6px"><?= Helper::e($p['kind'] ?? 'custom') ?></span>
                <?php if (empty($p['enabled'])): ?>
                <span class="badge badge-neutral" style="font-weight:500;margin-left:6px"><?= t('plugin_disabled') ?></span>
                <?php endif; ?>
            </h2>
        </div>
        <div class="card-body">
            <div class="form-group">
                <label class="checkbox-label">
                    <input type="checkbox" name="enabled" value="1" <?= !empty($p['enabled']) ? 'checked' : '' ?>>
                    <?= t('oidc_provider_enabled') ?>
                </label>
            </div>
            <div class="form-group">
                <label class="form-label"><?= t('oidc_provider_label') ?></label>
                <input type="text" name="label" class="form-input" value="<?= Helper::e($p['label']) ?>">
            </div>
            <div class="form-group">
                <label class="form-label"><?= t('oidc_discovery_url') ?></label>
                <input type="text" name="discovery_url" class="form-input" value="<?= Helper::e($p['discovery_url']) ?>">
            </div>
            <div class="form-row">
                <div class="form-group flex-1">
                    <label class="form-label"><?= t('oidc_client_id') ?></label>
                    <input type="text" name="client_id" class="form-input" value="<?= Helper::e($p['client_id']) ?>">
                </div>
                <div class="form-group flex-1">
                    <label class="form-label"><?= t('oidc_client_secret') ?></label>
                    <input type="password" name="client_secret" class="form-input" autocomplete="new-password" placeholder="<?= !empty($p['has_secret']) ? t('oidc_secret_set_keep') : '' ?>">
                </div>
            </div>
            <div class="form-group">
                <label class="form-label"><?= t('oidc_scopes') ?></label>
                <input type="text" name="scopes" class="form-input" value="<?= Helper::e($p['scopes']) ?>" placeholder="openid email profile">
            </div>
            <div style="display:flex;gap:8px">
                <button type="submit" class="btn btn-primary btn-sm"><?= t('save') ?></button>
                <button type="submit" formaction="" name="action" value="refresh" class="btn btn-ghost btn-sm"><?= t('oidc_refresh_discovery') ?></button>
                <button type="submit" formaction="" name="action" value="remove" class="btn btn-ghost btn-sm danger" data-confirm-click="<?= Helper::e(t('confirm_delete')) ?>"><?= t('delete') ?></button>
            </div>
        </div>
    </form>
    <?php endforeach; ?>

</div>

<div class="settings-col">

    <!-- Add provider -->
    <form method="POST" class="card">
        <?= Helper::csrf() ?>
        <input type="hidden" name="action" value="add">
        <div class="card-header"><h2 class="card-title"><?= t('oidc_add_provider') ?></h2></div>
        <div class="card-body">
            <div class="form-group">
                <label class="form-label"><?= t('oidc_kind') ?></label>
                <select name="kind" class="form-select" id="oidcKind">
                    <option value="keycloak"><?= t('oidc_kind_keycloak') ?></option>
                    <option value="google"><?= t('oidc_kind_google') ?></option>
                    <option value="microsoft"><?= t('oidc_kind_microsoft') ?></option>
                    <option value="custom"><?= t('oidc_kind_custom') ?></option>
                </select>
            </div>
            <div class="form-group">
                <label class="form-label"><?= t('oidc_kind_input_label') ?></label>
                <input type="text" name="kind_input" class="form-input" id="oidcKindInput" placeholder="https://keycloak.example.com/realms/plenvo">
                <p class="form-hint"><?= t('oidc_kind_input_hint') ?></p>
            </div>
            <div class="form-group">
                <label class="form-label"><?= t('oidc_provider_id') ?></label>
                <input type="text" name="id" class="form-input" placeholder="keycloak1" pattern="[a-z0-9_-]+" required>
                <p class="form-hint"><?= t('oidc_provider_id_hint') ?></p>
            </div>
            <div class="form-group">
                <label class="form-label"><?= t('oidc_provider_label') ?></label>
                <input type="text" name="label" class="form-input" placeholder="Sign in with Keycloak">
            </div>
            <div class="form-group">
                <label class="form-label"><?= t('oidc_discovery_url') ?> <?= t('oidc_optional') ?></label>
                <input type="text" name="discovery_url" class="form-input" placeholder="https://...">
                <p class="form-hint"><?= t('oidc_discovery_url_hint') ?></p>
            </div>
            <div class="form-row">
                <div class="form-group flex-1">
                    <label class="form-label"><?= t('oidc_client_id') ?></label>
                    <input type="text" name="client_id" class="form-input" required>
                </div>
                <div class="form-group flex-1">
                    <label class="form-label"><?= t('oidc_client_secret') ?></label>
                    <input type="password" name="client_secret" class="form-input" autocomplete="new-password" required>
                </div>
            </div>
            <div class="form-group">
                <label class="form-label"><?= t('oidc_scopes') ?></label>
                <input type="text" name="scopes" class="form-input" value="openid email profile">
            </div>
            <div class="form-group">
                <label class="checkbox-label">
                    <input type="checkbox" name="enabled" value="1" checked>
                    <?= t('oidc_provider_enabled') ?>
                </label>
            </div>
            <button type="submit" class="btn btn-primary"><?= t('oidc_add_provider') ?></button>
        </div>
    </form>

</div>
</div>
