<?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");

if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') exit;

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

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

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

// Check if JSON decoded properly
if (!$data) {
    echo json_encode(["success" => false, "message" => "Invalid JSON input"]);
    exit;
}

// Check required field
if (isset($data->email)) {
    $email = $conn->real_escape_string($data->email);

    // Fetch password from adminlogins
    $sql = "SELECT password FROM adminlogins WHERE email='$email' LIMIT 1";
    $result = $conn->query($sql);

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

echo json_encode($response);
?>
