<!--
Copyright (C) 2026 Stefy Lanza <stefy@nexlab.net>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.
-->
{% extends "base.html" %}

{% block title %}Prompts - AISBF Dashboard{% endblock %}

{% block content %}
<h2 style="margin-bottom: 30px;" data-i18n="prompts_page.title">Prompts Configuration</h2>

{% if success %}
<div class="alert alert-success">{{ success }}</div>
{% endif %}

{% if error %}
<div class="alert alert-error">{{ error }}</div>
{% endif %}

<div style="margin-bottom: 20px;">
    <label style="font-weight: 500; margin-bottom: 10px; display: block;" data-i18n="prompts_page.select_file">Select Prompt File:</label>
    <select id="prompt-selector" onchange="switchPrompt()">
        {% for prompt in prompts %}
        <option value="{{ prompt.key }}" {% if loop.first %}selected{% endif %}>{{ prompt.name }}</option>
        {% endfor %}
    </select>
</div>

<form method="POST" id="prompt-form">
    <input type="hidden" name="prompt_key" id="prompt_key" value="">
    
    <div class="form-group">
        <label for="prompt_content" data-i18n="prompts_page.content">Prompt Content:</label>
        <textarea id="prompt_content" name="prompt_content" style="min-height: 400px;"></textarea>
        <small style="color: var(--color-muted); display: block; margin-top: 5px;" data-i18n="prompts_page.content_hint">Edit the prompt template. Use markdown formatting as needed.</small>
    </div>
    
    <div style="display: flex; gap: 10px; margin-top: 20px;">
        <button type="submit" class="btn" data-i18n="prompts_page.save">Save Prompt</button>
        {% if not is_admin %}
        <button type="button" class="btn btn-secondary" onclick="resetPrompt()" data-i18n="prompts_page.reset">Reset to Default</button>
        {% endif %}
        <a href="{{ url_for(request, '/dashboard') }}" class="btn btn-secondary" data-i18n="prompts_page.cancel">Cancel</a>
    </div>
</form>

<script>
const prompts = {{ prompts_data | safe }};
let currentPromptKey = '';

function switchPrompt() {
    const selector = document.getElementById('prompt-selector');
    const key = selector.value;
    currentPromptKey = key;
    
    const prompt = prompts.find(p => p.key === key);
    if (prompt) {
        document.getElementById('prompt_content').value = prompt.content;
        document.getElementById('prompt_key').value = key;
    }
}

async function resetPrompt() {
    if (!currentPromptKey) return;
    const ok = await showDangerConfirm(window.i18n.t('prompts_page.reset_confirm'), window.i18n.t('prompts_page.reset_confirm_title'));
    if (ok) {
        fetch('/dashboard/prompts/reset/' + currentPromptKey, {
            method: 'POST',
            headers: {
                'X-Requested-With': 'XMLHttpRequest'
            }
        }).then(response => {
            if (response.ok) {
                location.reload();
            }
        });
    }
}

// Initialize with first prompt
if (prompts.length > 0) {
    currentPromptKey = prompts[0].key;
    document.getElementById('prompt_content').value = prompts[0].content;
    document.getElementById('prompt_key').value = prompts[0].key;
}
</script>
{% endblock %}
