{% extends "layout.html" %}

{% block content %}
<!-- Generate Character Modal -->
<div class="modal fade" id="generateCharModal" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Generate Character from Look</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <form method="POST" action="{{ url_for('generate_character_from_look', slug=look.slug) }}">
                <div class="modal-body">
                    <div class="mb-3">
                        <label class="form-label">Character Name</label>
                        <input type="text" class="form-control" name="character_name"
                               value="{{ look.name }}" required>
                    </div>
                    <div class="form-check">
                        <input class="form-check-input" type="checkbox" name="use_llm" id="use_llm" checked>
                        <label class="form-check-label" for="use_llm">
                            Use LLM to generate detailed character
                        </label>
                    </div>
                    <div class="form-text text-muted">
                        The look's LoRA will be automatically assigned to the new character.
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                    <button type="submit" class="btn btn-primary">Generate Character</button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- 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 — {{ look.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">{{ look.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="row">
    <div class="col-md-4">
        <div class="card mb-4">
            <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 look.image_path %}
                <img src="{{ url_for('static', filename='uploads/' + look.image_path) }}" alt="{{ look.name }}" class="img-fluid"
                     data-preview-path="{{ look.image_path }}">
                {% else %}
                <span class="text-muted">No Image Attached</span>
                {% endif %}
            </div>
            <div class="card-body">
                {# Character Selector #}
                <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 --</option>
                        <option value="__random__" {% if selected_character == '__random__' or not selected_character %}selected{% endif %}>🎲 Random Character</option>
                        {% for char in characters %}
                        <option value="{{ char.slug }}"
                            {% if selected_character == char.character_id or selected_character == char.slug %}selected
                            {% elif not selected_character and look.character_id == char.character_id %}selected
                            {% endif %}>{{ char.name }}</option>
                        {% endfor %}
                    </select>
                    <div class="form-text">Defaults to the first linked character.</div>
                </div>

                {# 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" form="generate-form">{{ 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" form="generate-form">{{ extra_negative or '' }}</textarea>
                </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" form="generate-form" 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" form="generate-form" data-requires="comfyui">Generate Preview</button>
                    <button type="button" class="btn btn-outline-info" id="endless-btn" onclick="window._endlessStart()" data-requires="comfyui">Endless</button>
                    <button type="button" class="btn btn-danger d-none" id="endless-stop-btn" onclick="window._endlessStop()">Stop Endless</button>
                    <small class="text-muted d-none" id="endless-counter"></small>
                    <button type="submit" form="generate-form" formaction="{{ url_for('save_look_defaults', slug=look.slug) }}" class="btn btn-sm btn-outline-secondary mt-2">Save as Default Selection</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" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100">
                <div id="progress-bar" class="progress-bar progress-bar-striped progress-bar-animated" style="width: 0%">0%</div>
            </div>
        </div>

        <div class="card mb-4 {% if preview_image %}border-success{% else %}border-secondary d-none{% endif %}" id="preview-card">
            <div class="card-header {% if preview_image %}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_look_cover_from_preview', slug=look.slug) }}" method="post" class="m-0" id="replace-cover-form">
                    <input type="hidden" name="preview_path" id="preview-path" value="{{ preview_image or '' }}">
                    <button type="submit" class="btn btn-sm btn-outline-light" id="replace-cover-btn" {% if not preview_image %}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;" onclick="openGallery([this.querySelector('img') ? this.querySelector('img').src : this.src || ''], 0)">
                    <img id="preview-img" src="{{ url_for('static', filename='uploads/' + preview_image) if preview_image else '' }}" alt="Preview" class="img-fluid">
                </div>
            </div>
        </div>

        {% set tags = look.data.tags if look.data.tags is mapping else {} %}
        {% if tags %}
        <div class="card mb-4">
            <div class="card-header bg-dark text-white"><span>Tags</span></div>
            <div class="card-body">
                {% if tags.origin_series %}<span class="badge bg-info">{{ tags.origin_series }}</span>{% endif %}
                {% if tags.origin_type %}<span class="badge bg-primary">{{ tags.origin_type }}</span>{% endif %}
                {% if look.is_nsfw %}<span class="badge bg-danger">NSFW</span>{% endif %}
                {% if look.is_favourite %}<span class="badge bg-warning text-dark">&#9733; Favourite</span>{% endif %}
            </div>
        </div>
        {% endif %}
    </div>

    <div class="col-md-8">
        <div class="d-flex justify-content-between align-items-center mb-4">
            <div>
                <h1 class="mb-0">
                    {{ look.name }}
                    <button class="btn btn-sm btn-link text-decoration-none fav-toggle-btn" data-url="/look/{{ look.slug }}/favourite" title="Toggle favourite">
                        <span style="font-size:1.2rem;">{% if look.is_favourite %}&#9733;{% else %}&#9734;{% endif %}</span>
                    </button>
                    {% if look.is_nsfw %}<span class="badge bg-danger" style="font-size:0.6rem;vertical-align:middle;">NSFW</span>{% endif %}
                </h1>
                {% if linked_character_ids %}
                <small class="text-muted">
                    Linked to:
                    {% for char_id in linked_character_ids %}
                    <a href="{{ url_for('detail', slug=char_id) }}" class="badge bg-primary text-decoration-none">{{ char_id.replace('_', ' ').title() }}</a>{% if not loop.last %} {% endif %}
                    {% endfor %}
                </small>
                {% endif %}
                <br>
                <a href="{{ url_for('edit_look', slug=look.slug) }}" class="btn btn-sm btn-link text-decoration-none">Edit Profile</a>
            </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>
                <button type="button" class="btn btn-outline-warning" id="regenerate-tags-btn" onclick="regenerateTags('looks', '{{ look.slug }}')">Regenerate Tags</button>
                <button type="button" class="btn btn-accent" data-bs-toggle="modal" data-bs-target="#generateCharModal">
                    <i class="bi bi-person-plus"></i> Generate Character
                </button>
                <a href="{{ url_for('transfer_resource', category='looks', slug=look.slug) }}" class="btn btn-outline-primary">Transfer</a>
                <a href="{{ url_for('looks_index') }}" class="btn btn-outline-secondary">Back to Library</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>
            {% if look.data.get('lora', {}).get('lora_name', '') != '' %}
            <li class="nav-item" role="presentation">
                <button class="nav-link" id="strengths-tab" data-bs-toggle="tab" data-bs-target="#strengths-pane" type="button" role="tab">Strengths</button>
            </li>
            {% endif %}
        </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_look_image', slug=look.slug) }}" method="post">
            {# Positive prompt #}
            {% if look.data.positive %}
            <div class="card mb-4">
                <div class="card-header bg-light d-flex justify-content-between align-items-center">
                    <strong>Positive Prompt</strong>
                    <div class="form-check form-switch">
                        <input class="form-check-input" type="checkbox" name="include_field" value="positive" id="includePositive" form="generate-form"
                        {% if preferences is not none %}
                            {% if 'positive' in preferences %}checked{% endif %}
                        {% elif look.default_fields is not none %}
                            {% if 'positive' in look.default_fields %}checked{% endif %}
                        {% else %}
                            checked
                        {% endif %}>
                        <label class="form-check-label small" for="includePositive">Include</label>
                    </div>
                </div>
                <div class="card-body">
                    <p class="mb-0">{{ look.data.positive }}</p>
                </div>
            </div>
            {% endif %}

            {# Negative prompt #}
            {% if look.data.negative %}
            <div class="card mb-4">
                <div class="card-header bg-light"><strong>Negative Prompt</strong></div>
                <div class="card-body">
                    <p class="mb-0 text-muted">{{ look.data.negative }}</p>
                </div>
            </div>
            {% endif %}

            {# LoRA section #}
            {% set lora = look.data.get('lora', {}) %}
            {% if lora %}
            <div class="card mb-4">
                <div class="card-header bg-light"><strong>LoRA</strong></div>
                <div class="card-body">
                    <dl class="row mb-0">
                        {% for key, value in lora.items() %}
                        {% set is_default = look.default_fields is not none and ('lora::' ~ key) in look.default_fields %}
                                <dt class="col-sm-4 text-capitalize {% if is_default %}text-accent{% endif %}">
                                    <input class="form-check-input me-1" type="checkbox" name="include_field" value="lora::{{ key }}"
{% if preferences is not none %}
                                {% if 'lora::' + key in preferences %}checked{% endif %}
                            {% elif look.default_fields is not none %}
                                {% if is_default %}checked{% endif %}
                            {% else %}
                                {% if value %}checked{% endif %}
                            {% endif %}>
                                    {{ key.replace('_', ' ') }}
                                    {% if is_default %}<span class="badge bg-primary ms-1" style="font-size: 0.55rem; vertical-align: middle;">DEF</span>{% endif %}
                                </dt>
                                <dd class="col-sm-8 {% if is_default %}text-accent{% endif %}">{{ value if value else '--' }}</dd>
                        {% endfor %}
                    </dl>
                </div>
            </div>
            {% endif %}
        </form>

            </div>{# /settings-pane #}

            {% set sg_has_lora = look.data.get('lora', {}).get('lora_name', '') != '' %}
            {% if sg_has_lora %}
            <div class="tab-pane fade" id="strengths-pane" role="tabpanel">
                {% set sg_entity = look %}
                {% set sg_category = 'looks' %}
                {% include 'partials/strengths_gallery.html' %}
            </div>
            {% endif %}
        </div>{# /tab-content #}
    </div>
</div>
{% endblock %}

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