<?php
$pageTitle = 'System Settings';
$breadcrumb = [['label' => 'Admin', 'url' => null], ['label' => 'System Settings', 'url' => null]];
ob_start();
?>
<div class="page-header">
    <div>
        <h1 class="page-title">System Settings</h1>
        <p class="page-subtitle">Platform-wide configuration (super admin only)</p>
    </div>
</div>

<div class="card">
    <div class="card-header">
        <h3 class="card-title">Platform Configuration</h3>
    </div>
    <div class="card-body">
        <form method="POST" action="<?= APP_URL ?>/admin/system">
            <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($_SESSION[CSRF_TOKEN_NAME] ?? '') ?>">

            <div class="form-group">
                <label class="form-label">Application Name</label>
                <input type="text" name="app_name" class="form-control" value="<?= htmlspecialchars($settings['app_name'] ?? APP_NAME) ?>">
            </div>
            <div class="form-group">
                <label class="form-label">Platform Fee Rate (decimal, e.g. 0.005 = 0.5%)</label>
                <input type="number" step="0.0001" name="platform_fee_rate" class="form-control" value="<?= htmlspecialchars($settings['platform_fee_rate'] ?? PLATFORM_FEE_RATE) ?>">
            </div>
            <div class="form-group">
                <label class="form-label">Default Locale</label>
                <select name="default_locale" class="form-control form-select">
                    <?php foreach (getLocales() as $code): ?>
                    <option value="<?= htmlspecialchars($code) ?>" <?= ($settings['default_locale'] ?? DEFAULT_LOCALE) === $code ? 'selected' : '' ?>><?= htmlspecialchars($code) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="form-group">
                <label class="form-label">Default Currency</label>
                <select name="default_currency" class="form-control form-select">
                    <?php foreach (getCurrencies() as $code => $symbol): ?>
                    <option value="<?= htmlspecialchars($code) ?>" <?= ($settings['default_currency'] ?? DEFAULT_CURRENCY) === $code ? 'selected' : '' ?>><?= htmlspecialchars($symbol . ' ' . $code) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="form-group">
                <label class="form-label">Default Timezone</label>
                <input type="text" name="default_timezone" class="form-control" value="<?= htmlspecialchars($settings['default_timezone'] ?? DEFAULT_TIMEZONE) ?>">
            </div>
            <div class="form-group">
                <label style="display:flex;align-items:center;gap:8px;">
                    <input type="checkbox" name="maintenance_mode" value="1" <?= !empty($settings['maintenance_mode']) ? 'checked' : '' ?>>
                    Maintenance Mode
                </label>
            </div>
            <div class="form-group">
                <label style="display:flex;align-items:center;gap:8px;">
                    <input type="checkbox" name="registration_open" value="1" <?= !empty($settings['registration_open']) ? 'checked' : '' ?>>
                    Allow New Organization Registration
                </label>
            </div>

            <button type="submit" class="btn btn-primary">Save Settings</button>
        </form>
    </div>
</div>

<div class="card" style="margin-top:24px;">
    <div class="card-header">
        <h3 class="card-title">Trial &amp; Payment Integration</h3>
    </div>
    <div class="card-body">
        <form method="POST" action="<?= APP_URL ?>/admin/system">
            <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($_SESSION[CSRF_TOKEN_NAME] ?? '') ?>">

            <p class="field-hint" style="margin-bottom:16px;">New organizations get a 7-day free trial. When it ends, they're sent to a payment page powered by Flutterwave, priced by the currency their location resolves to (via ipinfo.io) — see the per-plan &quot;Pricing&quot; button above.</p>

            <div class="form-group">
                <label class="form-label">Flutterwave Public Key</label>
                <input type="text" name="flw_public_key" class="form-control" placeholder="FLWPUBK-..." value="<?= htmlspecialchars($settings['flw_public_key'] ?? '') ?>">
            </div>
            <div class="form-group">
                <label class="form-label">Flutterwave Secret Key</label>
                <input type="password" name="flw_secret_key" class="form-control" placeholder="<?= !empty($settings['flw_secret_key']) ? '••••••••  (leave blank to keep current)' : 'FLWSECK-...' ?>" autocomplete="off">
            </div>
            <div class="form-group">
                <label class="form-label">Flutterwave Webhook Hash</label>
                <input type="password" name="flw_webhook_hash" class="form-control" placeholder="<?= !empty($settings['flw_webhook_hash']) ? '••••••••  (leave blank to keep current)' : 'Set this same value as the Secret Hash in your Flutterwave dashboard webhook settings' ?>" autocomplete="off">
                <p class="field-hint">Used to verify webhook calls from Flutterwave really came from Flutterwave. Set the same value in your Flutterwave dashboard under Settings &gt; Webhooks.</p>
            </div>
            <div class="form-group">
                <label class="form-label">ipinfo.io API Token</label>
                <input type="password" name="ipinfo_token" class="form-control" placeholder="<?= !empty($settings['ipinfo_token']) ? '••••••••  (leave blank to keep current)' : 'Get a free token at ipinfo.io' ?>" autocomplete="off">
                <p class="field-hint">Used to detect a visitor's country from their IP for currency pricing. Without a token, everyone is priced in the default currency above.</p>
            </div>

            <button type="submit" class="btn btn-primary">Save Integration Settings</button>
        </form>
    </div>
</div>

<div class="card" style="margin-top:24px;">
    <div class="card-header">
        <h3 class="card-title">Subscription Plans</h3>
        <a href="<?= APP_URL ?>/admin/plans" class="btn btn-sm btn-secondary">Manage Plans</a>
    </div>
    <div class="table-responsive">
        <table class="table">
            <thead><tr><th>Name</th><th>Price</th><th>Billing</th><th>Status</th></tr></thead>
            <tbody>
                <?php foreach ($plans ?? [] as $plan): ?>
                <tr>
                    <td><?= htmlspecialchars($plan['name']) ?></td>
                    <td><?= formatCurrency((float)$plan['price']) ?></td>
                    <td><?= htmlspecialchars(ucfirst($plan['billing_type'])) ?></td>
                    <td><span class="badge badge-<?= $plan['is_active'] ? 'active' : 'secondary' ?>"><?= $plan['is_active'] ? 'Active' : 'Inactive' ?></span></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
<?php
$content = ob_get_clean();
require ROOT_PATH . '/modules/shared/views/layouts/main.php';
?>
