<%
// PORTFOLIO INDEX - Scan automatique des réalisations avec vignettes intelligentes

function extractTags(tags) {
    if (!tags) return [];
    if (typeof tags.toArray === 'function') {
        return tags.toArray().map(function(t) { return t.name || t; });
    }
    if (Array.isArray(tags)) {
        return tags.map(function(t) { return typeof t === 'object' ? t.name : t; });
    }
    return [];
}

var portfolioItems = [];

site.pages.forEach(function(p) {
    var src = (p.source || '').toLowerCase();
    var pth = (p.path || '').toLowerCase();
    var layout = (p.layout || '').toLowerCase();
    
    if (src.indexOf('index.md') >= 0 || 
        pth === 'portfolio/' || 
        pth === 'portfolio/index.html' ||
        layout === 'portfolio-index') {
        return;
    }
    
    var isPortfolio = layout === 'portfolio' || 
                      src.indexOf('portfolio/') >= 0 ||
                      pth.indexOf('portfolio/') >= 0;
    
    if (isPortfolio) {
        var exists = portfolioItems.some(function(item) { return item.path === p.path; });
        if (!exists) {
            var itemTags = extractTags(p.tags);
            if (itemTags.length === 0 && p.tags_list) {
                itemTags = Array.isArray(p.tags_list) ? p.tags_list : [];
            }
            
            portfolioItems.push({
                title: p.title || 'Sans titre',
                path: p.path,
                date: p.date,
                icon: p.icon || '💼',
                type: p.type || 'project',
                category: p.category || p.type || 'project',
                description: p.description || '',
                // Données pour vignettes intelligentes
                cover: p.cover || '',
                thumbnail: p.thumbnail || '',
                images: p.images || [],
                client: p.client || {},
                project: p.project || {},
                live_url: p.live_url || '',
                featured: p.featured || false,
                status: p.status || 'live',
                order: p.order || 999,
                tags: itemTags,
                _page: p  // Référence à la page pour render_smart_thumbnail
            });
        }
    }
});

portfolioItems.sort(function(a, b) {
    if (a.featured && !b.featured) return -1;
    if (!a.featured && b.featured) return 1;
    if (a.order !== b.order) return a.order - b.order;
    return new Date(b.date) - new Date(a.date);
});

var clients = portfolioItems.filter(function(p) { return p.type === 'client'; });
var projects = portfolioItems.filter(function(p) { return p.type === 'project'; });
var opensource = portfolioItems.filter(function(p) { return p.type === 'opensource'; });

var typeColors = {
    'client': '#10b981',
    'project': '#3b82f6',
    'opensource': '#f59e0b',
    'security': '#ef4444'
};

