{% extends "base.html" %}

{% block title %}Combat Debug - Dev Tools{% endblock %}

{% block extra_head %}
<style>
    .dev-banner {
        background: #dc2626;
        color: white;
        padding: 0.5rem 1rem;
        text-align: center;
        font-weight: bold;
        position: sticky;
        top: 0;
        z-index: 100;
    }

    .combat-container {
        max-width: 1400px;
        margin: 1rem auto;
        padding: 0 1rem;
        display: grid;
        grid-template-columns: 280px 1fr 300px;
        gap: 1rem;
    }

    @media (max-width: 1200px) {
        .combat-container {
            grid-template-columns: 250px 1fr;
        }
        .right-panel {
            display: none;
        }
    }

    @media (max-width: 768px) {
        .combat-container {
            grid-template-columns: 1fr;
        }
        .left-panel {
            display: none;
        }
    }

    .panel {
        background: rgba(30, 30, 40, 0.9);
        border: 1px solid #4a4a5a;
        border-radius: 8px;
        padding: 1rem;
    }

    .panel h3 {
        color: #f59e0b;
        margin-top: 0;
        margin-bottom: 1rem;
        font-size: 1rem;
        border-bottom: 1px solid #4a4a5a;
        padding-bottom: 0.5rem;
        display: flex;
        justify-content: space-between;
        align-items: center;
    }

    .btn-refresh {
        background: #6366f1;
        color: white;
        padding: 0.25rem 0.5rem;
        border: none;
        border-radius: 4px;
        cursor: pointer;
        font-size: 0.75rem;
    }

    .btn-refresh:hover {
        background: #4f46e5;
    }

    /* Left Panel - State */
    .state-section {
        margin-bottom: 1.5rem;
    }

    .state-section h4 {
        color: #9ca3af;
        font-size: 0.75rem;
        text-transform: uppercase;
        margin: 0 0 0.5rem 0;
    }

    .state-item {
        margin-bottom: 0.5rem;
    }

    .state-label {
        color: #6b7280;
        font-size: 0.75rem;
    }

    .state-value {
        color: #e5e7eb;
        font-weight: 500;
    }

    .combatant-card {
        background: #2a2a3a;
        border-radius: 6px;
        padding: 0.75rem;
        margin-bottom: 0.5rem;
        border-left: 3px solid #4a4a5a;
    }

    .combatant-card.player {
        border-left-color: #3b82f6;
    }

    .combatant-card.enemy {
        border-left-color: #ef4444;
    }

    .combatant-card.active {
        box-shadow: 0 0 0 2px #f59e0b;
    }

    .combatant-card.defeated {
        opacity: 0.5;
    }

    .combatant-name {
        color: #e5e7eb;
        font-weight: 600;
        margin-bottom: 0.5rem;
    }

    .resource-bar {
        height: 8px;
        background: #1a1a2a;
        border-radius: 4px;
        overflow: hidden;
        margin-bottom: 0.25rem;
    }

    .resource-bar-fill {
        height: 100%;
        border-radius: 4px;
        transition: width 0.3s ease;
    }

    .resource-bar-fill.hp {
        background: linear-gradient(90deg, #ef4444, #f87171);
    }

    .resource-bar-fill.mp {
        background: linear-gradient(90deg, #3b82f6, #60a5fa);
    }

    .resource-bar-fill.low {
        background: linear-gradient(90deg, #dc2626, #ef4444);
        animation: pulse 1s infinite;
    }

    @keyframes pulse {
        0%, 100% { opacity: 1; }
        50% { opacity: 0.6; }
    }

    .resource-text {
        font-size: 0.7rem;
        color: #9ca3af;
        display: flex;
        justify-content: space-between;
    }

    /* Debug Actions */
    .debug-actions {
        margin-top: 1rem;
        padding-top: 1rem;
        border-top: 1px solid #4a4a5a;
    }

    .debug-btn {
        width: 100%;
        padding: 0.5rem;
        margin-bottom: 0.5rem;
        border: 1px solid #4a4a5a;
        border-radius: 4px;
        cursor: pointer;
        font-size: 0.85rem;
        transition: all 0.2s;
    }

    .debug-btn.victory {
        background: #064e3b;
        color: #a7f3d0;
    }

    .debug-btn.victory:hover {
        background: #065f46;
    }

    .debug-btn.defeat {
        background: #7f1d1d;
        color: #fecaca;
    }

    .debug-btn.defeat:hover {
        background: #991b1b;
    }

    .debug-btn.reset {
        background: #1e40af;
        color: #bfdbfe;
    }

    .debug-btn.reset:hover {
        background: #1d4ed8;
    }

    /* Center Panel - Main */
    .main-panel {
        min-height: 600px;
        display: flex;
        flex-direction: column;
    }

    #combat-log {
        flex: 1;
        background: #1a1a2a;
        border-radius: 6px;
        padding: 1rem;
        min-height: 300px;
        max-height: 400px;
        overflow-y: auto;
        margin-bottom: 1rem;
    }

    .log-entry {
        padding: 0.5rem 0.75rem;
        margin-bottom: 0.5rem;
        border-radius: 4px;
        font-size: 0.9rem;
    }

    .log-entry--player {
        background: rgba(59, 130, 246, 0.15);
        border-left: 3px solid #3b82f6;
    }

    .log-entry--enemy {
        background: rgba(239, 68, 68, 0.15);
        border-left: 3px solid #ef4444;
    }

    .log-entry--crit {
        background: rgba(245, 158, 11, 0.2);
        border-left: 3px solid #f59e0b;
    }

    .log-entry--system {
        background: rgba(107, 114, 128, 0.15);
        border-left: 3px solid #6b7280;
        font-style: italic;
        color: #9ca3af;
    }

    .log-entry--heal {
        background: rgba(16, 185, 129, 0.15);
        border-left: 3px solid #10b981;
    }

    .log-actor {
        font-weight: 600;
        color: #e5e7eb;
    }

    .log-message {
        color: #d1d5db;
    }

    .log-damage {
        color: #ef4444;
        font-weight: 600;
    }

    .log-heal {
        color: #10b981;
        font-weight: 600;
    }

    .log-crit {
        color: #f59e0b;
        font-size: 0.75rem;
        margin-left: 0.5rem;
    }

    /* Action Buttons */
    .actions-grid {
        display: grid;
        grid-template-columns: repeat(5, 1fr);
        gap: 0.5rem;
        margin-bottom: 1rem;
    }

    @media (max-width: 900px) {
        .actions-grid {
            grid-template-columns: repeat(3, 1fr);
        }
    }

    .action-btn {
        padding: 0.75rem 1rem;
        border: none;
        border-radius: 6px;
        cursor: pointer;
        font-size: 0.9rem;
        font-weight: 500;
        transition: all 0.2s;
        text-align: center;
    }

    .action-btn:disabled {
        opacity: 0.5;
        cursor: not-allowed;
    }

    .action-btn.attack {
        background: #ef4444;
        color: white;
    }
    .action-btn.attack:hover:not(:disabled) {
        background: #dc2626;
    }

    .action-btn.ability {
        background: #8b5cf6;
        color: white;
    }
    .action-btn.ability:hover:not(:disabled) {
        background: #7c3aed;
    }

    .action-btn.item {
        background: #10b981;
        color: white;
    }
    .action-btn.item:hover:not(:disabled) {
        background: #059669;
    }

    .action-btn.defend {
        background: #3b82f6;
        color: white;
    }
    .action-btn.defend:hover:not(:disabled) {
        background: #2563eb;
    }

    .action-btn.flee {
        background: #6b7280;
        color: white;
    }
    .action-btn.flee:hover:not(:disabled) {
        background: #4b5563;
    }

    /* Right Panel */
    .turn-order {
        margin-bottom: 1rem;
    }

    .turn-item {
        display: flex;
        align-items: center;
        padding: 0.5rem;
        margin-bottom: 0.25rem;
        background: #2a2a3a;
        border-radius: 4px;
        font-size: 0.85rem;
    }

    .turn-item.active {
        background: #3b3b5b;
        border: 1px solid #f59e0b;
    }

    .turn-number {
        width: 24px;
        height: 24px;
        background: #4a4a5a;
        border-radius: 50%;
        display: flex;
        align-items: center;
        justify-content: center;
        margin-right: 0.5rem;
        font-size: 0.75rem;
        color: #9ca3af;
    }

    .turn-item.active .turn-number {
        background: #f59e0b;
        color: #1a1a2a;
    }

    .turn-name {
        color: #e5e7eb;
    }

    .turn-name.player {
        color: #60a5fa;
    }

    .turn-name.enemy {
        color: #f87171;
    }

    /* Effects Panel */
    .effects-panel {
        margin-top: 1rem;
    }

    .effect-item {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 0.5rem;
        margin-bottom: 0.25rem;
        background: #2a2a3a;
        border-radius: 4px;
        font-size: 0.8rem;
    }

    .effect-name {
        color: #e5e7eb;
    }

    .effect-duration {
        color: #f59e0b;
        font-size: 0.75rem;
    }

    /* Debug Panel */
    .debug-panel {
        margin-top: 1rem;
        padding: 1rem;
        background: #1a1a2a;
        border-radius: 6px;
        font-family: monospace;
        font-size: 0.75rem;
    }

    .debug-toggle {
        color: #9ca3af;
        cursor: pointer;
        user-select: none;
    }

    .debug-content {
        margin-top: 0.5rem;
        max-height: 300px;
        overflow: auto;
        white-space: pre;
        color: #a3e635;
    }

    /* Modal Styles */
    .modal-overlay {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0, 0, 0, 0.8);
        display: flex;
        align-items: center;
        justify-content: center;
        z-index: 1000;
    }

    .modal-content {
        background: #1a1a2a;
        border: 1px solid #4a4a5a;
        border-radius: 8px;
        padding: 1.5rem;
        max-width: 500px;
        width: 90%;
        max-height: 80vh;
        overflow-y: auto;
    }

    .modal-content h3 {
        color: #f59e0b;
        margin-top: 0;
        margin-bottom: 1rem;
    }

    .modal-close {
        margin-top: 1rem;
        padding: 0.5rem 1rem;
        background: #6b7280;
        border: none;
        border-radius: 4px;
        color: white;
        cursor: pointer;
    }

    /* Sheet Styles */
    .combat-items-sheet {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        background: #1a1a2a;
        border-top: 1px solid #4a4a5a;
        border-radius: 16px 16px 0 0;
        padding: 1rem;
        max-height: 50vh;
        overflow-y: auto;
        z-index: 1000;
        transform: translateY(100%);
        transition: transform 0.3s ease;
    }

    .combat-items-sheet.open {
        transform: translateY(0);
    }

    .sheet-backdrop {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0, 0, 0, 0.5);
        z-index: 999;
    }

    .sheet-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 1rem;
    }

    .sheet-header h3 {
        color: #f59e0b;
        margin: 0;
    }

    .sheet-close {
        background: none;
        border: none;
        color: #9ca3af;
        font-size: 1.5rem;
        cursor: pointer;
    }

    .error {
        background: #7f1d1d;
        color: #fecaca;
        padding: 0.75rem;
        border-radius: 6px;
    }

    .success {
        background: #064e3b;
        color: #a7f3d0;
        padding: 0.75rem;
        border-radius: 6px;
    }

    .back-link {
        color: #60a5fa;
        text-decoration: none;
        font-size: 0.85rem;
    }

    .back-link:hover {
        text-decoration: underline;
    }
