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

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

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

if (empty($data['name']) || empty($data['role'])) {
    http_response_code(400);
    echo json_encode(['success' => false, 'message' => 'Nom et rôle sont requis']);
    exit;
}

try {
    $stmt = getDB()->prepare("INSERT INTO team_members (name, role, bio, image, email, linkedin, order_position, status) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
    $stmt->execute([
        $data['name'],
        $data['role'],
        $data['bio'] ?? '',
        $data['image'] ?? '',
        $data['email'] ?? '',
        $data['linkedin'] ?? '',
        $data['order_position'] ?? 0,
        $data['status'] ?? 'active'
    ]);
    
    echo json_encode([
        'success' => true,
        'message' => 'Membre ajouté avec succès',
        'id' => getDB()->lastInsertId()
    ]);
} catch (Exception $e) {
    http_response_code(500);
    echo json_encode(['success' => false, 'message' => $e->getMessage()]);
}
