<?php
// db_connect.php - connexion PDO unique
// A inclure avec : require_once __DIR__.'/db_connect.php';

if (!defined('DB_CONNECTED')) {
    define('DB_CONNECTED', true);

    // --- CONFIG DB ---
    $DB_HOST = '127.0.0.1';
    $DB_PORT = '3306'; // <-- port ajouté
    $DB_USER = 'root';
    $DB_PASS = 'root';
    $DB_NAME = 'jgsborne10';
    $DB_CHARSET = 'utf8';

    // Options PDO recommandées
    $options = [
        PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
        PDO::ATTR_EMULATE_PREPARES   => false,
        PDO::ATTR_PERSISTENT         => false,
    ];

    try {
        $dsn = "mysql:host=$DB_HOST;port=$DB_PORT;dbname=$DB_NAME;charset=$DB_CHARSET";
        $pdo = new PDO($dsn, $DB_USER, $DB_PASS, $options);
    } catch (PDOException $e) {
        http_response_code(500);
        header('Content-Type: application/json; charset=utf-8');
        echo json_encode([
            'success' => false,
            'message' => 'Erreur connexion DB',
            'error' => $e->getMessage()
        ]);
        exit;
    }
}