</style>
{% endblock %}

{% block content %}
<div class="dev-banner">
    DEV MODE - Combat Session {{ session_id[:8] }}...
</div>

<div class="combat-container">
    <!-- Left Panel: Combat State -->
    <div class="panel left-panel">
        <h3>
            Combat State
            <button class="btn-refresh"
                    hx-get="{{ url_for('dev.combat_state', session_id=session_id) }}"
                    hx-target="#state-content"
                    hx-swap="innerHTML">
                Refresh
            </button>
        </h3>
        <div id="state-content">
            {% include 'dev/partials/combat_state.html' %}
        </div>

        <!-- Debug Actions -->
        <div class="debug-actions">
            <h4 style="color: #f59e0b; font-size: 0.85rem; margin: 0 0 0.5rem 0;">Debug Actions</h4>
            <button class="debug-btn reset"
                    hx-post="{{ url_for('dev.reset_hp_mp', session_id=session_id) }}"
                    hx-target="#combat-log"
                    hx-swap="beforeend">
                Reset HP/MP
            </button>
            <button class="debug-btn victory"
                    hx-post="{{ url_for('dev.force_end_combat', session_id=session_id) }}"
                    hx-vals='{"victory": "true"}'
                    hx-target="#combat-log"
                    hx-swap="innerHTML">
                Force Victory
            </button>
            <button class="debug-btn defeat"
                    hx-post="{{ url_for('dev.force_end_combat', session_id=session_id) }}"
                    hx-vals='{"victory": "false"}'
                    hx-target="#combat-log"
                    hx-swap="innerHTML">
                Force Defeat
            </button>
        </div>

        <div style="margin-top: 1rem; padding-top: 1rem; border-top: 1px solid #4a4a5a;">
            <a href="{{ url_for('dev.combat_hub') }}" class="back-link">&larr; Back to Combat Hub</a>
        </div>
    </div>

    <!-- Center Panel: Combat Log & Actions -->
    <div class="panel main-panel">
        <h3>Combat Log</h3>

        <!-- Combat Log -->
        <div id="combat-log" role="log" aria-live="polite">
            {% for entry in combat_log %}
            <div class="log-entry log-entry--{{ entry.type }}">
                {% if entry.actor %}
                <span class="log-actor">{{ entry.actor }}</span>
                {% endif %}
                <span class="log-message">{{ entry.message }}</span>
                {% if entry.damage %}
                <span class="log-damage">-{{ entry.damage }} HP</span>
                {% endif %}
                {% if entry.heal %}
                <span class="log-heal">+{{ entry.heal }} HP</span>
                {% endif %}
                {% if entry.is_crit %}
                <span class="log-crit">CRITICAL!</span>
                {% endif %}
            </div>
            {% else %}
            <div class="log-entry log-entry--system">
                Combat begins!
                {% if is_player_turn %}
                Take your action.
                {% else %}
                Waiting for enemy turn...
                {% endif %}
            </div>
            {% endfor %}
        </div>

        <!-- Action Buttons -->
        <div class="actions-grid" id="action-buttons">
            <button class="action-btn attack"
                    hx-post="{{ url_for('dev.combat_action', session_id=session_id) }}"
                    hx-vals='{"action_type": "attack"}'
                    hx-target="#combat-log"
                    hx-swap="beforeend"
                    hx-disabled-elt="this"
                    {% if not is_player_turn %}disabled{% endif %}>
                Attack
            </button>
            <button class="action-btn ability"
                    hx-get="{{ url_for('dev.combat_abilities', session_id=session_id) }}"
                    hx-target="#modal-container"
                    hx-swap="innerHTML"
                    {% if not is_player_turn %}disabled{% endif %}>
                Ability
            </button>
            <button class="action-btn item"
                    hx-get="{{ url_for('dev.combat_items', session_id=session_id) }}"
                    hx-target="#sheet-container"
                    hx-swap="innerHTML"
                    {% if not is_player_turn %}disabled{% endif %}>
                Item
            </button>
            <button class="action-btn defend"
                    hx-post="{{ url_for('dev.combat_action', session_id=session_id) }}"
                    hx-vals='{"action_type": "defend"}'
                    hx-target="#combat-log"
                    hx-swap="beforeend"
                    hx-disabled-elt="this"
                    {% if not is_player_turn %}disabled{% endif %}>
                Defend
            </button>
            <button class="action-btn flee"
                    hx-post="{{ url_for('dev.combat_action', session_id=session_id) }}"
                    hx-vals='{"action_type": "flee"}'
                    hx-target="#combat-log"
                    hx-swap="beforeend"
                    hx-disabled-elt="this"
                    {% if not is_player_turn %}disabled{% endif %}>
                Flee
            </button>
        </div>

        <!-- Debug Panel -->
        <div class="debug-panel">
            <div class="debug-toggle" onclick="this.nextElementSibling.style.display = this.nextElementSibling.style.display === 'none' ? 'block' : 'none'">
                [+] Raw State JSON (click to toggle)
            </div>
            <div class="debug-content" style="display: none;">{{ raw_state | tojson(indent=2) }}</div>
        </div>
    </div>

    <!-- Right Panel: Turn Order & Effects -->
    <div class="panel right-panel">
        <h3>Turn Order</h3>

        <div class="turn-order">
            {% for combatant_id in turn_order %}
            {% set ns = namespace(combatant=None) %}
            {% for c in encounter.combatants %}
                {% if c.combatant_id == combatant_id %}
                    {% set ns.combatant = c %}
                {% endif %}
            {% endfor %}
            <div class="turn-item {% if combatant_id == current_turn_id %}active{% endif %}">
                <span class="turn-number">{{ loop.index }}</span>
                <span class="turn-name {% if ns.combatant and ns.combatant.is_player %}player{% else %}enemy{% endif %}">
                    {% if ns.combatant %}{{ ns.combatant.name }}{% else %}{{ combatant_id[:8] }}{% endif %}
                </span>
            </div>
            {% endfor %}
        </div>

        <h3 style="margin-top: 1rem;">Active Effects</h3>
        <div class="effects-panel">
            {% if player_combatant and player_combatant.active_effects %}
                {% for effect in player_combatant.active_effects %}
                <div class="effect-item">
                    <span class="effect-name">{{ effect.name }}</span>
                    <span class="effect-duration">{{ effect.remaining_duration }} turns</span>
                </div>
                {% endfor %}
            {% else %}
            <p style="color: #6b7280; font-size: 0.85rem; text-align: center;">No active effects</p>
            {% endif %}
        </div>
    </div>