var typeLabels = {
    'client': { icon: '🏢', name: 'Clients' },
    'project': { icon: '🚀', name: 'Projets' },
    'opensource': { icon: '🔓', name: 'Open Source' },
    'security': { icon: '🛡️', name: 'Sécurité' }
};
%>
<div class="portfolio-index">
    <header class="portfolio-header">
        <div class="section-header">
            <div class="section-tag">Réalisations</div>
            <h1 class="section-title"><%= page.icon || '💼' %> <%= page.title || 'Portfolio' %></h1>
            <p class="section-subtitle"><%= page.description || 'Découvrez mes réalisations, projets clients et contributions open source' %></p>
        </div>
        
        <div class="portfolio-stats">
            <div class="stat-card">
                <span class="stat-value"><%= clients.length %></span>
                <span class="stat-label">🏢 Clients</span>
            </div>
            <div class="stat-card">
                <span class="stat-value"><%= projects.length %></span>
                <span class="stat-label">🚀 Projets</span>
            </div>
            <div class="stat-card">
                <span class="stat-value"><%= opensource.length %></span>
                <span class="stat-label">🔓 Open Source</span>
            </div>
            <div class="stat-card">
                <span class="stat-value"><%= portfolioItems.length %></span>
                <span class="stat-label">📦 Total</span>
            </div>
        </div>
        
        <div class="portfolio-filters">
            <button class="filter-btn active" data-filter="all">🎲 Tous</button>
            <button class="filter-btn" data-filter="client">🏢 Clients</button>
            <button class="filter-btn" data-filter="project">🚀 Projets</button>
            <button class="filter-btn" data-filter="opensource">🔓 Open Source</button>
        </div>
    </header>
    
    <% if (portfolioItems.length > 0) { %>
    
    <div class="portfolio-grid" id="portfolioGrid">
        <% portfolioItems.forEach(function(item) { 
            var color = typeColors[item.type] || '#f97316';
            var label = typeLabels[item.type] || { icon: '📦', name: 'Autre' };
            var hasImage = item.cover || item.thumbnail || (item.images && item.images.length > 0);
        %>
        <a href="<%= url_for(item.path) %>" class="portfolio-card" data-type="<%= item.type %>" style="--item-color: <%= color %>">
            <% if (item.featured) { %>
            <div class="featured-badge">⭐ Featured</div>
            <% } %>
            
            <div class="card-visual">
                <% if (hasImage) { %>
                    <% var imgSrc = item.cover || item.thumbnail || item.images[0]; %>
                    <img src="<%= imgSrc %>" alt="<%= item.title %>" loading="lazy" class="card-thumb-img">
                <% } else if (item._page && typeof render_smart_thumbnail === 'function') { %>
                    <%- render_smart_thumbnail(item._page, { class: 'card-thumb-dynamic' }) %>
                <% } else { %>
                    <!-- Vignette CSS générée -->
                    <div class="card-thumb-generated" style="background: linear-gradient(135deg, <%= color %>15, <%= color %>30);">
                        <span class="card-thumb-icon"><%= item.icon %></span>
                        <span class="card-thumb-title"><%= item.title %></span>
                        <% if (item.client && item.client.name) { %>
                        <span class="card-thumb-client">🏢 <%= item.client.name %></span>
                        <% } %>
                    </div>
                <% } %>
                
                <div class="card-overlay">
                    <span class="view-btn">👁️ Voir le projet</span>
                </div>
                
                <div class="card-type-badge" style="background: <%= color %>;">
                    <%= label.icon %> <%= label.name %>
                </div>
            </div>
            
            <div class="card-content">
                <h3><%= item.icon %> <%= item.title %></h3>
                <p><%= item.description %></p>
                
                <% if (item.client && item.client.name) { %>
                <div class="card-client">
                    🏢 <%= item.client.name %>
                    <% if (item.client.sector) { %> • <%= item.client.sector %><% } %>
                </div>
                <% } %>
                
                <% if (item.project && item.project.technologies && item.project.technologies.length) { %>
                <div class="card-tech">
                    <% item.project.technologies.slice(0, 4).forEach(function(tech) { %>
                    <span class="tech-tag"><%= tech %></span>
                    <% }); %>
                </div>
                <% } %>
                
                <div class="card-footer">
                    <span class="card-status status-<%= item.status %>">
                        <%= item.status === 'live' ? '🟢 En ligne' : item.status === 'demo' ? '🟡 Démo' : '📦 Archivé' %>
                    </span>
                    <% if (item.live_url) { %>
                    <span class="card-link">↗</span>
                    <% } %>
                </div>
            </div>
        </a>
        <% }); %>
    </div>
    
    <% } else { %>
    
    <div class="empty-state">
        <div class="empty-icon">💼</div>
        <h3>Aucune réalisation trouvée</h3>
        <p>Debug: <%= site.pages.length %> pages scannées</p>
        <p>Ajoutez des fichiers .md dans <code>source/portfolio/</code> avec <code>layout: portfolio</code></p>
    </div>
    
    <% } %>
    
    <nav class="portfolio-nav">
        <a href="/showcase/" class="btn btn-primary">🏪 Voir le Showcase</a>
        <a href="/" class="btn btn-secondary">← Accueil</a>
    </nav>
</div>

<style>
.portfolio-index {
    padding: 4rem 2rem;
    max-width: 1400px;
    margin: 0 auto;
}

.portfolio-header {
    text-align: center;
    margin-bottom: 3rem;
}

.section-subtitle {
    color: var(--text-muted);
    max-width: 600px;
    margin: 1rem auto 2rem;
}

.portfolio-stats {
    display: flex;
    justify-content: center;
    gap: 2rem;
    margin-bottom: 2rem;
    flex-wrap: wrap;
}

.stat-card {
    text-align: center;
    padding: 1rem 1.5rem;
    background: var(--bg-secondary);
    border-radius: 12px;
    border: 1px solid var(--border-subtle);
}

.stat-value {
    font-size: 2rem;
    font-weight: 700;
    color: var(--accent-primary);
    display: block;
}

.stat-label {
    font-size: 0.85rem;
    color: var(--text-muted);
}

.portfolio-filters {
    display: flex;
    justify-content: center;
    gap: 0.5rem;
    flex-wrap: wrap;
}

.filter-btn {
    padding: 0.5rem 1rem;
    background: var(--bg-secondary);
    border: 1px solid var(--border-subtle);
    border-radius: 20px;
    color: var(--text-secondary);
    cursor: pointer;
    font-size: 0.85rem;
}

