{% 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 — {{ ckpt.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="20" spellcheck="false"></textarea>
                </div>
                <script type="application/json" id="json-raw-data">{{ ckpt.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="row">
    <div class="col-md-4">
        <div class="card mb-4">
            <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 ckpt.image_path %}
                <img src="{{ url_for('static', filename='uploads/' + ckpt.image_path) }}" alt="{{ ckpt.name }}" class="img-fluid">
                {% else %}
                <div class="d-flex align-items-center justify-content-center bg-light" style="height: 400px;">
                    <span class="text-muted">No Image Attached</span>
                </div>
                {% endif %}
            </div>
            <div class="card-body">
                <form action="{{ url_for('upload_checkpoint_image', slug=ckpt.slug) }}" method="post" enctype="multipart/form-data">
                    <div class="mb-3">
                        <label for="image" class="form-label text-muted small">Update Cover Image</label>
                        <input class="form-control form-control-sm" type="file" id="image" name="image" required>
                    </div>
                    <button type="submit" class="btn btn-sm btn-outline-primary w-100 mb-2">Upload</button>
                </form>

                <hr>

                <div class="mb-3">
                    <label for="character_select" class="form-label">Preview with Character</label>
                    <select class="form-select" id="character_select" name="character_slug" form="generate-form">
                        <option value="">-- No Character (Generic) --</option>
                        <option value="__random__" {% if selected_character == '__random__' %}selected{% endif %}>🎲 Random Character</option>
                        {% for char in characters %}
                        <option value="{{ char.slug }}" {% if selected_character == char.slug %}selected{% endif %}>{{ char.name }}</option>
                        {% endfor %}
                    </select>
                </div>

                <div class="d-grid gap-2">
                    <button type="submit" name="action" value="preview" class="btn btn-success" form="generate-form">Generate Preview</button>
                </div>
            </div>
        </div>

        <div id="progress-container" class="mb-4 d-none">
            <label id="progress-label" class="form-label">Generating...</label>
            <div class="progress">
                <div id="progress-bar" class="progress-bar progress-bar-striped progress-bar-animated" style="width: 0%">0%</div>
            </div>
        </div>

        {% if preview_image %}
        <div class="card mb-4 border-success">
            <div class="card-header bg-success text-white d-flex justify-content-between align-items-center p-2">
                <small>Latest Preview</small>
                <form action="{{ url_for('replace_checkpoint_cover_from_preview', slug=ckpt.slug) }}" method="post" class="m-0">
                    <button type="submit" class="btn btn-sm btn-outline-light">Replace Cover</button>
                </form>
            </div>
            <div class="card-body p-0">
                <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').src)">
                    <img id="preview-img" src="{{ url_for('static', filename='uploads/' + preview_image) }}" alt="Preview" class="img-fluid">
                </div>
            </div>
        </div>
        {% else %}
        <div class="card mb-4 border-secondary d-none" id="preview-card">
            <div class="card-header bg-secondary text-white d-flex justify-content-between align-items-center p-2">
                <small>Latest Preview</small>
                <form action="{{ url_for('replace_checkpoint_cover_from_preview', slug=ckpt.slug) }}" method="post" class="m-0" id="replace-cover-form">
                    <button type="submit" class="btn btn-sm btn-outline-light" id="replace-cover-btn" disabled>Replace Cover</button>
                </form>
            </div>
            <div class="card-body p-0">
                <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').src)">
                    <img id="preview-img" src="" alt="Preview" class="img-fluid">
                </div>
            </div>
        </div>
        {% endif %}
    </div>

    <div class="col-md-8">
        <div class="d-flex justify-content-between align-items-center mb-3">
            <div>
                <h1 class="mb-0">{{ ckpt.name }}</h1>
            </div>
            <div class="d-flex gap-2">
                <button type="button" class="btn btn-outline-secondary" data-bs-toggle="modal" data-bs-target="#jsonEditorModal">Edit JSON</button>
                <a href="{{ url_for('checkpoints_index') }}" class="btn btn-outline-secondary">Back to Gallery</a>
            </div>
        </div>

        <ul class="nav nav-tabs mb-4" id="detailTabs" role="tablist">
            <li class="nav-item" role="presentation">
                <button class="nav-link active" id="settings-tab" data-bs-toggle="tab" data-bs-target="#settings-pane" type="button" role="tab">Settings</button>
            </li>
            <li class="nav-item" role="presentation">
                <button class="nav-link" id="previews-tab" data-bs-toggle="tab" data-bs-target="#previews-pane" type="button" role="tab">
                    Previews{% if existing_previews %} <span class="badge bg-secondary">{{ existing_previews|length }}</span>{% endif %}
                </button>
            </li>
        </ul>

        <div class="tab-content" id="detailTabContent">
            <div class="tab-pane fade show active" id="settings-pane" role="tabpanel">
                <form id="generate-form" action="{{ url_for('generate_checkpoint_image', slug=ckpt.slug) }}" method="post">
                    {% set d = ckpt.data or {} %}

                    <div class="card mb-4">
                        <div class="card-header bg-light">
                            <strong>Checkpoint Info</strong>
                        </div>
                        <div class="card-body">
                            <dl class="row mb-0">
                                <dt class="col-sm-3">Name</dt>
                                <dd class="col-sm-9">{{ ckpt.name }}</dd>

                                <dt class="col-sm-3">Family</dt>
                                <dd class="col-sm-9">{{ ckpt.checkpoint_path.split('/')[0] }}</dd>

                                <dt class="col-sm-3">File</dt>
                                <dd class="col-sm-9 text-muted small">
                                    <code>{{ ckpt.checkpoint_path }}</code>
                                </dd>
                            </dl>
                        </div>
                    </div>

                    <div class="card mb-4">
                        <div class="card-header bg-light">
                            <strong>Generation Settings</strong>
                        </div>
                        <div class="card-body">
                            <dl class="row mb-0">
                                <dt class="col-sm-4">Steps</dt>
                                <dd class="col-sm-8">{{ d.get('steps', 25) }}</dd>

                                <dt class="col-sm-4">CFG</dt>
                                <dd class="col-sm-8">{{ d.get('cfg', 5) }}</dd>

                                <dt class="col-sm-4">Sampler</dt>
                                <dd class="col-sm-8"><code>{{ d.get('sampler_name', 'euler_ancestral') }}</code></dd>

                                <dt class="col-sm-4">Scheduler</dt>
                                <dd class="col-sm-8"><code>{{ d.get('scheduler', 'normal') }}</code></dd>

                                <dt class="col-sm-4">VAE</dt>
                                <dd class="col-sm-8">
                                    {% if d.get('vae', 'integrated') == 'integrated' %}
                                    <span class="badge bg-secondary">Integrated</span>
                                    {% else %}
                                    <code class="small">{{ d.get('vae') }}</code>
                                    {% endif %}
                                </dd>
                            </dl>
                        </div>
                    </div>

                    <div class="card mb-4">
                        <div class="card-header bg-light">
                            <strong>Base Prompts</strong>
                        </div>
                        <div class="card-body">
                            <dl class="row mb-0">
                                <dt class="col-sm-4">Positive</dt>
                                <dd class="col-sm-8 small"><code>{{ d.get('base_positive', '') or '--' }}</code></dd>

                                <dt class="col-sm-4">Negative</dt>
                                <dd class="col-sm-8 small"><code>{{ d.get('base_negative', '') or '--' }}</code></dd>
                            </dl>
                        </div>
                    </div>
                </form>
            </div>

            <div class="tab-pane fade" id="previews-pane" role="tabpanel">
                <div class="d-flex justify-content-between align-items-center mb-3">
                    <span class="text-muted small">{{ existing_previews|length }} preview(s)</span>
                    <div class="d-flex gap-2">
                        <button type="button" id="generate-all-btn" class="btn btn-primary btn-sm">Generate All Characters</button>
                        <button type="button" id="stop-all-btn" class="btn btn-danger btn-sm d-none">Stop</button>
                    </div>
                </div>
                <div id="batch-progress" class="mb-3 d-none">
                    <label id="batch-label" class="form-label small fw-semibold"></label>
                    <div class="progress" style="height: 8px;">
                        <div id="batch-bar" class="progress-bar progress-bar-striped progress-bar-animated" style="width: 0%"></div>
                    </div>
                </div>
                <div id="preview-gallery" class="row row-cols-2 row-cols-md-3 g-2">
                    {% for img in existing_previews %}
                    <div class="col">
                        <img src="{{ url_for('static', filename='uploads/' + img) }}"
                             class="img-fluid rounded"
                             style="cursor: pointer; aspect-ratio: 1; object-fit: cover; width: 100%;"
                             onclick="showImage(this.src)"
                             data-bs-toggle="modal" data-bs-target="#imageModal">
                    </div>
                    {% else %}
                    <div class="col-12 text-muted small" id="gallery-empty">No previews yet. Generate some!</div>
                    {% endfor %}
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block scripts %}
<script>
    document.addEventListener('DOMContentLoaded', () => {
        const form = document.getElementById('generate-form');
        const progressBar = document.getElementById('progress-bar');
        const progressContainer = document.getElementById('progress-container');
        const progressLabel = document.getElementById('progress-label');
        const previewCard = document.getElementById('preview-card');
        const previewImg = document.getElementById('preview-img');

        let currentJobId = null;

        async function waitForJob(jobId) {
            return new Promise((resolve, reject) => {
                const poll = setInterval(async () => {
                    try {
                        const resp = await fetch(`/api/queue/${jobId}/status`);
                        const data = await resp.json();
                        if (data.status === 'done') { clearInterval(poll); resolve(data); }
                        else if (data.status === 'failed' || data.status === 'removed') { clearInterval(poll); reject(new Error(data.error || 'Job failed')); }
                        else if (data.status === 'processing') progressLabel.textContent = 'Generating…';
                        else progressLabel.textContent = 'Queued…';
                    } catch (err) {}
                }, 1500);
            });
        }

        form.addEventListener('submit', async (e) => {
            const submitter = e.submitter;
            if (!submitter || submitter.value !== 'preview') return;
            e.preventDefault();
            const formData = new FormData(form);
            formData.append('action', 'preview');
            progressContainer.classList.remove('d-none');
            progressBar.style.width = '100%'; progressBar.textContent = '';
            progressBar.classList.add('progress-bar-striped', 'progress-bar-animated');
            progressLabel.textContent = 'Queuing…';
            try {
                const response = await fetch(form.getAttribute('action'), {
                    method: 'POST', body: formData, headers: { 'X-Requested-With': 'XMLHttpRequest' }
                });
                const data = await response.json();
                if (data.error) { alert('Error: ' + data.error); progressContainer.classList.add('d-none'); return; }
                currentJobId = data.job_id;
                const jobResult = await waitForJob(currentJobId);
                currentJobId = null;
                if (jobResult.result && jobResult.result.image_url) {
                    previewImg.src = jobResult.result.image_url;
                    if (previewCard) previewCard.classList.remove('d-none');
                    const replaceBtn = document.getElementById('replace-cover-btn');
                    if (replaceBtn) replaceBtn.disabled = false;
                }
            } catch (err) { console.error(err); alert('Generation failed: ' + err.message); }
            finally { progressContainer.classList.add('d-none'); progressBar.classList.remove('progress-bar-striped', 'progress-bar-animated'); }
        });

        // Batch: Generate All Characters
        const allCharacters = [
            {% for char in characters %}{ slug: "{{ char.slug }}", name: {{ char.name | tojson }} },
            {% endfor %}
        ];

        let stopBatch = false;
        const generateAllBtn = document.getElementById('generate-all-btn');
        const stopAllBtn = document.getElementById('stop-all-btn');
        const batchProgress = document.getElementById('batch-progress');
        const batchLabel = document.getElementById('batch-label');
        const batchBar = document.getElementById('batch-bar');

        function addToPreviewGallery(imageUrl, charName) {
            const gallery = document.getElementById('preview-gallery');
            const placeholder = document.getElementById('gallery-empty');
            if (placeholder) placeholder.remove();
            const col = document.createElement('div');
            col.className = 'col';
            col.innerHTML = `<div class="position-relative">
                <img src="${imageUrl}" class="img-fluid rounded"
                     style="cursor: pointer; aspect-ratio: 1; object-fit: cover; width: 100%;"
                     onclick="showImage(this.src)"
                     data-bs-toggle="modal" data-bs-target="#imageModal"
                     title="${charName}">
                <div class="position-absolute bottom-0 start-0 w-100 bg-dark bg-opacity-50 text-white p-1 rounded-bottom" style="font-size: 0.7rem; line-height: 1.2;">${charName}</div>
            </div>`;
            gallery.insertBefore(col, gallery.firstChild);
            const badge = document.querySelector('#previews-tab .badge');
            if (badge) badge.textContent = parseInt(badge.textContent || '0') + 1;
            else document.getElementById('previews-tab').insertAdjacentHTML('beforeend', ' <span class="badge bg-secondary">1</span>');
        }

        generateAllBtn.addEventListener('click', async () => {
            if (allCharacters.length === 0) { alert('No characters available.'); return; }
            stopBatch = false;
            generateAllBtn.disabled = true;
            stopAllBtn.classList.remove('d-none');
            batchProgress.classList.remove('d-none');
            bootstrap.Tab.getOrCreateInstance(document.getElementById('previews-tab')).show();
            for (let i = 0; i < allCharacters.length; i++) {
                if (stopBatch) break;
                const char = allCharacters[i];
                batchBar.style.width = `${Math.round((i / allCharacters.length) * 100)}%`;
                batchLabel.textContent = `Generating ${char.name} (${i + 1} / ${allCharacters.length})`;
                const genForm = document.getElementById('generate-form');
                const fd = new FormData();
                fd.append('character_slug', char.slug);
                fd.append('action', 'preview');
                try {
                    progressContainer.classList.remove('d-none');
                    progressBar.style.width = '100%'; progressBar.textContent = '';
                    progressBar.classList.add('progress-bar-striped', 'progress-bar-animated');
                    progressLabel.textContent = `${char.name}: Queuing…`;
                    const resp = await fetch(genForm.getAttribute('action'), {
                        method: 'POST', body: fd, headers: { 'X-Requested-With': 'XMLHttpRequest' }
                    });
                    const data = await resp.json();
                    if (data.error) { console.error(`Error for ${char.name}:`, data.error); progressContainer.classList.add('d-none'); continue; }
                    currentJobId = data.job_id;
                    const jobResult = await waitForJob(currentJobId);
                    currentJobId = null;
                    if (jobResult.result && jobResult.result.image_url) {
                        addToPreviewGallery(jobResult.result.image_url, char.name);
                        previewImg.src = jobResult.result.image_url;
                        if (previewCard) previewCard.classList.remove('d-none');
                    }
                } catch (err) { console.error(`Failed for ${char.name}:`, err); currentJobId = null; }
                finally { progressContainer.classList.add('d-none'); progressBar.classList.remove('progress-bar-striped', 'progress-bar-animated'); }
            }
            batchBar.style.width = '100%';
            batchLabel.textContent = stopBatch ? 'Stopped.' : 'Complete!';
            generateAllBtn.disabled = false;
            stopAllBtn.classList.add('d-none');
            setTimeout(() => { batchProgress.classList.add('d-none'); batchBar.style.width = '0%'; }, 3000);
        });

        stopAllBtn.addEventListener('click', () => {
            stopBatch = true;
            stopAllBtn.classList.add('d-none');
            batchLabel.textContent = 'Stopping after current generation...';
        });

        // JSON Editor
        initJsonEditor('{{ url_for("save_checkpoint_json", slug=ckpt.slug) }}');
    });

    function showImage(src) {
        if (src) document.getElementById('modalImage').src = src;
    }
</script>
{% endblock %}
