{% extends "layout.html" %}

{% block content %}
<!-- JSON Editor Modal -->
<div class="modal fade" id="jsonEditorModal" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog modal-lg modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Edit JSON — {{ preset.name }}</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <ul class="nav nav-tabs mb-3" role="tablist">
                    <li class="nav-item"><button class="nav-link active" id="json-simple-tab" type="button">Simple</button></li>
                    <li class="nav-item"><button class="nav-link" id="json-advanced-tab" type="button">Advanced JSON</button></li>
                </ul>
                <div id="json-editor-error" class="alert alert-danger d-none"></div>
                <div id="json-simple-panel"></div>
                <div id="json-advanced-panel" class="d-none">
                    <textarea id="json-editor-textarea" class="form-control font-monospace" rows="25" spellcheck="false"></textarea>
                </div>
                <script type="application/json" id="json-raw-data">{{ preset.data | tojson }}</script>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                <button type="button" class="btn btn-primary" id="json-save-btn">Save</button>
            </div>
        </div>
    </div>
</div>

<div class="d-flex justify-content-between align-items-center mb-3">
    <div>
        <a href="{{ url_for('presets_index') }}" class="btn btn-sm btn-outline-secondary me-2">Back to Library</a>
        <h3 class="d-inline-block mb-0">{{ preset.name }}</h3>
    </div>
    <div class="d-flex gap-2">
        <a href="{{ url_for('edit_preset', slug=preset.slug) }}" class="btn btn-sm btn-outline-primary">Edit</a>
        <button class="btn btn-sm btn-outline-secondary" data-bs-toggle="modal" data-bs-target="#jsonEditorModal">JSON</button>
        <form action="{{ url_for('clone_preset', slug=preset.slug) }}" method="post" class="d-contents">
            <button type="submit" class="btn btn-sm btn-outline-secondary">Clone</button>
        </form>
    </div>
</div>

