<?php require ROOT_PATH . '/admin/templates/header.php'; ?>

<div class="content-wrapper">
<section class="content pt-3">
<div class="container-fluid pb-5">

<div class="d-flex justify-content-between align-items-start gap-3 mb-4"><div><h3 class="mb-1 fw-bold"><i class="bi bi-stars text-purple me-2"></i><?= __('integrations.gemini.page_title','Gemini AI Settings') ?></h3></div></div>


<?php
use Core\System\Language;

$current = $gemini ?? [];
$isArabic  = Language::code() === 'ar';
?>

<div class="container-fluid py-3">

    <h2 class="page-title mb-3">
        <i class="bi bi-robot me-2"></i>
        <?= __('ai.gemini.title', 'Gemini Settings') ?>
    </h2>

    <?php if (!empty($modelsError)): ?>
        <div class="alert alert-warning">
            <?= htmlspecialchars($modelsError) ?>
        </div>
    <?php endif; ?>

    <div class="card shadow-sm">
        <div class="card-body">

            <form method="POST" action="/admin/settings/gemini/save">
                <?= \Core\System\Csrf::field(); ?>

                <div class="row g-3 mb-3">

                    <div class="col-md-4">
                        <div class="form-check form-switch mt-4">
                            <input class="form-check-input"
                                   type="checkbox"
                                   name="enabled"
                                   <?= !empty($current['is_active']) ? 'checked' : '' ?>>
                            <label class="form-check-label">
                                <?= __('ai.gemini.enabled', 'Enabled') ?>
                            </label>
                        </div>
                    </div>

                    <div class="col-md-8">
                        <label class="form-label"><?= __('ai.gemini.api_key', 'API Key') ?></label>
                        <input type="password"
                               class="form-control"
                               name="api_key"
                               value="<?= htmlspecialchars($current['api_key'] ?? '') ?>">
                        <div class="form-text">
                            <?= __('ai.gemini.api_key_hint', 'After saving the key, reload this page to load available models.') ?>
                        </div>
                    </div>

                </div>

                <?php if ($isArabic): ?>
                    <div class="mb-3">
                        <label class="form-label"><?= __('ai.default_dialect', 'Default Arabic Dialect') ?></label>
                        <select name="dialect" class="form-select">
                            <option value="fusha" <?= ($current['dialect'] ?? 'fusha') === 'fusha' ? 'selected' : '' ?>><?= __('ai.dialect.fusha', 'Fusha') ?></option>
                            <option value="algerian" <?= ($current['dialect'] ?? '') === 'algerian' ? 'selected' : '' ?>><?= __('ai.dialect.algerian', 'Algerian') ?></option>
                            <option value="moroccan" <?= ($current['dialect'] ?? '') === 'moroccan' ? 'selected' : '' ?>><?= __('ai.dialect.moroccan', 'Moroccan') ?></option>
                            <option value="egyptian" <?= ($current['dialect'] ?? '') === 'egyptian' ? 'selected' : '' ?>><?= __('ai.dialect.egyptian', 'Egyptian') ?></option>
                            <option value="saudi" <?= ($current['dialect'] ?? '') === 'saudi' ? 'selected' : '' ?>><?= __('ai.dialect.saudi', 'Saudi') ?></option>
                        </select>
                    </div>
                <?php endif; ?>

                <div class="mb-3">
                    <label class="form-label"><?= __('ai.gemini.model', 'Model') ?></label>

                    <?php if (!empty($models)): ?>
                        <select name="model" class="form-select">
                            <?php foreach ($models as $m): ?>
                                <option value="<?= htmlspecialchars($m) ?>"
                                    <?= (($current['model'] ?? '') === $m) ? 'selected' : '' ?>>
                                    <?= htmlspecialchars($m) ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                        <div class="form-text">
                            <?= __('ai.gemini.model_hint', 'Models are loaded from Google ListModels. Choose one that supports generateContent.') ?>
                        </div>
                    <?php else: ?>
                        <input type="text"
                               class="form-control"
                               name="model"
                               value="<?= htmlspecialchars($current['model'] ?? '') ?>"
                               placeholder="models/....">
                        <div class="form-text">
                            <?= __('ai.gemini.model_hint_empty', 'Enter model name after saving API key, then reload to see dropdown.') ?>
                        </div>
                    <?php endif; ?>
                </div>

                <hr class="my-4">

                <button type="submit" class="btn btn-primary">
                    <i class="bi bi-save me-1"></i>
                    <?= __('actions.save', 'Save') ?>
                </button>

            </form>

        </div>
    </div>

</div>

<?php ?>
</div></section></div>
<?php require ROOT_PATH . '/admin/templates/footer.php'; ?>