</div>

<!-- Modal Container -->
<div id="modal-container"></div>

<!-- Sheet Container -->
<div id="sheet-container"></div>

<script>
// Close modal function
function closeModal() {
    document.getElementById('modal-container').innerHTML = '';
}

// Close combat sheet function
function closeCombatSheet() {
    document.getElementById('sheet-container').innerHTML = '';
}

// Refresh combat state panel
function refreshCombatState() {
    htmx.ajax('GET', '{{ url_for("dev.combat_state", session_id=session_id) }}', {
        target: '#state-content',
        swap: 'innerHTML'
    });
}

// Auto-scroll combat log
const combatLog = document.getElementById('combat-log');
if (combatLog) {
    combatLog.scrollTop = combatLog.scrollHeight;
}

// Observe combat log for new entries and auto-scroll
const observer = new MutationObserver(function() {
    combatLog.scrollTop = combatLog.scrollHeight;
});
observer.observe(combatLog, { childList: true });

// Guard against duplicate enemy turn requests
let enemyTurnPending = false;
let enemyTurnTimeout = null;

function triggerEnemyTurn(delay = 1000) {
    // Prevent duplicate requests
    if (enemyTurnPending) {
        return;
    }

    // Clear any pending timeout
    if (enemyTurnTimeout) {
        clearTimeout(enemyTurnTimeout);
    }

    enemyTurnPending = true;
    enemyTurnTimeout = setTimeout(function() {
        htmx.ajax('POST', '{{ url_for("dev.combat_enemy_turn", session_id=session_id) }}', {
            target: '#combat-log',
            swap: 'beforeend'
        }).then(function() {
            enemyTurnPending = false;
            // Refresh state after enemy turn completes
            setTimeout(refreshCombatState, 500);
        }).catch(function() {
            enemyTurnPending = false;
        });
    }, delay);
}

