{% 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>

<!-- Image Modal -->
<div class="modal fade" id="imageModal" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog modal-xl modal-dialog-centered">
        <div class="modal-content bg-transparent border-0">
            <div class="modal-body p-0 text-center">
                <img id="modalImage" src="" alt="Enlarged Image" class="img-fluid" style="max-height: 90vh;">
            </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;"
                 data-bs-toggle="modal" data-bs-target="#imageModal"
                 onclick="showImage(this.querySelector('img') ? this.querySelector('img').src : '')">
                {% 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 action="{{ url_for('upload_preset_image', slug=preset.slug) }}" method="post" enctype="multipart/form-data" class="mb-3">
                    <label class="form-label text-muted small">Update Cover Image</label>
                    <div class="input-group input-group-sm">
                        <input class="form-control" type="file" name="image" required>
                        <button type="submit" class="btn btn-outline-primary">Upload</button>
                    </div>
                </form>

                <form id="generate-form" action="{{ url_for('generate_preset_image', slug=preset.slug) }}" method="post">
                    <div class="d-grid gap-2">
                        <button type="submit" name="action" value="preview" class="btn btn-success">Generate Preview</button>
                        <button type="submit" name="action" value="replace" class="btn btn-outline-warning btn-sm">Generate &amp; Set Cover</button>
                    </div>
                </form>
            </div>
        </div>

        <!-- Selected Preview -->
        <div class="card mb-3" id="preview-pane" {% if not preview_path %}style="display:none"{% endif %}>
            <div class="card-header d-flex justify-content-between align-items-center py-1">
                <small class="fw-semibold">Selected Preview</small>
            </div>
            <div class="card-body p-1">
                <img id="preview-img" src="{{ url_for('static', filename='uploads/' + preview_path) if preview_path else '' }}"
                     class="img-fluid rounded" alt="Preview">
            </div>
            <div class="card-footer p-2">
                <form action="{{ url_for('replace_preset_cover_from_preview', slug=preset.slug) }}" method="post">
                    <input type="hidden" name="preview_path" id="preview-path" value="{{ preview_path or '' }}">
                    <button type="submit" class="btn btn-sm btn-warning w-100">Set as Cover</button>
                </form>
            </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_specs','hair','eyes','hands','arms','torso','pelvis','legs','feet','extra'] %}
                        <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 ['full_body','headwear','top','bottom','legwear','footwear','hands','gloves','accessories'] %}
                        <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',   ['full_body','additional','head','eyes','arms','hands']),
                ('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>
        </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>
// Job polling
let currentJobId = null;

document.getElementById('generate-form').addEventListener('submit', function(e) {
    e.preventDefault();
    const btn = e.submitter;
    const actionVal = btn.value;
    const formData = new FormData(this);
    formData.set('action', actionVal);

    btn.disabled = true;
    btn.textContent = 'Generating...';

    fetch(this.action, {
        method: 'POST',
        headers: {'X-Requested-With': 'XMLHttpRequest'},
        body: formData
    })
    .then(r => r.json())
    .then(data => {
        if (data.job_id) {
            currentJobId = data.job_id;
            pollJob(currentJobId, btn, actionVal);
        } else {
            btn.disabled = false;
            btn.textContent = btn.dataset.label || 'Generate Preview';
            alert('Error: ' + (data.error || 'Unknown error'));
        }
    })
    .catch(() => { btn.disabled = false; });
});

function pollJob(jobId, btn, actionVal) {
    fetch('/api/queue/' + jobId + '/status')
    .then(r => r.json())
    .then(data => {
        if (data.status === 'done' && data.result) {
            btn.disabled = false;
            btn.textContent = btn.getAttribute('data-orig') || btn.textContent.replace('Generating...', 'Generate Preview');
            // Add to gallery
            const img = document.createElement('img');
            img.src = data.result.image_url;
            img.className = 'img-fluid rounded';
            img.style.cursor = 'pointer';
            img.dataset.previewPath = data.result.relative_path;
            img.addEventListener('click', () => selectPreview(data.result.relative_path, img.src));
            const col = document.createElement('div');
            col.className = 'col-4 col-md-3';
            col.appendChild(img);
            document.getElementById('generated-images').prepend(col);
            document.getElementById('no-images-msg')?.classList.add('d-none');
            selectPreview(data.result.relative_path, data.result.image_url);
        } else if (data.status === 'failed') {
            btn.disabled = false;
            btn.textContent = 'Generate Preview';
            alert('Generation failed: ' + (data.error || 'Unknown error'));
        } else {
            setTimeout(() => pollJob(jobId, btn, actionVal), 1500);
        }
    })
    .catch(() => setTimeout(() => pollJob(jobId, btn, actionVal), 3000));
}

function selectPreview(relativePath, imageUrl) {
    document.getElementById('preview-path').value = relativePath;
    document.getElementById('preview-img').src = imageUrl;
    document.getElementById('preview-pane').style.display = '';
}

function showImage(src) {
    if (src) document.getElementById('modalImage').src = src;
}

// Delegate click on generated images
document.addEventListener('click', function(e) {
    const img = e.target.closest('img[data-preview-path]');
    if (img) selectPreview(img.dataset.previewPath, img.src);
});

// JSON editor
initJsonEditor("{{ url_for('save_preset_json', slug=preset.slug) }}");
</script>
{% endblock %}
