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

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

$data = json_decode(file_get_contents('php://input'), true);

if (empty($data['id'])) {
    http_response_code(400);
    echo json_encode(['success' => false, 'message' => 'ID requis']);
    exit;
}

try {
    $stmt = getDB()->prepare("UPDATE projects SET title = ?, client_name = ?, description = ?, full_content = ?, image = ?, gallery = ?, technologies = ?, featured = ?, order_position = ?, status = ? WHERE id = ?");
    $stmt->execute([
        $data['title'],
        $data['client_name'] ?? '',
        $data['description'] ?? '',
        $data['full_content'] ?? '',
        $data['image'] ?? '',
        json_encode($data['gallery'] ?? []),
        json_encode($data['technologies'] ?? []),
        $data['featured'] ? 1 : 0,
        $data['order_position'] ?? 0,
        $data['status'] ?? 'active',
        $data['id']
    ]);
    
    echo json_encode([
        'success' => true,
        'message' => 'Projet mis à jour avec succès'
    ]);
} catch (Exception $e) {
    http_response_code(500);
    echo json_encode(['success' => false, 'message' => $e->getMessage()]);
}