// Auto-trigger enemy turn on page load if it's not the player's turn
{% if not is_player_turn %}
document.addEventListener('DOMContentLoaded', function() {
    // Small delay to let the page render first
    triggerEnemyTurn(500);
});
{% endif %}

// Handle enemy turn trigger
document.body.addEventListener('htmx:afterRequest', function(event) {
    // Check for enemyTurn trigger
    const trigger = event.detail.xhr.getResponseHeader('HX-Trigger');
    if (trigger && trigger.includes('enemyTurn')) {
        triggerEnemyTurn(1000);
    }

    // Refresh state after any combat action (player action, debug action, but NOT enemy turn - handled above)
    const requestUrl = event.detail.pathInfo?.requestPath || '';
    const isActionBtn = event.detail.elt && event.detail.elt.classList && event.detail.elt.classList.contains('action-btn');
    const isDebugBtn = event.detail.elt && event.detail.elt.classList && event.detail.elt.classList.contains('debug-btn');

    if (isActionBtn || isDebugBtn) {
        setTimeout(refreshCombatState, 500);
    }
});

// Re-enable buttons when player turn returns
document.body.addEventListener('htmx:afterSwap', function(event) {
    // If state was updated, check if it's player turn
    if (event.detail.target.id === 'state-content') {
        const stateContent = document.getElementById('state-content');
        const isPlayerTurn = stateContent && stateContent.textContent.includes('Your Turn');
        const buttons = document.querySelectorAll('.action-btn');
        buttons.forEach(function(btn) {
            btn.disabled = !isPlayerTurn;
        });
    }
});
</script>
{% endblock %}
