<?php
include($_SERVER['DOCUMENT_ROOT'] . '/logic/testinput.php');

$data = json_decode(file_get_contents('php://input'));
getData($data->username);

function getData($email)
{
    require($_SERVER['DOCUMENT_ROOT'] . '/config/setupDBAccess.php');
    
    $sql = "SELECT `user_id`, `username`, `password`, `email`, `phone`, `salutation`, `firstname`, `lastname`, `address`, `role`, `created_at`, `plz`, `name` FROM `user` JOIN `cities` ON `plz` = `postalcode` WHERE `username` = ?";
    $stmt = $db->prepare($sql);
    $stmt->bind_param("s", $email);
    
    $stmt->execute();
    $stmt->store_result();
    $stmt->bind_result($user_id, $username, $password, $email, $phone, $salutation, $firstname, $lastname, $address, $role, $created_at, $plz, $name);
    if ($stmt->num_rows == 1) {
        if ($stmt -> fetch()) {
            $data = array(
                'user_id' => $user_id,
                'username' => $username,
                'password' => $password,
                'email' => $email,
                'phone' => $phone,
                'salutation' => $salutation,
                'firstname' => $firstname,
                'lastname' => $lastname,
                'address' => $address,
                'plz' => $plz,
                'city' => $name,
                'role' => $role,
                'created_at' => $created_at
            );
            $data = json_encode($data);
            $response = $data;
        } else {
            $response = "failure";
        }
    } else {
        $response = "failure";
    }
    $stmt->close();
    $db->close();
    
    echo $response;
}