<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

include 'connection.php';

header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json");

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

if (!$data) {
    echo json_encode(["success" => false, "message" => "No JSON data received"]);
    exit;
}

if (empty($data->emailOrMobile)) {
    echo json_encode(["success" => false, "message" => "emailOrMobile is missing"]);
    exit;
}

$emailOrMobile = $data->emailOrMobile;

// Prepare the query
$query = "SELECT id, status, com_name, mob_num, email 
          FROM registration 
          WHERE email = ? OR mob_num = ?";

$stmt = $conn->prepare($query);

if ($stmt === false) {
    echo json_encode(["success" => false, "message" => "Prepare failed: " . $conn->error]);
    exit;
}

// Bind parameters
$stmt->bind_param("ss", $emailOrMobile, $emailOrMobile);

// Execute
$stmt->execute();
$stmt->store_result();

if ($stmt->num_rows > 0) {
    // Bind result variables
    $stmt->bind_result($userId, $status, $name, $mobile, $email);
    $stmt->fetch();

    echo json_encode([
        "success" => true,
        "id" => $userId,
        "status" => $status,
        "com_name" => $name,
        "mob_num" => $mobile,
        "email" => $email
    ]);
} else {
    echo json_encode(["success" => false, "message" => "User not found"]);
}

$stmt->close();
$conn->close();
