<?php
session_start();

header("Access-Control-Allow-Origin: https://enerliths.com");
header("Access-Control-Allow-Credentials: true");
header("Access-Control-Allow-Headers: Content-Type");
header("Access-Control-Allow-Methods: GET, OPTIONS");
header("Content-Type: application/json");

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

if (isset($_SESSION['valid']) && $_SESSION['valid'] === true) {
    echo json_encode([
        "authenticated" => true,
        "user_id" => $_SESSION['user_id'],
        "email" => $_SESSION['email'],
        "full_name" => $_SESSION['full_name'] ?? null,
        "role" => $_SESSION['role'] ?? 'user'
    ]);
} else {
    echo json_encode([
        "authenticated" => false
    ]);
}
