{% extends 'base.html' %}

{% block title %}Settings - Matzeflix{% endblock %}

{% block content %}
<div class="row mb-4">
    <div class="col">
        <h2 class="fw-bold">Settings</h2>
        <p class="text-muted">Configure your service connections and API keys.</p>
    </div>
</div>

<div class="row justify-content-center">
    <div class="col-lg-8">
        <div class="card border-0 shadow">
            <div class="card-body p-4 p-md-5">
                <form method="post">
                    {% csrf_token %}
                    
                    {% for config in service_configs %}
                    <div class="mb-5 p-4 rounded bg-dark bg-opacity-25 border border-secondary border-opacity-25">
                        <div class="d-flex align-items-center mb-4">
                            <div class="p-3 rounded bg-danger bg-opacity-10 me-3">
                                {% if config.name == 'jellyfin' %}<i class="bi bi-play-btn fs-3 text-danger"></i>
                                {% elif config.name == 'radarr' %}<i class="bi bi-film fs-3 text-danger"></i>
                                {% elif config.name == 'sonarr' %}<i class="bi bi-tv fs-3 text-danger"></i>
                                {% elif config.name == 'jellyseerr' %}<i class="bi bi-plus-circle fs-3 text-danger"></i>
                                {% endif %}
                            </div>
                            <div>
                                <h4 class="text-capitalize mb-0 fw-bold">{{ config.name }}</h4>
                                <small class="text-muted">Configuration for {{ config.name|capfirst }}</small>
                            </div>
                        </div>
                        
                        <div class="row g-3">
                            <div class="col-12">
                                <label for="{{ config.name }}_url" class="form-label small text-muted text-uppercase fw-bold">Server URL</label>
                                <input type="url" class="form-control form-control-lg bg-dark border-secondary border-opacity-50 text-white" 
                                       id="{{ config.name }}_url" name="{{ config.name }}_url" 
                                       placeholder="http://192.168.1.100:8096" value="{{ config.url }}">
                            </div>
                            
                            <div class="col-12">
                                <label for="{{ config.name }}_api_key" class="form-label small text-muted text-uppercase fw-bold">API Key / Token</label>
                                <div class="input-group">
                                    <input type="password" class="form-control form-control-lg bg-dark border-secondary border-opacity-50 text-white" 
                                           id="{{ config.name }}_api_key" name="{{ config.name }}_api_key" 
                                           value="{{ config.api_key }}">
                                    <button class="btn btn-outline-secondary" type="button" onclick="togglePassword('{{ config.name }}_api_key')">
                                        <i class="bi bi-eye"></i>
                                    </button>
                                </div>
                            </div>
                            
                            <div class="col-12">
                                <div class="form-check form-switch mt-2">
                                    <input class="form-check-input" type="checkbox" id="{{ config.name }}_active" name="{{ config.name }}_active" 
                                           {% if config.is_active %}checked{% endif %}>
                                    <label class="form-check-label text-muted" for="{{ config.name }}_active">Enable this service</label>
                                </div>
                            </div>
                        </div>
                    </div>
                    {% endfor %}

                    <div class="d-grid gap-3 mt-5">
                        <button type="submit" class="btn btn-primary btn-lg py-3 fw-bold">
                            <i class="bi bi-check2-circle me-2"></i>SAVE ALL CONFIGURATIONS
                        </button>
                        <a href="{% url 'home' %}" class="btn btn-link text-decoration-none text-muted text-center">
                            Return to Dashboard
                        </a>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<script>
    function togglePassword(id) {
        const input = document.getElementById(id);
        if (input.type === "password") {
            input.type = "text";
        } else {
            input.type = "password";
        }
    }
</script>
{% endblock %}