.filter-btn:hover { border-color: var(--accent-primary); }
.filter-btn.active { background: var(--accent-primary); color: #000; }

.portfolio-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
    gap: 1.5rem;
}

.portfolio-card {
    background: var(--bg-secondary);
    border: 1px solid var(--border-subtle);
    border-radius: 16px;
    overflow: hidden;
    text-decoration: none;
    color: inherit;
    transition: all 0.3s ease;
    position: relative;
}

.portfolio-card:hover {
    transform: translateY(-4px);
    border-color: var(--item-color);
    box-shadow: 0 12px 40px rgba(0,0,0,0.3);
}

.featured-badge {
    position: absolute;
    top: 10px;
    right: 10px;
    background: linear-gradient(135deg, #f59e0b, #f97316);
    color: #000;
    padding: 0.25rem 0.75rem;
    border-radius: 20px;
    font-size: 0.75rem;
    font-weight: 600;
    z-index: 10;
}

.card-visual {
    position: relative;
    aspect-ratio: 16/10;
    background: var(--bg-tertiary);
    overflow: hidden;
}

.card-thumb-img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    transition: transform 0.3s ease;
}

.portfolio-card:hover .card-thumb-img {
    transform: scale(1.05);
}

.card-thumb-dynamic {
    width: 100%;
    height: 100%;
}

.card-thumb-generated {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    padding: 1rem;
}

.card-thumb-icon {
    font-size: 2.5rem;
}

.card-thumb-title {
    font-size: 0.95rem;
    font-weight: 600;
    color: var(--item-color, var(--accent-primary));
    text-align: center;
}

.card-thumb-client {
    font-size: 0.75rem;
    color: var(--text-muted);
}

.card-overlay {
    position: absolute;
    inset: 0;
    background: rgba(0,0,0,0.6);
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    transition: opacity 0.3s;
}

.portfolio-card:hover .card-overlay { opacity: 1; }

.view-btn {
    background: var(--item-color, var(--accent-primary));
    color: #000;
    padding: 0.5rem 1rem;
    border-radius: 8px;
    font-weight: 600;
    font-size: 0.85rem;
    transform: scale(0.9);
    transition: transform 0.3s ease;
}

.portfolio-card:hover .view-btn {
    transform: scale(1);
}

.card-type-badge {
    position: absolute;
    top: 10px;
    left: 10px;
    padding: 0.25rem 0.6rem;
    border-radius: 6px;
    font-size: 0.7rem;
    font-weight: 600;
    color: #000;
}

.card-content {
    padding: 1.25rem;
}

.card-content h3 {
    font-size: 1.1rem;
    margin-bottom: 0.5rem;
}

.card-content p {
    color: var(--text-secondary);
    font-size: 0.9rem;
    margin-bottom: 1rem;
    line-height: 1.5;
}

.card-client {
    font-size: 0.85rem;
    color: var(--text-muted);
    margin-bottom: 0.75rem;
}

.card-tech {
    display: flex;
    flex-wrap: wrap;
    gap: 0.4rem;
    margin-bottom: 1rem;
}

.tech-tag {
    font-size: 0.7rem;
    padding: 0.15rem 0.5rem;
    background: var(--bg-tertiary);
    border-radius: 4px;
    color: var(--text-muted);
}

.card-footer {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.card-status {
    font-size: 0.8rem;
}

.card-link {
    color: var(--accent-primary);
}

.empty-state {
    text-align: center;
    padding: 4rem 2rem;
    color: var(--text-muted);
}

.empty-icon { font-size: 4rem; margin-bottom: 1rem; }

.portfolio-nav {
    display: flex;
    justify-content: center;
    gap: 1rem;
    margin-top: 3rem;
    flex-wrap: wrap;
}

.portfolio-card.hidden { display: none; }

@media (max-width: 768px) {
    .portfolio-grid { grid-template-columns: 1fr; }
    .portfolio-stats { gap: 1rem; }
}
</style>

<script>
document.addEventListener('DOMContentLoaded', function() {
    var filterBtns = document.querySelectorAll('.filter-btn');
    var cards = document.querySelectorAll('.portfolio-card');
    
    filterBtns.forEach(function(btn) {
        btn.addEventListener('click', function() {
            var filter = this.dataset.filter;
            filterBtns.forEach(function(b) { b.classList.remove('active'); });
            this.classList.add('active');
            
            cards.forEach(function(card) {
                if (filter === 'all' || card.dataset.type === filter) {
                    card.classList.remove('hidden');
                } else {
                    card.classList.add('hidden');
                }
            });
        });
    });
});
</script>
