<?php

declare(strict_types=1);

require_once __DIR__ . '/helpers.php';

$settingsModuleBoot = 'settings_module_boot';
$editableSettingsSchema = 'editable_settings_schema';
$settingsFormValues = 'settings_form_values';

$settingsModuleBoot();

$pageTitle = 'Settings';
$schema = $editableSettingsSchema();
$formErrors = flash('errors') ?? [];
$values = $settingsFormValues();

$fieldValue = static function (string $field, mixed $default = '') use ($values) {
    if (isset($_SESSION['_old']) && array_key_exists($field, $_SESSION['_old'])) {
        return old($field, $default);
    }

    return $values[$field] ?? $default;
};

require dirname(__DIR__, 2) . '/includes/header.php';
require dirname(__DIR__, 2) . '/includes/sidebar.php';
require dirname(__DIR__, 2) . '/includes/topbar.php';
require dirname(__DIR__, 2) . '/includes/flash.php';
?>
<section class="panel-card mb-4">
    <div class="panel-card-header mb-3">
        <div>
            <h2 class="h4 mb-0">System settings</h2>
        </div>
    </div>
    <p class="text-muted mb-0">These settings drive league branding, competition defaults, discipline thresholds, and workflow toggles across the engine.</p>
</section>

<form method="post" action="<?php echo e(base_url('app/modules/settings/actions.php')); ?>" enctype="multipart/form-data" novalidate>
    <?php echo csrf_input(); ?>
    <input type="hidden" name="action" value="update">

    <div class="row g-4">
        <?php foreach ($schema as $section): ?>
            <div class="col-12 col-xl-6">
                <section class="panel-card h-100">
                    <div class="panel-card-header mb-3">
                        <div>
                            <p class="eyebrow mb-1">Configuration</p>
                            <h3 class="h5 mb-0"><?php echo e($section['title']); ?></h3>
                        </div>
                    </div>
                    <div class="row g-3">
                        <?php foreach ($section['fields'] as $key => $meta): ?>
                            <div class="col-12 <?php echo ($meta['type'] ?? '') === 'textarea' ? '' : 'col-md-6'; ?>">
                                <label class="form-label" for="<?php echo e($key); ?>"><?php echo e($meta['label']); ?></label>
                                <?php if (($meta['type'] ?? '') === 'select'): ?>
                                    <select class="form-select <?php echo validation_error($formErrors, $key) ? 'is-invalid' : ''; ?>" id="<?php echo e($key); ?>" name="<?php echo e($key); ?>">
                                        <?php foreach (($meta['options'] ?? []) as $optionValue => $optionLabel): ?>
                                            <option value="<?php echo e((string) $optionValue); ?>" <?php echo (string) $fieldValue($key) === (string) $optionValue ? 'selected' : ''; ?>><?php echo e($optionLabel); ?></option>
                                        <?php endforeach; ?>
                                    </select>
                                <?php elseif (($meta['type'] ?? '') === 'file'): ?>
                                    <input class="form-control <?php echo validation_error($formErrors, $key) ? 'is-invalid' : ''; ?>" id="<?php echo e($key); ?>" name="<?php echo e($key); ?>" type="file" accept="<?php echo e((string) ($meta['accept'] ?? 'image/*')); ?>">
                                    <?php if ((string) $fieldValue($key) !== ''): ?>
                                        <div class="settings-logo-preview mt-3">
                                            <img src="<?php echo e(base_url((string) $fieldValue($key))); ?>" alt="League logo preview">
                                        </div>
                                        <div class="form-text">Upload a new image to replace the current league logo.</div>
                                    <?php else: ?>
                                        <div class="form-text">Upload a league logo to display it across the admin and public portal.</div>
                                    <?php endif; ?>
                                <?php else: ?>
                                    <input class="form-control <?php echo validation_error($formErrors, $key) ? 'is-invalid' : ''; ?>" id="<?php echo e($key); ?>" name="<?php echo e($key); ?>" type="<?php echo e(($meta['type'] ?? 'text') === 'number' ? 'number' : (($meta['type'] ?? 'text') === 'email' ? 'email' : 'text')); ?>" <?php echo ($meta['type'] ?? '') === 'number' ? 'min="0" step="1"' : ''; ?> value="<?php echo e((string) $fieldValue($key)); ?>">
                                <?php endif; ?>
                                <?php if (validation_error($formErrors, $key)): ?><div class="invalid-feedback d-block"><?php echo e((string) validation_error($formErrors, $key)); ?></div><?php endif; ?>
                            </div>
                        <?php endforeach; ?>
                    </div>
                </section>
            </div>
        <?php endforeach; ?>
    </div>

    <div class="d-flex gap-2 mt-4">
        <button class="btn btn-jlfc" type="submit">Save settings</button>
        <a class="btn btn-outline-jlfc" href="<?php echo e(base_url('dashboard.php')); ?>">Back to dashboard</a>
    </div>
</form>
<?php require dirname(__DIR__, 2) . '/includes/footer.php'; ?>