<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type, Authorization");

// Handle preflight request
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') exit;

include_once __DIR__ . "/../config.php";

$response = ["success" => false, "message" => "Invalid request"];

// Get JSON input
$data = json_decode(file_get_contents("php://input"));

if (isset($data->email)) {
    $email = $conn->real_escape_string($data->email);

    // ✅ Fetch all details but exclude password and pin
    $sql = "SELECT client_index, username, email, gender, dob, number, address, website, date, uid, 
                   company_name, company_address, company_number, db_prefex, status, profile_pic, empId, 
                   userDepartment, userRole, userAccessLevel, emailToken, email_confirmed, 
                   email_token_time, country, dial_code, zipcode, city, state 
            FROM adminlogins 
            WHERE email='$email' 
            LIMIT 1";

    $result = $conn->query($sql);

    if ($result && $result->num_rows > 0) {
        $user = $result->fetch_assoc();
        $response = ["success" => true, "data" => $user];
    } else {
        $response = ["success" => false, "message" => "Admin not found"];
    }
} else {
    $response = ["success" => false, "message" => "Email required"];
}

// Return JSON response
echo json_encode($response);
?>
