{% extends "base.html" %}
{% block title %}Settings - MediaRiver{% endblock %}
{% block content %}
<h1 class="page-title">Settings</h1>

<div class="card">
    <h2 class="card-title">Configuration</h2>
    <form hx-post="/api/settings" hx-swap="none">
        <div class="form-group">
            <label for="workflows_dir">Workflows Directory</label>
            <input type="text" id="workflows_dir" name="workflows_dir" value="{{ config.workflows_dir }}">
        </div>
        <div class="form-group">
            <label for="log_level">Log Level</label>
            <select id="log_level" name="log_level">
                {% for lvl in ["debug", "info", "warning", "error"] %}
                <option value="{{ lvl }}" {{ 'selected' if lvl == config.log_level }}>{{ lvl }}</option>
                {% endfor %}
            </select>
        </div>
        <div class="form-group">
            <label for="database_url">Database URL</label>
            <input type="text" id="database_url" name="database_url" value="{{ config.database_url or '' }}">
        </div>
        <div class="form-group">
            <label for="port">Port</label>
            <input type="number" id="port" name="port" value="{{ config.port }}">
        </div>
        <div class="form-group">
            <label for="env">Environment Variables (JSON)</label>
            <textarea id="env" name="env" rows="4">{{ env_json }}</textarea>
        </div>
        <button type="submit" class="btn btn-accent">Save Settings</button>
    </form>
</div>

<div class="card mt-4">
    <h2 class="card-title">Updates</h2>
    <p class="text-sm mb-4">Current version: <strong>{{ current_version }}</strong></p>
    <div id="update-status">
        {% if update_status %}
        <p class="text-sm">
            Remote: <strong>{{ update_status.remote }}</strong>
            {% if update_status.up_to_date %}
            <span class="badge badge-done">Up to date</span>
            {% else %}
            <span class="badge badge-pending">{{ update_status.commits_behind }} commit(s) behind</span>
            {% endif %}
        </p>
        {% endif %}
    </div>
    <div class="mt-4">
        <button class="btn" hx-get="/api/update/check" hx-target="#update-status" hx-swap="innerHTML">Check for Updates</button>
        <button class="btn btn-accent" hx-post="/api/update/apply" hx-swap="none">Apply Update</button>
    </div>
</div>

<div class="card mt-4">
    <h2 class="card-title">Windows Startup</h2>
    <div id="startup-status">
        {% if startup_enabled %}
        <p class="text-sm"><span class="badge badge-done">Enabled</span> MediaRiver starts automatically 30s after logon</p>
        <button class="btn" hx-post="/api/startup/disable" hx-target="#startup-status" hx-swap="innerHTML">Disable Startup</button>
        {% else %}
        <p class="text-sm"><span class="badge badge-failed">Disabled</span> MediaRiver does not start on Windows boot</p>
        <button class="btn btn-accent" hx-post="/api/startup/enable" hx-target="#startup-status" hx-swap="innerHTML">Enable Startup</button>
        {% endif %}
    </div>
</div>

<div class="card mt-4">
    <h2 class="card-title">Control</h2>
    <div style="display: flex; gap: 12px;">
        <button class="btn" hx-post="/api/engine/restart" hx-swap="none" title="Restart the media processing engine only">Restart Engine</button>
        <button class="btn btn-accent" hx-post="/api/app/restart" hx-swap="none" title="Full restart — engine + web server (picks up code changes)">Restart App</button>
    </div>
    <p class="text-sm mt-2" style="color: #888;">Engine = media pipeline process. App = entire tray app + web server (use after updates).</p>
</div>
{% endblock %}
