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

class DataHandler
{
    public function registerUser($data)
    {
        $username = testinput($data->email);
        $password = password_hash(testinput($data->password), PASSWORD_DEFAULT);
        $email = testinput($data->email);
        $phone = testinput($data->phone);
        $salutation = testinput($data->salutation);
        $firstname = testinput($data->firstname);
        $lastname = testinput($data->lastname);
        $street = testinput($data->street);
        $postalcode = testinput($data->postalcode);
        $city = testinput($data->city);
        $role = "customer";

        require($_SERVER['DOCUMENT_ROOT'] . '/config/setupDBAccess.php');
        
        $sql2 = "INSERT IGNORE INTO `cities` (`city_id`, `postalcode`, `name`) VALUES (?,?,?)";
        $sql = "INSERT INTO `user` (`user_id`, `username`, `password`, `email`, `phone`, `salutation`, `firstname`, `lastname`, `address`, `plz`, `role`, `created_at`) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)";
        

        $stmtUser = $db->prepare($sql);
        $stmtCities = $db->prepare($sql2);

        $stmtUser->bind_param("ssssssssss", null, $username, $password, $email, $phone, $salutation, $firstname, $lastname, $street, $postalcode, $role, null);
        $stmtCities->bind_param("sss", null, $postalcode, $city);

        if ($stmtUser->execute() && $stmtCities->execute()) {
            echo "Your registration was successfully.\n";
            $returnArray['Response'] = "Your registration was successfully.";
        } else {
            echo "Your registration was not successfully. Please try again later.\n";
            $returnArray['Response'] = "Your registration was not successfully. Please try again later.";
        }
        $stmtUser->close();
        $stmtCities->close();
        $stmtAddress->close();
        $db->close();

        return $returnArray;
        
    }
}
?>