<?php
session_start();

require_once '/home/tebefvsu/blockchain.tebekonomi.com/include/Constants.php';
require_once '/home/tebefvsu/blockchain.tebekonomi.com/include/Helper.php';
require_once "/home/tebefvsu/blockchain.tebekonomi.com/vendor/autoload.php";
require_once "/home/tebefvsu/blockchain.tebekonomi.com/include/DatabaseConn.php";
require_once "/home/tebefvsu/blockchain.tebekonomi.com/models/User.php";
require_once '/home/tebefvsu/blockchain.tebekonomi.com/models/Settings.php';
require_once '/home/tebefvsu/blockchain.tebekonomi.com/models/LoginRecord.php';

if (isset($_POST['email'])) {
    $email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
    if ($email == null || $email == FALSE) {
        $error = 'Email is not valid';
        die($error);
    } else {
        $db = new DatabaseConn();
        $link = $db->connect();
        $email = strtolower($email);
        $stmt = mysqli_prepare($link, "select email from users WHERE  email = ?");
        $stmt->bind_param("s", $email);
        $stmt->execute();
        $stmt->store_result();
        if ($stmt->num_rows > 0) {
            $error = 'Email already exist!';
            die($error);
        }
    }
    $password = filter_input(INPUT_POST, 'password', FILTER_SANITIZE_STRING);
    if ($password == null || $password == FALSE) {
        $error = 'Password is not valid';
        die($error);
    } else if (strlen($password) < 8) {
        $error = 'Password is weak';
        die($error);
    }
    
    $cpassword = filter_input(INPUT_POST, 'cpassword', FILTER_SANITIZE_STRING);
    if ($cpassword == null || $cpassword == FALSE) {
        $error = 'Confirmation Password is not valid';
        die($error);
    } else if (strcmp($password, $cpassword) != 0) {
        $error = 'Password does not match';
        die($error);
    }



    if (!isset($error)) {
        $db = new DatabaseConn();
        $link = $db->connect();
        $settings = Settings::getSystemSettings($link);
        date_default_timezone_set('Africa/Lagos');
        $time = time();
        $user = new User();
        $user->setCountry("n/a");
        $user->setDateJoined($time);
        $user->setEmail($email);
        $user->setName("n/a");
        $user->setPassword($password);
        $user->setPhone("n/a");
        $user->setStatus(($settings->getEmailVerification() == 1) ? NOT_CONFIRMED : NEW_USER);
        User::saveUser($user, $link);
        
        $response = (strcmp($user->getStatus(), NOT_CONFIRMED) == 0) ? User::sendEmailVerification($user, $link) : false;
        if ($response) {
            $_SESSION[EMAIL] = $user->getEmail();
            die('CONFIRM');
        } else if (is_null($response)) {
            User::deleteUser($user->getEmail(), $link);
            $error = "We couldn't verify your email";
            die($error);
        } else {
            LoginRecord::saveUserRecords($user, $link);
            $_SESSION[USER] = $user->getEmail();
            header("location: dashboard/");
            die("OK");
        }
    }
}


?>
