<h2 class="mb-4">Ustawienia</h2>

<div class="card" style="max-width: 700px;">
    <div class="card-body">
        <form method="post" action="/settings">

            <h5 class="mb-3 border-bottom pb-2">OpenAI (Generowanie artykułów)</h5>

            <div class="mb-3">
                <label for="openai_api_key" class="form-label">Klucz API OpenAI</label>
                <input type="password" class="form-control" id="openai_api_key" name="openai_api_key"
                    value="<?= htmlspecialchars($settings['openai_api_key']) ?>"
                    placeholder="sk-proj-...">
            </div>

            <div class="mb-3">
                <label for="openai_model" class="form-label">Model AI</label>
                <select class="form-select" id="openai_model" name="openai_model">
                    <optgroup label="GPT-5">
                        <option value="gpt-5" <?= $settings['openai_model'] === 'gpt-5' ? 'selected' : '' ?>>GPT-5 (najnowszy)</option>
                        <option value="gpt-5-mini" <?= $settings['openai_model'] === 'gpt-5-mini' ? 'selected' : '' ?>>GPT-5 Mini</option>
                    </optgroup>
                    <optgroup label="GPT-4">
                        <option value="gpt-4o" <?= $settings['openai_model'] === 'gpt-4o' ? 'selected' : '' ?>>GPT-4o (rekomendowany)</option>
                        <option value="gpt-4o-mini" <?= $settings['openai_model'] === 'gpt-4o-mini' ? 'selected' : '' ?>>GPT-4o-mini (tańszy)</option>
                        <option value="gpt-4-turbo" <?= $settings['openai_model'] === 'gpt-4-turbo' ? 'selected' : '' ?>>GPT-4 Turbo</option>
                    </optgroup>
                    <optgroup label="GPT-3.5">
                        <option value="gpt-3.5-turbo" <?= $settings['openai_model'] === 'gpt-3.5-turbo' ? 'selected' : '' ?>>GPT-3.5 Turbo (najtańszy)</option>
                    </optgroup>
                </select>
            </div>

            <div class="row mb-3">
                <div class="col">
                    <label for="article_min_words" class="form-label">Min. słów w artykule</label>
                    <input type="number" class="form-control" id="article_min_words" name="article_min_words"
                        value="<?= htmlspecialchars($settings['article_min_words']) ?>">
                </div>
                <div class="col">
                    <label for="article_max_words" class="form-label">Max. słów w artykule</label>
                    <input type="number" class="form-control" id="article_max_words" name="article_max_words"
                        value="<?= htmlspecialchars($settings['article_max_words']) ?>">
                </div>
            </div>

            <div class="mb-3">
                <label for="article_generation_prompt" class="form-label">Prompt do generowania artykułu</label>
                <textarea class="form-control" id="article_generation_prompt" name="article_generation_prompt" rows="10"><?= htmlspecialchars($settings['article_generation_prompt']) ?></textarea>
                <small class="text-muted">Dostepne placeholdery: {min_words}, {max_words}. Zalecenie: prompt opisuje styl i strukture, a temat jest dopinany automatycznie.</small>
            </div>

            <h6 class="mb-3 mt-4 border-bottom pb-2">Linkowanie wewnetrzne (Smart Matching)</h6>

            <div class="form-check form-switch mb-3">
                <input type="hidden" name="internal_linking_enabled" value="0">
                <input class="form-check-input" type="checkbox" role="switch" id="internal_linking_enabled" name="internal_linking_enabled" value="1" <?= (string) ($settings['internal_linking_enabled'] ?? '1') !== '0' ? 'checked' : '' ?>>
                <label class="form-check-label" for="internal_linking_enabled">Wlacz automatyczne linkowanie wewnetrzne</label>
            </div>

            <div class="row mb-3">
                <div class="col">
                    <label for="internal_link_min_count" class="form-label">Min. liczba linkow</label>
                    <input type="number" class="form-control" id="internal_link_min_count" name="internal_link_min_count" min="0" max="8" value="<?= htmlspecialchars($settings['internal_link_min_count'] ?? '2') ?>">
                </div>
                <div class="col">
                    <label for="internal_link_max_count" class="form-label">Max. liczba linkow</label>
                    <input type="number" class="form-control" id="internal_link_max_count" name="internal_link_max_count" min="1" max="8" value="<?= htmlspecialchars($settings['internal_link_max_count'] ?? '4') ?>">
                </div>
            </div>

            <div class="mb-3">
                <label for="openai_embedding_model" class="form-label">Model embeddingow</label>
                <input type="text" class="form-control" id="openai_embedding_model" name="openai_embedding_model" value="<?= htmlspecialchars($settings['openai_embedding_model'] ?? 'text-embedding-3-small') ?>" placeholder="text-embedding-3-small">
                <small class="text-muted">Wykorzystywany do semantycznego dopasowania powiazanych artykulow.</small>
            </div>

            <h5 class="mb-3 mt-4 border-bottom pb-2">Obrazki</h5>

            <div class="mb-3">
                <label for="image_provider" class="form-label">Dostawca obrazków</label>
                <select class="form-select" id="image_provider" name="image_provider">
                    <option value="freepik" <?= $settings['image_provider'] === 'freepik' ? 'selected' : '' ?>>Freepik AI (generowanie)</option>
                    <option value="unsplash" <?= $settings['image_provider'] === 'unsplash' ? 'selected' : '' ?>>Unsplash (darmowe stocki)</option>
                    <option value="pexels" <?= $settings['image_provider'] === 'pexels' ? 'selected' : '' ?>>Pexels (darmowe stocki)</option>
                </select>
            </div>

            <div class="mb-3">
                <label for="image_generation_prompt" class="form-label">Prompt do generowania zdjęcia (Freepik)</label>
                <textarea class="form-control" id="image_generation_prompt" name="image_generation_prompt" rows="3"><?= htmlspecialchars($settings['image_generation_prompt']) ?></textarea>
                <small class="text-muted">Dostępne placeholdery: {topic_name}, {article_title}</small>
            </div>

            <div class="mb-3">
                <label for="freepik_api_key" class="form-label">Klucz API Freepik</label>
                <input type="password" class="form-control" id="freepik_api_key" name="freepik_api_key"
                    value="<?= htmlspecialchars($settings['freepik_api_key']) ?>">
            </div>

            <div class="mb-3">
                <label for="unsplash_api_key" class="form-label">Klucz API Unsplash</label>
                <input type="password" class="form-control" id="unsplash_api_key" name="unsplash_api_key"
                    value="<?= htmlspecialchars($settings['unsplash_api_key']) ?>">
            </div>

            <div class="mb-3">
                <label for="pexels_api_key" class="form-label">Klucz API Pexels</label>
                <input type="password" class="form-control" id="pexels_api_key" name="pexels_api_key"
                    value="<?= htmlspecialchars($settings['pexels_api_key']) ?>">
            </div>

            <h5 class="mb-3 mt-4 border-bottom pb-2">SEMSTORM (metryki SEO)</h5>

            <div class="mb-3">
                <label for="semstorm_login" class="form-label">Login SEMSTORM</label>
                <input type="text" class="form-control" id="semstorm_login" name="semstorm_login"
                    value="<?= htmlspecialchars($settings['semstorm_login']) ?>">
            </div>

            <div class="mb-3">
                <label for="semstorm_password" class="form-label">Hasło SEMSTORM</label>
                <input type="password" class="form-control" id="semstorm_password" name="semstorm_password"
                    value="<?= htmlspecialchars($settings['semstorm_password']) ?>">
            </div>

            <div class="mb-3">
                <label for="semstorm_api_base" class="form-label">Bazowy URL API</label>
                <input type="text" class="form-control" id="semstorm_api_base" name="semstorm_api_base"
                    value="<?= htmlspecialchars($settings['semstorm_api_base']) ?>"
                    placeholder="https://api.semstorm.com">
            </div>

            <div class="mb-3">
                <label for="semstorm_timeout_seconds" class="form-label">Timeout API SEMSTORM (sekundy)</label>
                <input type="number" class="form-control" id="semstorm_timeout_seconds" name="semstorm_timeout_seconds"
                    value="<?= htmlspecialchars($settings['semstorm_timeout_seconds']) ?>" min="5" max="120">
            </div>

            <h5 class="mb-3 mt-4 border-bottom pb-2">DataForSEO (indeksacja domeny)</h5>

            <div class="mb-3">
                <label for="dataforseo_login" class="form-label">Login DataForSEO</label>
                <input type="text" class="form-control" id="dataforseo_login" name="dataforseo_login"
                    value="<?= htmlspecialchars($settings['dataforseo_login']) ?>">
            </div>

            <div class="mb-3">
                <label for="dataforseo_password" class="form-label">Haslo DataForSEO</label>
                <input type="password" class="form-control" id="dataforseo_password" name="dataforseo_password"
                    value="<?= htmlspecialchars($settings['dataforseo_password']) ?>">
            </div>

            <div class="mb-3">
                <label for="dataforseo_api_base" class="form-label">Bazowy URL API</label>
                <input type="text" class="form-control" id="dataforseo_api_base" name="dataforseo_api_base"
                    value="<?= htmlspecialchars($settings['dataforseo_api_base']) ?>"
                    placeholder="https://api.dataforseo.com">
            </div>

            <div class="row mb-3">
                <div class="col-md-6">
                    <label for="dataforseo_location_code" class="form-label">Location code</label>
                    <input type="number" class="form-control" id="dataforseo_location_code" name="dataforseo_location_code"
                        value="<?= htmlspecialchars($settings['dataforseo_location_code']) ?>" min="1">
                </div>
                <div class="col-md-6">
                    <label for="dataforseo_language_code" class="form-label">Language code</label>
                    <input type="text" class="form-control" id="dataforseo_language_code" name="dataforseo_language_code"
                        value="<?= htmlspecialchars($settings['dataforseo_language_code']) ?>" placeholder="pl">
                </div>
            </div>

            <div class="mb-3">
                <label for="dataforseo_timeout_seconds" class="form-label">Timeout API DataForSEO (sekundy)</label>
                <input type="number" class="form-control" id="dataforseo_timeout_seconds" name="dataforseo_timeout_seconds"
                    value="<?= htmlspecialchars($settings['dataforseo_timeout_seconds']) ?>" min="5" max="120">
            </div>

            <button type="submit" class="btn btn-primary">Zapisz ustawienia</button>
        </form>
    </div>
</div>
