<?php
// Load environment variables from .env file
// **FIX:** Corrected the path to go up three levels to find the 'vendor' directory
require_once realpath(__DIR__ . '/../../../vendor/autoload.php');
// **FIX:** Corrected the path to go up two levels to find 'load_env.php'
require_once realpath(__DIR__ . '/../../load_env.php');

$env_file = '/home/tripz-egypt-wl/env/.env';
loadEnvironment($env_file);

// --- CORS Headers ---
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: POST, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type");
header('Content-Type: application/json');

if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
    http_response_code(200);
    exit;
}

$dbname = getenv('dbname');
// --- Database Connection ONLY ---
try {
    $dsn = "mysql:host=localhost;dbname=$dbname;charset=utf8mb4";
    $options = [
        PDO::ATTR_EMULATE_PREPARES   => false,
        PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
        PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES UTF8"
    ];
    $user = getenv('USER');
    $pass = getenv('PASS');
    $con = new PDO($dsn, $user, $pass, $options);

} catch (PDOException $e) {
    error_log("Webhook DB Connection Error: " . $e->getMessage());
    http_response_code(500);
    echo json_encode(['error' => 'Internal Server Error']);
    exit;
}
?>
