<?php
// Load environment variables from .env file
require_once realpath(__DIR__ . '/../vendor/autoload.php');
require_once 'load_env.php';
$env_file = '/home/intaleq-walletintaleq/env/.env';
loadEnvironment($env_file);

// Get environment variables (You don't need user/pass for JWT auth itself)
$secretKey = getenv('SECRET_KEY'); // Only need the secret key now

// --- CORS Headers ---
header("Access-Control-Allow-Origin: https://wallet.sefer.live");
header("Access-Control-Allow-Methods: GET, POST, OPTIONS"); // Adjust as needed
header("Access-Control-Allow-Headers: Content-Type, Authorization");
header('Content-Type: application/json'); // Set content type to JSON

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

$dbname = getenv('dbname');

// --- Database Connection (Still needed for your application logic) ---
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'); // Still used for DB connection
    $pass = getenv('PASS'); // Still used for DB connection
    $con = new PDO($dsn, $user, $pass, $options);
    
// echo $con;
    // --- JWT Authentication ---
    include "functions.php"; // Include the functions file

     $decodedToken = authenticateJWT(); // Call the authentication function


} catch (PDOException $e) {
    error_log($e->getMessage());
    http_response_code(500); // Internal Server Error
    echo json_encode(['error' => 'A database error occurred.']);
    exit;
}
?>