{% extends "layout.html" %}

{% block content %}
<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 character.image_path %}
                <img src="{{ url_for('static', filename='uploads/' + character.image_path) }}" alt="{{ character.name }}" class="img-fluid" data-preview-path="{{ character.image_path }}">
                {% else %}
                <span class="text-muted">No Image Attached</span>
                {% endif %}
            </div>
            <div class="card-body">
                {# 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_defaults', slug=character.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-label="Generation Progress" 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_cover_from_preview', slug=character.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 = character.data.tags if character.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 character.is_nsfw %}<span class="badge bg-danger">NSFW</span>{% endif %}
                {% if character.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">
                    {{ character.name }}
                    <button class="btn btn-sm btn-link text-decoration-none fav-toggle-btn" data-url="/character/{{ character.slug }}/favourite" title="Toggle favourite">
                        <span style="font-size:1.2rem;">{% if character.is_favourite %}&#9733;{% else %}&#9734;{% endif %}</span>
                    </button>
                    {% if character.is_nsfw %}<span class="badge bg-danger" style="font-size:0.6rem;vertical-align:middle;">NSFW</span>{% endif %}
                </h1>
                <div class="btn-group" role="group">
                    <a href="{{ url_for('edit_character', slug=character.slug) }}" class="btn btn-sm btn-link text-decoration-none">Edit Profile</a>
                    <a href="{{ url_for('transfer_character', slug=character.slug) }}" class="btn btn-sm btn-warning text-decoration-none">
                        <i class="bi bi-arrow-left-right"></i> Transfer
                    </a>
                </div>
            </div>
            <div class="d-flex gap-2">
                <button type="button" class="btn btn-outline-warning" id="regenerate-tags-btn" onclick="regenerateTags('characters', '{{ character.slug }}')">Regenerate Tags</button>
                <a href="/" 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 character.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">

        <!-- Outfit Management Section -->
        {% set outfits = character.get_available_outfits() %}
        <div class="card mb-4 border-primary">
            <div class="card-header bg-primary text-white d-flex justify-content-between align-items-center">
                <span><i class="bi bi-shirt"></i> Wardrobe</span>
                <span class="badge bg-light text-primary">{{ outfits|length }} outfit(s)</span>
            </div>
            <div class="card-body">
                <!-- Active Outfit Selector -->
                <form action="{{ url_for('switch_outfit', slug=character.slug) }}" method="post" class="mb-3">
                    <label class="form-label">Active Outfit</label>
                    <div class="input-group">
                        <select name="outfit" class="form-select">
                            {% for outfit in outfits %}
                            <option value="{{ outfit.outfit_id }}" {% if outfit.outfit_id == character.active_outfit %}selected{% endif %}>
                                {{ outfit.name }}
                                {% if outfit.source == 'assigned' %}(External){% endif %}
                            </option>
                            {% endfor %}
                        </select>
                        <button type="submit" class="btn btn-primary">Switch</button>
                    </div>
                </form>
                
                <!-- Assign New Outfit -->
                <form action="{{ url_for('assign_outfit', slug=character.slug) }}" method="post">
                    <label class="form-label">Add Outfit to Wardrobe</label>
                    <div class="input-group">
                        <select name="outfit_id" class="form-select">
                            <option value="">-- Select Outfit --</option>
                            {% for outfit in all_outfits %}
                                {% if outfit.outfit_id not in character.assigned_outfit_ids and outfit.outfit_id != 'default' %}
                                <option value="{{ outfit.outfit_id }}">{{ outfit.name }}</option>
                                {% endif %}
                            {% endfor %}
                        </select>
                        <button type="submit" class="btn btn-success">Assign</button>
                    </div>
                </form>
                
                <!-- Currently Assigned Outfits -->
                {% if character.assigned_outfit_ids %}
                <div class="mt-3">
                    <label class="form-label">Assigned External Outfits</label>
                    <div class="d-flex flex-wrap gap-2">
                        {% for outfit_id in character.assigned_outfit_ids %}
                          {% set outfit = get_outfit_by_id(outfit_id) %}
                          {% if outfit %}
                          <span class="badge bg-secondary d-flex align-items-center">
                            {{ outfit.name }}
                            <form action="{{ url_for('unassign_outfit', slug=character.slug, outfit_id=outfit_id) }}" method="post" class="d-inline ms-1">
                                <button type="submit" class="btn btn-sm btn-link text-white p-0" style="text-decoration: none;" onclick="return confirm('Unassign this outfit?')">&times;</button>
                            </form>
                          </span>
                          {% endif %}
                        {% endfor %}
                    </div>
                </div>
                {% endif %}
            </div>
        </div>

        <form id="generate-form" action="{{ url_for('generate_image', slug=character.slug) }}" method="post">
            {% for section, details in character.data.items() %}
                {% if section == 'wardrobe' %}
                    {# Special handling for wardrobe - show active outfit #}
                    {% set active_wardrobe = character.get_active_wardrobe() %}
                    <div class="card mb-4">
                        <div class="card-header bg-light d-flex justify-content-between align-items-center">
                            <strong>
                                Wardrobe
                                {% if outfits|length > 1 %}
                                <span class="badge bg-secondary ms-2">{{ character.active_outfit or 'default' }}</span>
                                {% endif %}
                            </strong>
                            {% if outfits|length > 1 %}
                            <a href="{{ url_for('edit_character', slug=character.slug) }}#outfits" class="btn btn-sm btn-outline-secondary">Manage Outfits</a>
                            {% endif %}
                        </div>
                        <div class="card-body">
                            <dl class="row mb-0">
                                {% for key, value in active_wardrobe.items() %}
                                {% set is_default = character.default_fields is not none and ('wardrobe::' ~ key) in character.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="wardrobe::{{ key }}"
                                    {% if preferences is not none %}
                                        {% if 'wardrobe::' + key in preferences %}checked{% endif %}
                                    {% elif character.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>
                {% elif section not in ['character_id', 'tags', 'name'] and details is mapping %}
                <div class="card mb-4">
                    <div class="card-header bg-light text-capitalize"><strong>{{ section.replace('_', ' ') }}</strong></div>
                    <div class="card-body">
                        <dl class="row mb-0">
                            {% if section == 'identity' %}
                            {% set is_name_default = character.default_fields is not none and 'special::name' in character.default_fields %}
                            <dt class="col-sm-4 text-capitalize {% if is_name_default %}text-accent{% endif %}">
                                <input class="form-check-input me-1" type="checkbox" name="include_field" value="special::name"
                                {% if preferences is not none %}
                                    {% if 'special::name' in preferences %}checked{% endif %}
                                {% elif character.default_fields is not none %}
                                    {% if is_name_default %}checked{% endif %}
                                {% else %}
                                    checked
                                {% endif %}>
                                Character ID
                                {% if is_name_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_name_default %}text-accent{% endif %}">{{ character.character_id }}</dd>
                            {% endif %}

                            {% for key, value in details.items() %}
                            {% set is_default = character.default_fields is not none and (section ~ '::' ~ key) in character.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="{{ section }}::{{ key }}"
                                {% if preferences is not none %}
                                    {% if section + '::' + key in preferences %}checked{% endif %}
                                {% elif character.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 %}
            {% endfor %}
        </form>

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

            {% set sg_has_lora = character.data.get('lora', {}).get('lora_name', '') != '' %}
            {% if sg_has_lora %}
            <div class="tab-pane fade" id="strengths-pane" role="tabpanel">
                {% set sg_entity = character %}
                {% set sg_category = 'characters' %}
                {% 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({});
});
</script>
{% endblock %}