<div class="row">
    <!-- Left: image + generate -->
    <div class="col-md-4">
        <div class="card mb-3">
            <div class="img-container" style="height:auto;min-height:400px;cursor:pointer;"
                 onclick="openGallery([this.querySelector('img') ? this.querySelector('img').src : this.src || ''], 0)">
                {% if preset.image_path %}
                <img src="{{ url_for('static', filename='uploads/' + preset.image_path) }}"
                     alt="{{ preset.name }}" class="img-fluid"
                     data-preview-path="{{ preset.image_path }}">
                {% else %}
                <div class="d-flex align-items-center justify-content-center bg-light" style="height:400px;">
                    <span class="text-muted">No Image</span>
                </div>
                {% endif %}
            </div>
            <div class="card-body">
                <form id="generate-form" action="{{ url_for('generate_preset_image', slug=preset.slug) }}" method="post">
                    {# Additional Prompts #}
                    <div class="mb-2">
                        <label for="extra_positive" class="form-label">Additional Positive</label>
                        <textarea class="form-control form-control-sm font-monospace" id="extra_positive" name="extra_positive" rows="2" placeholder="e.g. masterpiece, best quality">{{ extra_positive or '' }}</textarea>
                    </div>
                    <div class="mb-3">
                        <label for="extra_negative" class="form-label">Additional Negative</label>
                        <textarea class="form-control form-control-sm font-monospace" id="extra_negative" name="extra_negative" rows="2" placeholder="e.g. blurry, low quality">{{ extra_negative or '' }}</textarea>
                    </div>

                    {# Resolution override #}
                    {% set res = preset.data.get('resolution', {}) %}
                    <div class="mb-3">
                        <label class="form-label">Resolution Override</label>
                        <div class="d-flex flex-wrap gap-1 mb-2">
                            <button type="button" class="btn btn-sm btn-outline-secondary res-preset" data-w="" data-h="">Preset Default</button>
                            <button type="button" class="btn btn-sm btn-outline-secondary res-preset" data-w="1024" data-h="1024">1:1</button>
                            <button type="button" class="btn btn-sm btn-outline-secondary res-preset" data-w="1152" data-h="896">4:3 L</button>
                            <button type="button" class="btn btn-sm btn-outline-secondary res-preset" data-w="896" data-h="1152">4:3 P</button>
                            <button type="button" class="btn btn-sm btn-outline-secondary res-preset" data-w="1344" data-h="768">16:9 L</button>
                            <button type="button" class="btn btn-sm btn-outline-secondary res-preset" data-w="768" data-h="1344">16:9 P</button>
                            <button type="button" class="btn btn-sm btn-outline-secondary res-preset" data-w="1280" data-h="800">16:10 L</button>
                            <button type="button" class="btn btn-sm btn-outline-secondary res-preset" data-w="800" data-h="1280">16:10 P</button>
                        </div>
                        <div class="d-flex align-items-center gap-2">
                            <label class="form-label mb-0 small fw-semibold">W</label>
                            <input type="number" class="form-control form-control-sm" name="width" id="res-width"
                                   value="" min="64" max="4096" step="64" style="width:88px" placeholder="Auto">
                            <span class="text-muted">&times;</span>
                            <label class="form-label mb-0 small fw-semibold">H</label>
                            <input type="number" class="form-control form-control-sm" name="height" id="res-height"
                                   value="" min="64" max="4096" step="64" style="width:88px" placeholder="Auto">
                        </div>
                    </div>

                    <div class="d-grid gap-2">
                        <div class="input-group input-group-sm mb-1">
                            <span class="input-group-text">Seed</span>
                            <input type="number" class="form-control" id="seed-input" name="seed" placeholder="Random" min="1" step="1">
                            <button type="button" class="btn btn-outline-secondary" id="seed-clear-btn" title="Clear (random)">&times;</button>
                        </div>
                        <button type="submit" name="action" value="preview" class="btn btn-success" data-requires="comfyui">Generate Preview</button>
                        <button type="button" id="endless-btn" class="btn btn-outline-success" onclick="window._endlessStart()" data-requires="comfyui">Endless</button>
                        <button type="button" id="endless-stop-btn" class="btn btn-danger d-none" onclick="window._endlessStop()">Stop</button>
                        <small id="endless-counter" class="text-muted d-none"></small>
                        <button type="submit" name="action" value="replace" class="btn btn-outline-warning btn-sm" data-requires="comfyui">Generate &amp; Set Cover</button>
                    </div>
                </form>
            </div>
        </div>

        <!-- Selected Preview -->
        <div class="card mb-4 {% if preview_path %}border-success{% else %}border-secondary d-none{% endif %}" id="preview-card">
            <div class="card-header {% if preview_path %}bg-success{% else %}bg-secondary{% endif %} text-white d-flex justify-content-between align-items-center" id="preview-card-header">
                <span>Selected Preview</span>
                <form action="{{ url_for('replace_preset_cover_from_preview', slug=preset.slug) }}" method="post" class="m-0" id="replace-cover-form">
                    <input type="hidden" name="preview_path" id="preview-path" value="{{ preview_path or '' }}">
                    <button type="submit" class="btn btn-sm btn-outline-light" id="replace-cover-btn" {% if not preview_path %}disabled{% endif %}>Replace Cover</button>
                </form>
            </div>
            <div class="card-body p-0">
                <div class="img-container" style="height: auto; min-height: 400px; cursor: pointer;">
                    <img id="preview-img" src="{{ url_for('static', filename='uploads/' + preview_path) if preview_path else '' }}" alt="Preview" class="img-fluid">
                </div>
            </div>
        </div>
    </div>

    <!-- Right: preset summary -->
    <div class="col-md-8">

        {% macro toggle_badge(val) %}
            {% if val == 'random' %}<span class="badge bg-warning text-dark">RNG</span>
            {% elif val %}<span class="badge bg-success">ON</span>
            {% else %}<span class="badge bg-secondary">OFF</span>{% endif %}
        {% endmacro %}

        {% macro entity_badge(val) %}
            {% if val == 'random' %}<span class="badge bg-warning text-dark">Random</span>
            {% elif val %}<span class="badge bg-info text-dark">{{ val | replace('_', ' ') | title }}</span>
            {% else %}<span class="badge bg-secondary">None</span>{% endif %}
        {% endmacro %}

        <!-- Character -->
        <div class="card mb-3">
            <div class="card-header d-flex justify-content-between align-items-center py-2">
                <strong>Character</strong>
                {{ entity_badge(preset.data.character.character_id) }}
            </div>
            <div class="card-body py-2">
                {% set char_fields = preset.data.character.fields %}
                <div class="mb-2">
                    <small class="text-muted fw-semibold d-block mb-1">Identity</small>
                    <div class="d-flex flex-wrap gap-1">
                        {% for k in ['base','head','upper_body','lower_body','hands','feet','additional'] %}
                        <span class="badge-field d-flex align-items-center gap-1 border rounded px-2 py-1 small">
                            <span>{{ k | replace('_', ' ') }}</span>
                            {{ toggle_badge(char_fields.identity.get(k, true)) }}
                        </span>
                        {% endfor %}
                    </div>
                </div>
                <div class="mb-2">
                    <small class="text-muted fw-semibold d-block mb-1">Defaults</small>
                    <div class="d-flex flex-wrap gap-1">
                        {% for k in ['expression','pose','scene'] %}
                        <span class="badge-field d-flex align-items-center gap-1 border rounded px-2 py-1 small">
                            <span>{{ k }}</span>
                            {{ toggle_badge(char_fields.defaults.get(k, false)) }}
                        </span>
                        {% endfor %}
                    </div>
                </div>
                {% set wd = char_fields.wardrobe %}
                <div>
                    <small class="text-muted fw-semibold d-block mb-1">Wardrobe
                        <span class="badge bg-light text-dark border ms-1">outfit: {{ wd.get('outfit', 'default') }}</span>
                    </small>
                    <div class="d-flex flex-wrap gap-1">
                        {% for k in ['base','head','upper_body','lower_body','hands','feet','additional'] %}
                        <span class="badge-field d-flex align-items-center gap-1 border rounded px-2 py-1 small">
                            <span>{{ k | replace('_', ' ') }}</span>
                            {{ toggle_badge(wd.fields.get(k, true)) }}
                        </span>
                        {% endfor %}
                    </div>
                </div>
                <div class="mt-2">
                    <small class="text-muted">LoRA:</small>
                    {% if preset.data.character.use_lora %}<span class="badge bg-success">ON</span>{% else %}<span class="badge bg-secondary">OFF</span>{% endif %}
                </div>
            </div>
        </div>

        <!-- Secondary resources row -->
        <div class="row g-2 mb-3">
            {% for section, label, field_key, field_keys in [
                ('outfit',   'Outfit',   'outfit_id',   []),
                ('action',   'Action',   'action_id',   ['base','head','upper_body','lower_body','hands','feet','additional']),
                ('style',    'Style',    'style_id',    []),
                ('scene',    'Scene',    'scene_id',    ['background','foreground','furniture','colors','lighting','theme']),
                ('detailer', 'Detailer', 'detailer_id', []),
            ] %}
            {% set sec = preset.data.get(section, {}) %}
            <div class="col-md-6">
                <div class="card h-100">
                    <div class="card-header d-flex justify-content-between align-items-center py-1">
                        <small class="fw-semibold">{{ label }}</small>
                        {{ entity_badge(sec.get(field_key)) }}
                    </div>
                    {% if field_keys %}
                    <div class="card-body py-2">
                        <div class="d-flex flex-wrap gap-1">
                            {% for k in field_keys %}
                            <span class="badge-field d-flex align-items-center gap-1 border rounded px-1 py-1" style="font-size:0.7rem">
                                <span>{{ k | replace('_', ' ') }}</span>
                                {{ toggle_badge(sec.get('fields', {}).get(k, true)) }}
                            </span>
                            {% endfor %}
                        </div>
                        <div class="mt-1">
                            <small class="text-muted">LoRA:</small>
                            {% if sec.get('use_lora', true) %}<span class="badge bg-success">ON</span>{% else %}<span class="badge bg-secondary">OFF</span>{% endif %}
                        </div>
                    </div>
                    {% else %}
                    <div class="card-body py-2">
                        <small class="text-muted">LoRA:</small>
                        {% if sec.get('use_lora', true) %}<span class="badge bg-success">ON</span>{% else %}<span class="badge bg-secondary">OFF</span>{% endif %}
                    </div>
                    {% endif %}
                </div>
            </div>
            {% endfor %}

            <!-- Look & Checkpoint -->
            <div class="col-md-6">
                <div class="card h-100">
                    <div class="card-header py-1"><small class="fw-semibold">Look</small></div>
                    <div class="card-body py-2">{{ entity_badge(preset.data.get('look', {}).get('look_id')) }}</div>
                </div>
            </div>
            <div class="col-md-6">
                <div class="card h-100">
                    <div class="card-header py-1"><small class="fw-semibold">Checkpoint</small></div>
                    <div class="card-body py-2">{{ entity_badge(preset.data.get('checkpoint', {}).get('checkpoint_path')) }}</div>
                </div>
            </div>
            {% set res = preset.data.get('resolution', {}) %}
            <div class="col-md-6">
                <div class="card h-100">
                    <div class="card-header py-1"><small class="fw-semibold">Resolution</small></div>
                    <div class="card-body py-2">
                        {% if res.get('random', false) %}
                        <span class="badge bg-warning text-dark">Random</span>
                        {% elif res.get('width') %}
                        <span class="badge bg-info text-dark">{{ res.width }} &times; {{ res.height }}</span>
                        {% else %}
                        <span class="badge bg-secondary">Default</span>
                        {% endif %}
                    </div>
                </div>
            </div>
        </div>

        <!-- Tags -->
        {% if preset.data.tags %}
        <div class="card mb-3">
            <div class="card-header py-2"><strong>Extra Tags</strong></div>
            <div class="card-body py-2">
                {% for tag in preset.data.tags %}
                <span class="badge bg-light text-dark border me-1">{{ tag }}</span>
                {% endfor %}
            </div>
        </div>
        {% endif %}

        <!-- Generated images -->
        {% set upload_dir = 'static/uploads/presets/' + preset.slug %}
        {% if preset.image_path or True %}
        <div class="card">
            <div class="card-header py-2"><strong>Generated Images</strong></div>
            <div class="card-body py-2">
                <div class="row g-2" id="generated-images">
                    {% if preset.image_path %}
                    <div class="col-4 col-md-3">
                        <img src="{{ url_for('static', filename='uploads/' + preset.image_path) }}"
                             class="img-fluid rounded" style="cursor:pointer"
                             data-preview-path="{{ preset.image_path }}"
                             onclick="selectPreview('{{ preset.image_path }}', this.src)">
                    </div>
                    {% endif %}
                </div>
                <p id="no-images-msg" class="text-muted small mt-2 {% if preset.image_path %}d-none{% endif %}">No generated images yet.</p>
            </div>
        </div>
        {% endif %}
    </div>
</div>
{% endblock %}

{% block scripts %}
<script src="/static/js/detail-common.js"></script>
<script>
document.addEventListener('DOMContentLoaded', () => {
    initDetailPage({
        jsonEditorUrl: "{{ url_for('save_preset_json', slug=preset.slug) }}"
    });
});
</script>
{% endblock %}
