<?php

class User
{
    public static $sql;
    public static function signup($user, $pass, $email, $phone)
    {
        // Store $conn values from class Database
        $conn = Database::getConnection();

        // To save password with password_hash()
        $option = [
            'cost' => 9
        ];
        $pass = password_hash($pass, PASSWORD_BCRYPT, $option);

        $sql = "INSERT INTO `auth` (`username`, `password`, `email`, `phone`, `block`, `active`)
    VALUES ('$user', '$pass', '$email', '$phone', '0', '1');";
        $error = false;


        if ($conn->query($sql) === true) {
            $error = false;
        } else {
            echo "Error: " . $sql . "<br>" . $conn->error;
            $error = $conn->error;
        }

        $conn->close();
        return $error;
    }

    // Check whether the user credential is exists in database
    public static function login($user, $pass)
    {

        // Since it is in static function we need to declare again in this function.
        $password = $pass;

        // store query in a variable
        $query = "SELECT * FROM `auth` WHERE `username` = '$user'";

        // To get database connection
        $conn = Database::getConnection();

        // sends the query with query() to get the data from database
        $result = $conn -> query($query);

        /*
        [*] Accessing (num_rows) is the variable present inside the class eg: $object->variable_name;
        */
        if ($result -> num_rows == 1) {

            // fetch data as array from database and store in $row
            $row = $result->fetch_assoc();

            // validate password with password_verify() from database
            // if ($row['password'] == $password) {
            if (password_verify($password, $row['password'])) {
                return $row;
            } else {
                return false;
            }
        } else {
            return false;
        }

    }
}