<?php
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');

require_once '../config/database.php';

try {
    $stmt = getDB()->query("SELECT * FROM projects ORDER BY order_position ASC");
    $projects = $stmt->fetchAll();
    
    // Decode JSON fields
    foreach ($projects as &$project) {
        $project['gallery'] = json_decode($project['gallery'] ?? '[]', true);
        $project['technologies'] = json_decode($project['technologies'] ?? '[]', true);
    }
    
    echo json_encode([
        'success' => true,
        'data' => $projects
    ]);
} catch (Exception $e) {
    http_response_code(500);
    echo json_encode(['success' => false, 'message' => $e->getMessage()]);
}
