<?php
function emptyInputSignup($name, $email, $username, $pwd, $pwdrepeat) //cheks if input is empty / pārbauda vai kāds no laukiem nav tukšs
{
    $result;
    if (empty($name) || empty($email) || empty($username) || empty($pwd) || empty($pwdrepeat)) {
        $result = true;
    } else {
        $result = false;
    }
    return $result;
}

function invalidUid($username) //cheks if the username is written correctly / pārbauda vai lietotājvārds ir irvadīts korekti
{
    $result;
    if (!preg_match("/^[a-zA-Z0-9]*$/", $username)) {
        $result = true;
    } else {
        $result = false;
    }
    return $result;
}

function invalidEmail($email) //cheks if email is written correctly / pārbauda vai E-pasts ir ievadīds korekti
{
    $result;
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $result = true;
    } else {
        $result = false;
    }
    return $result;
}

function pwdMatch($pwd, $pwdrepeat) //Cheks if passwords match / pārbauda vai paroles sakrīt
{
    $result;
    if ($pwd !== $pwdrepeat) {
        $result = true;
    } else {
        $result = false;
    }
    return $result;
}

function uidExists($conn, $username, $email)//creates a user that checks that someone with that username or email already exists / pārbauda vai šads lietotājs jau neeksistē datubāzē 
{
    $sql = "SELECT * FROM users WHERE usersUid = ? OR usersEmail = ?;";
    $stmt = mysqli_stmt_init($conn);
    if (!mysqli_stmt_prepare($stmt, $sql)) {
        header("location: ../signup.php?error=stementfailed");
        exit();
    }

    mysqli_stmt_bind_param($stmt, "ss", $username, $email);
    mysqli_stmt_execute($stmt);

    $resultsData = mysqli_stmt_get_result($stmt);

    if ($row = mysqli_fetch_assoc($resultsData)) {
        return $row;
    } else {
        $result = false;
        return $result;
    }
    mysqli_stmt_close($stmt);
}

function createUser($conn, $name, $email, $username, $pwd) //inserts users submited data to database / ievieto datubāzē ievadītos datuss
{
    $sql = "INSERT INTO users (usersName, usersEmail, usersUid, usersPwd) VALUES (?, ?, ?, ?);";
    $stmt = mysqli_stmt_init($conn);
    if (!mysqli_stmt_prepare($stmt, $sql)) {
        header("location: ../signup.php?error=stementFailed");
        exit();
    }

    $hashedPwd = password_hash($pwd, PASSWORD_DEFAULT);//cheks that everything goes smoothy / pārbauda vai viss notika korekti 

    mysqli_stmt_bind_param($stmt, "ssss", $name, $email, $username, $hashedPwd);
    mysqli_stmt_execute($stmt);
    mysqli_stmt_close($stmt);
    header("location: ../signup.php?error=none");
    exit();
}


function emptyInputLogin($username, $pwd) //emty login funtion / pārbauda vai kāds no laukkiem nav tukšs
{
    $result;
    if (empty($username) || empty($pwd)) {
        $result = true;
    } else {
        $result = false;
    }
    return $result;
}

function loginUser($conn, $username, $pwd)
{
    $uidExists = uidExists($conn, $username, $username);

    if ($uidExists === false) {
        header("location: ../login.php?error=wrongLogin"); //cheks username / pārbauda lietotājvārdu 
    }

    $hashedPwd = $uidExists["usersPwd"];
    $checkHashedPwd = password_verify($pwd, $hashedPwd);

    if ($checkHashedPwd === false) {
        header("location: ../login.php?error=wrongLogin"); //cheks password / pārbauda paroli
    } 
    else if ($checkHashedPwd === true) {
        session_start();
        $_SESSION["userid"] = $uidExists["usersId"]; //starts session / uzzsāk sesiju 
        header("location: ../afterlogin.php");
        exit;
    }
}