{#
    Partial: AI Audit Actions Section

    Variables required:
        company - Company object (with .id, .slug)
        audit_type - 'seo', 'gbp', or 'social'

    Include this at the bottom of audit templates:
    {% include 'partials/audit_ai_actions.html' %}
#}

<!-- AI Analysis & Actions Section -->
<div id="aiActionsSection" style="margin-top: var(--spacing-2xl);">
    <h2 class="section-title" style="font-size: var(--font-size-xl); font-weight: 600; color: var(--text-primary); margin-bottom: var(--spacing-md); display: flex; align-items: center; gap: var(--spacing-sm);">
        <svg width="24" height="24" fill="none" stroke="currentColor" viewBox="0 0 24 24">
            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9.663 17h4.673M12 3v1m6.364 1.636l-.707.707M21 12h-1M4 12H3m3.343-5.657l-.707-.707m2.828 9.9a5 5 0 117.072 0l-.548.547A3.374 3.374 0 0014 18.469V19a2 2 0 11-4 0v-.531c0-.895-.356-1.754-.988-2.386l-.548-.547z"/>
        </svg>
        Analiza AI i Rekomendacje
    </h2>

    <!-- Generate Analysis Button -->
    <div id="aiAnalyzePrompt" style="background: var(--surface); padding: var(--spacing-xl); border-radius: var(--radius-lg); box-shadow: var(--shadow); text-align: center;">
        <p style="color: var(--text-secondary); margin-bottom: var(--spacing-md);">
            AI przeanalizuje wyniki audytu i zaproponuje priorytetowane akcje do podjecia.
        </p>
        <button class="btn btn-primary" onclick="runAIAnalysis()" id="aiAnalyzeBtn">
            <svg width="18" height="18" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9.663 17h4.673M12 3v1m6.364 1.636l-.707.707M21 12h-1M4 12H3m3.343-5.657l-.707-.707m2.828 9.9a5 5 0 117.072 0l-.548.547A3.374 3.374 0 0014 18.469V19a2 2 0 11-4 0v-.531c0-.895-.356-1.754-.988-2.386l-.548-.547z"/>
            </svg>
            Wygeneruj analize AI
        </button>
    </div>

    <!-- AI Loading Spinner -->
    <div id="aiLoading" style="display: none; background: var(--surface); padding: var(--spacing-xl); border-radius: var(--radius-lg); box-shadow: var(--shadow); text-align: center;">
        <div style="width: 40px; height: 40px; border: 3px solid var(--border); border-top-color: var(--primary); border-radius: 50%; animation: spin 1s linear infinite; margin: 0 auto var(--spacing-md);"></div>
        <p style="color: var(--text-secondary);">
            Analiza AI w toku... (moze potrwac 15-30 sekund)
            <span id="aiTimer" style="font-weight: 600;"></span>
        </p>
    </div>

    <!-- AI Results Container -->
    <div id="aiResults" style="display: none;">
        <!-- Summary -->
        <div id="aiSummary" style="background: linear-gradient(135deg, #eff6ff 0%, #f0fdf4 100%); padding: var(--spacing-lg); border-radius: var(--radius-lg); margin-bottom: var(--spacing-lg); border: 1px solid #bfdbfe;">
            <div style="display: flex; align-items: flex-start; gap: var(--spacing-sm);">
                <svg width="20" height="20" fill="none" stroke="#2563eb" viewBox="0 0 24 24" style="flex-shrink: 0; margin-top: 2px;">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"/>
                </svg>
                <p id="aiSummaryText" style="color: var(--text-primary); line-height: 1.6; margin: 0;"></p>
            </div>
            <div id="aiCacheInfo" style="display: none; margin-top: var(--spacing-sm); font-size: var(--font-size-xs); color: var(--text-tertiary);">
                Analiza z <span id="aiCacheDate"></span> &mdash; <a href="#" onclick="runAIAnalysis(true); return false;" style="color: var(--primary);">Wygeneruj ponownie</a>
            </div>
        </div>

        <!-- Comparison with previous analysis -->
        <div id="aiComparison" style="display: none; margin-bottom: var(--spacing-lg);">
            <div style="display: flex; align-items: center; gap: var(--spacing-sm); margin-bottom: var(--spacing-sm); cursor: pointer;" onclick="toggleComparison()">
                <svg id="aiComparisonArrow" width="16" height="16" fill="none" stroke="currentColor" viewBox="0 0 24 24" style="transition: transform 0.2s;">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7"/>
                </svg>
                <span style="font-size: var(--font-size-sm); font-weight: 600; color: var(--text-secondary);">Porownanie z poprzednia analiza</span>
                <span id="aiComparisonDate" style="font-size: var(--font-size-xs); color: var(--text-tertiary);"></span>
            </div>
            <div id="aiComparisonBody" style="display: none;">
                <table class="ai-comparison-table">
                    <thead>
                        <tr>
                            <th>Aspekt</th>
                            <th>Poprzednia</th>
                            <th>Obecna</th>
                        </tr>
                    </thead>
                    <tbody id="aiComparisonRows"></tbody>
                </table>
                <div id="aiComparisonDiff" style="margin-top: var(--spacing-sm);"></div>
            </div>
        </div>

        <!-- Actions List -->
        <div style="font-size: var(--font-size-lg); font-weight: 600; color: var(--text-primary); margin-bottom: var(--spacing-md);">
            Priorytetowe akcje
        </div>
        <div id="aiActionsList"></div>
    </div>
</div>

<style>
    .ai-action-card {
        background: var(--surface);
        border-radius: var(--radius-lg);
        box-shadow: var(--shadow-sm);
        padding: var(--spacing-lg);
        margin-bottom: var(--spacing-md);
        border-left: 4px solid var(--border);
        transition: box-shadow 0.2s;
    }
    .ai-action-card:hover {
        box-shadow: var(--shadow);
    }
    .ai-action-card.priority-critical { border-left-color: #ef4444; }
    .ai-action-card.priority-high { border-left-color: #f97316; }
    .ai-action-card.priority-medium { border-left-color: #f59e0b; }
    .ai-action-card.priority-low { border-left-color: #84cc16; }

    .ai-priority-badge {
        display: inline-flex;
        align-items: center;
        gap: 4px;
        padding: 2px 8px;
        border-radius: var(--radius-sm);
        font-size: var(--font-size-xs);
        font-weight: 600;
        text-transform: uppercase;
        letter-spacing: 0.5px;
    }
    .ai-priority-badge.critical { background: #fee2e2; color: #dc2626; }
    .ai-priority-badge.high { background: #ffedd5; color: #ea580c; }
    .ai-priority-badge.medium { background: #fef3c7; color: #d97706; }
    .ai-priority-badge.low { background: #ecfccb; color: #65a30d; }

    .ai-score-bar {
        height: 6px;
        border-radius: 3px;
        background: #e2e8f0;
        overflow: hidden;
    }
    .ai-score-bar-fill {
        height: 100%;
        border-radius: 3px;
        transition: width 0.3s;
    }
    .ai-score-bar-fill.impact { background: #3b82f6; }
    .ai-score-bar-fill.effort { background: #f59e0b; }

    .ai-content-output {
        background: #1e293b;
        color: #e2e8f0;
        padding: var(--spacing-md);
        border-radius: var(--radius);
        margin-top: var(--spacing-md);
        font-family: 'Menlo', 'Monaco', 'Consolas', monospace;
        font-size: var(--font-size-sm);
        white-space: pre-wrap;
        word-break: break-all;
        position: relative;
        max-height: 400px;
        overflow-y: auto;
    }

    .ai-copy-btn {
        position: absolute;
        top: 8px;
        right: 8px;
        background: rgba(255,255,255,0.1);
        border: 1px solid rgba(255,255,255,0.2);
        color: #e2e8f0;
        padding: 4px 10px;
        border-radius: var(--radius-sm);
        font-size: var(--font-size-xs);
        cursor: pointer;
        transition: background 0.2s;
    }
    .ai-copy-btn:hover {
        background: rgba(255,255,255,0.2);
    }

    .ai-action-buttons {
        display: flex;
        gap: var(--spacing-sm);
        margin-top: var(--spacing-md);
        flex-wrap: wrap;
    }

    .ai-action-card.implemented {
        opacity: 0.6;
        border-left-color: #10b981;
    }
    .ai-action-card.implemented .ai-action-title {
        text-decoration: line-through;
    }
    .ai-action-card.dismissed {
        display: none;
    }

    .ai-comparison-table {
        width: 100%;
        border-collapse: collapse;
        font-size: var(--font-size-sm);
    }
    .ai-comparison-table th {
        background: var(--bg-tertiary);
        padding: 8px 12px;
        text-align: left;
        font-weight: 600;
        color: var(--text-secondary);
        border-bottom: 2px solid var(--border);
    }
    .ai-comparison-table td {
        padding: 8px 12px;
        border-bottom: 1px solid var(--border);
        color: var(--text-primary);
        vertical-align: top;
    }
    .ai-comparison-table tr:last-child td {
        border-bottom: none;
    }
    .ai-diff-added {
        display: inline-block;
        background: #dcfce7;
        color: #166534;
        padding: 2px 8px;
        border-radius: var(--radius-sm);
        font-size: var(--font-size-xs);
        margin: 2px;
    }
    .ai-diff-removed {
        display: inline-block;
        background: #fee2e2;
        color: #991b1b;
        padding: 2px 8px;
        border-radius: var(--radius-sm);
        font-size: var(--font-size-xs);
        margin: 2px;
        text-decoration: line-through;
    }
</style>

<script>
document.addEventListener('DOMContentLoaded', function() {
    if (typeof companyId === 'undefined' || typeof auditType === 'undefined') return;

    fetch('/api/audit/analyze', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'X-CSRFToken': csrfToken
        },
        body: JSON.stringify({
            company_id: companyId,
            audit_type: auditType,
            force: false
        })
    })
    .then(function(r) { return r.json(); })
    .then(function(data) {
        if (data.success && data.cached) {
            var prompt = document.getElementById('aiAnalyzePrompt');
            if (prompt) prompt.style.display = 'none';
            renderAIResults(data);
        }
    })
    .catch(function() {});
});

function toggleComparison() {
    var body = document.getElementById('aiComparisonBody');
    var arrow = document.getElementById('aiComparisonArrow');
    if (body.style.display === 'none') {
        body.style.display = 'block';
        arrow.style.transform = 'rotate(90deg)';
    } else {
        body.style.display = 'none';
        arrow.style.transform = '';
    }
}

function renderAIComparison(data) {
    var section = document.getElementById('aiComparison');
    if (!data.previous) {
        section.style.display = 'none';
        return;
    }

    var prev = data.previous;
    var prevActions = prev.actions || [];
    var currActions = data.actions || [];
    var priorityOrder = ['critical', 'high', 'medium', 'low'];

    function countPriority(actions, p) {
        return actions.filter(function(a) { return a.priority === p; }).length;
    }

    var rows = document.getElementById('aiComparisonRows');
    var html = '';

    // Summary row
    var prevSummaryShort = (prev.summary || '').substring(0, 120) + ((prev.summary || '').length > 120 ? '...' : '');
    var currSummaryShort = (data.summary || '').substring(0, 120) + ((data.summary || '').length > 120 ? '...' : '');
    html += '<tr><td><strong>Podsumowanie</strong></td><td style="color: var(--text-secondary);">' + escapeHtml(prevSummaryShort) + '</td><td>' + escapeHtml(currSummaryShort) + '</td></tr>';

    // Count row
    html += '<tr><td><strong>Liczba akcji</strong></td><td>' + prevActions.length + '</td><td>' + currActions.length + '</td></tr>';

    // Priority breakdown
    priorityOrder.forEach(function(p) {
        var labels = {critical: 'Krytyczne', high: 'Wysokie', medium: 'Srednie', low: 'Niskie'};
        var pc = countPriority(prevActions, p);
        var cc = countPriority(currActions, p);
        if (pc > 0 || cc > 0) {
            var diff = cc - pc;
            var diffStr = diff > 0 ? ' <span style="color:#16a34a;">(+' + diff + ')</span>' : diff < 0 ? ' <span style="color:#dc2626;">(' + diff + ')</span>' : '';
            html += '<tr><td>' + labels[p] + '</td><td>' + pc + '</td><td>' + cc + diffStr + '</td></tr>';
        }
    });

    // Date row
    if (prev.generated_at) {
        var pd = new Date(prev.generated_at);
        html += '<tr><td><strong>Data analizy</strong></td><td>' + pd.toLocaleDateString('pl-PL') + ' ' + pd.toLocaleTimeString('pl-PL', {hour:'2-digit', minute:'2-digit'}) + '</td><td>teraz</td></tr>';
    }

    rows.innerHTML = html;

    // Diff: new/removed actions
    var prevTitles = prevActions.map(function(a) { return a.title; });
    var currTitles = currActions.map(function(a) { return a.title; });
    var added = currTitles.filter(function(t) { return prevTitles.indexOf(t) === -1; });
    var removed = prevTitles.filter(function(t) { return currTitles.indexOf(t) === -1; });

    var diffEl = document.getElementById('aiComparisonDiff');
    var diffHtml = '';
    if (added.length > 0) {
        diffHtml += '<div style="margin-bottom: var(--spacing-xs);"><strong style="font-size: var(--font-size-xs); color: var(--text-secondary);">Nowe akcje:</strong> ';
        added.forEach(function(t) { diffHtml += '<span class="ai-diff-added">' + escapeHtml(t) + '</span>'; });
        diffHtml += '</div>';
    }
    if (removed.length > 0) {
        diffHtml += '<div><strong style="font-size: var(--font-size-xs); color: var(--text-secondary);">Usuniete akcje:</strong> ';
        removed.forEach(function(t) { diffHtml += '<span class="ai-diff-removed">' + escapeHtml(t) + '</span>'; });
        diffHtml += '</div>';
    }
    diffEl.innerHTML = diffHtml;

    // Show date in header
    if (prev.generated_at) {
        var pd2 = new Date(prev.generated_at);
        document.getElementById('aiComparisonDate').textContent = '(z ' + pd2.toLocaleDateString('pl-PL') + ')';
    }

    section.style.display = 'block';
}
</script>
