<?php
// Define base path depending on environment
$base_dir = __DIR__;
try {
    if (file_exists($base_dir . '/api/db_config.php')) {
        require_once $base_dir . '/api/db_config.php';
    } else if (file_exists(dirname($base_dir) . '/api/db_config.php')) {
        require_once dirname($base_dir) . '/api/db_config.php';
    }
} catch (Exception $e) {
    // DB config failed, $pdo won't exist — we'll fall through gracefully
}

$html = file_get_contents(__DIR__ . '/index.html');

try {
    if (!isset($pdo)) throw new Exception('No DB connection');
    $stmt = $pdo->query("
        SELECT p.*, 
        (SELECT image_url FROM project_images WHERE project_id = p.id AND is_primary = 1 LIMIT 1) as primary_image,
        GROUP_CONCAT(s.tech_name SEPARATOR ',') as stack
        FROM projects p
        LEFT JOIN project_stack s ON p.id = s.project_id
        GROUP BY p.id
        ORDER BY p.created_at DESC
    ");
    $projects = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    $projectsHtml = '';
    
    foreach ($projects as $project) {
        $img = htmlspecialchars($project['primary_image'] ?? 'https://images.unsplash.com/photo-1551033406-611cf9a28f67?q=80&w=1000');
        $title = htmlspecialchars($project['title']);
        $category = htmlspecialchars($project['category']);
        $desc = htmlspecialchars($project['description']);
        $slug = htmlspecialchars($project['slug']);
        
        $stackHtml = '';
        if (!empty($project['stack'])) {
            $techs = explode(',', $project['stack']);
            foreach ($techs as $tech) {
                $stackHtml .= '<span style="font-size: 0.7rem; text-transform: uppercase; letter-spacing: 0.1em; border: 1px solid #333; padding: 0.2rem 0.8rem; border-radius: 50px;">' . htmlspecialchars($tech) . '</span>';
            }
        }
        
        $privateBadge = $project['is_private'] ? '<div style="position: absolute; top: 1rem; right: 1rem; background: rgba(0,0,0,0.8); backdrop-filter: blur(4px); padding: 0.3rem 0.8rem; border-radius: 50px; font-size: 0.7rem; text-transform: uppercase; letter-spacing: 0.1em; z-index: 2; border: 1px solid #333;">🔒 Private</div>' : '';
        
        // Link wrapper replaces click handler for crawler
        $projectsHtml .= '
        <div class="project-card" style="cursor: pointer; position: relative;">
            <a href="/projects/' . $slug . '" style="text-decoration: none; color: inherit; display: block;">
                ' . $privateBadge . '
                <div class="image-wrapper asymmetric-border" style="aspect-ratio: 4/3; margin-bottom: 1.5rem; overflow: hidden; background: #111;">
                    <img src="' . $img . '" alt="' . $title . '" loading="lazy" style="width: 100%; height: 100%; object-fit: cover; transition: transform 0.6s cubic-bezier(0.16, 1, 0.3, 1);">
                </div>
                <div style="font-size: 0.7rem; text-transform: uppercase; letter-spacing: 0.2em; color: var(--primary); margin-bottom: 0.5rem;">' . $category . '</div>
                <h3 class="serif" style="font-size: 1.8rem; margin-bottom: 1rem; line-height: 1.2;">' . $title . '</h3>
                <p style="color: #888; font-size: 0.95rem; margin-bottom: 1.5rem; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;">' . $desc . '</p>
                <div style="display: flex; gap: 0.5rem; flex-wrap: wrap;">
                    ' . $stackHtml . '
                </div>
            </a>
        </div>';
    }
    
    // Inject the HTML into the div
    $targetDiv = '<div id="projects-grid" style="display: grid; grid-template-columns: repeat(auto-fill, minmax(350px, 1fr)); gap: 3rem;">';
    $html = str_replace($targetDiv, $targetDiv . "\n" . $projectsHtml, $html);
    
} catch (Exception $e) {
    // If DB fails, output raw HTML to let JS take over gracefully
}

echo $html;
