{% extends "layout.html" %}

{% block content %}
<div class="container">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h1>Edit Scene: {{ scene.name }}</h1>
        <a href="{{ url_for('scene_detail', slug=scene.slug) }}" class="btn btn-outline-secondary">Cancel</a>
    </div>

    <form action="{{ url_for('edit_scene', slug=scene.slug) }}" method="post" id="main-form">
        <div class="row">
            <div class="col-md-8">
                <!-- Basic Info -->
                <div class="card mb-4">
                    <div class="card-header bg-dark text-white">Basic Information</div>
                    <div class="card-body">
                        <div class="mb-3">
                            <label for="scene_name" class="form-label">Display Name</label>
                            <input type="text" class="form-control" id="scene_name" name="scene_name" value="{{ scene.name }}" required>
                        </div>
                        <div class="mb-3">
                            <label for="description" class="form-label">Description</label>
                            <textarea class="form-control" id="description" name="description" rows="2">{{ scene.data.description }}</textarea>
                        </div>
                    </div>
                </div>

                <!-- LoRA -->
                <div class="card mb-4">
                    <div class="card-header bg-info text-white">LoRA Settings</div>
                    <div class="card-body">
                        <div class="row">
                            <div class="col-md-8">
                                <label for="lora_lora_name" class="form-label">LoRA Name</label>
                                <select class="form-select" id="lora_lora_name" name="lora_lora_name">
                                    <option value="">None</option>
                                    {% for lora in loras %}
                                    <option value="{{ lora }}" {% if scene.data.lora and scene.data.lora.lora_name == lora %}selected{% endif %}>{{ lora }}</option>
                                    {% endfor %}
                                </select>
                            </div>
                            <div class="col-md-4">
                                <label for="lora_lora_weight" class="form-label">Weight</label>
                                <input type="number" step="0.01" class="form-control" id="lora_lora_weight" name="lora_lora_weight" value="{{ scene.data.lora.lora_weight if scene.data.lora else 1.0 }}">
                            </div>
                        </div>
                        <div class="mt-3">
                            <label for="lora_lora_triggers" class="form-label">Triggers</label>
                            <input type="text" class="form-control" id="lora_lora_triggers" name="lora_lora_triggers" value="{{ scene.data.lora.lora_triggers if scene.data.lora else '' }}">
                        </div>
                        <div class="row mt-3">
                            <div class="col-md-6">
                                <label for="lora_lora_weight_min" class="form-label small text-muted">Min Weight <span class="text-warning">(randomised)</span></label>
                                <input type="number" step="0.05" min="-5" max="5" class="form-control form-control-sm"
                                       id="lora_lora_weight_min" name="lora_lora_weight_min"
                                       value="{{ scene.data.lora.get('lora_weight_min', '') if scene.data.lora else '' }}"
                                       placeholder="e.g. 0.6">
                            </div>
                            <div class="col-md-6">
                                <label for="lora_lora_weight_max" class="form-label small text-muted">Max Weight <span class="text-warning">(randomised)</span></label>
                                <input type="number" step="0.05" min="-5" max="5" class="form-control form-control-sm"
                                       id="lora_lora_weight_max" name="lora_lora_weight_max"
                                       value="{{ scene.data.lora.get('lora_weight_max', '') if scene.data.lora else '' }}"
                                       placeholder="e.g. 1.0">
                            </div>
                        </div>
                        <p class="text-muted small mt-1 mb-0">When Min ≠ Max, weight is randomised between them each generation.</p>
                    </div>
                </div>

                <!-- Scene Definition Section -->
                {% set sdata = scene.data.get('scene', {}) %}
                <div class="card mb-4">
                    <div class="card-header bg-light"><strong>Scene Definition</strong></div>
                    <div class="card-body">
                        <div class="mb-3">
                            <label for="scene_background" class="form-label">Background</label>
                            <input type="text" class="form-control" id="scene_background" name="scene_background" value="{{ sdata.get('background', '') }}">
                        </div>
                        <div class="mb-3">
                            <label for="scene_foreground" class="form-label">Foreground</label>
                            <input type="text" class="form-control" id="scene_foreground" name="scene_foreground" value="{{ sdata.get('foreground', '') }}">
                        </div>
                        <div class="mb-3">
                            <label for="scene_furniture" class="form-label">Furniture (comma separated)</label>
                            {% set furniture = sdata.get('furniture', []) %}
                            <input type="text" class="form-control" id="scene_furniture" name="scene_furniture" value="{{ furniture|join(', ') if furniture is iterable and furniture is not string else furniture }}">
                        </div>
                        <div class="mb-3">
                            <label for="scene_colors" class="form-label">Colors (comma separated)</label>
                            {% set colors = sdata.get('colors', []) %}
                            <input type="text" class="form-control" id="scene_colors" name="scene_colors" value="{{ colors|join(', ') if colors is iterable and colors is not string else colors }}">
                        </div>
                        <div class="mb-3">
                            <label for="scene_lighting" class="form-label">Lighting</label>
                            <input type="text" class="form-control" id="scene_lighting" name="scene_lighting" value="{{ sdata.get('lighting', '') }}">
                        </div>
                        <div class="mb-3">
                            <label for="scene_theme" class="form-label">Theme</label>
                            <input type="text" class="form-control" id="scene_theme" name="scene_theme" value="{{ sdata.get('theme', '') }}">
                        </div>
                    </div>
                </div>

                <!-- Tags -->
                <div class="card mb-4">
                    <div class="card-header bg-light"><strong>Tags</strong></div>
                    <div class="card-body">
                        <div class="mb-3">
                            <label for="tags" class="form-label">Tags</label>
                            <input type="text" class="form-control" id="tags" name="tags"
                                   value="{{ scene.data.tags | join(', ') if scene.data.tags else '' }}">
                            <div class="form-text">Comma-separated tags appended to every generation.</div>
                        </div>
                    </div>
                </div>

                <div class="d-grid gap-2 d-md-flex justify-content-md-end">
                    <a href="{{ url_for('scene_detail', slug=scene.slug) }}" class="btn btn-secondary">Cancel</a>
                    <button type="submit" class="btn btn-primary">Save Changes</button>
                </div>
            </div>
        </div>
    </form>
</div>
{% endblock %}
