<?php

session_start(); // user email should not be longer 40 characters
date_default_timezone_set('Africa/Lagos');

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

require_once './vendor/autoload.php';
require_once './models/User.php';
require_once './include/DatabaseConn.php';
require_once './include/Constants.php';
require_once './include/Helper.php';
require_once './models/LoginRecord.php';
require_once './models/USettings.php';
require_once './include/GoogleAuthenticator.php';
require_once './models/Settings.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);
    }
    $password = filter_input(INPUT_POST, 'password');
    if ($password == null || $password == FALSE) {
        $error = 'Password is not valid';
        die($error);
    }
    if (!isset($error)) {
        $db = new DatabaseConn();
        $link = $db->connect();
        $user = User::verifyLoginCredentials(User::instantiate($email, $password), $link);
        if (is_null($user)) {
            $error = "Invalid login crendentials";
            die($error);
        } else if ($user->getStatus() == SUSPENDED) {
            $error = "Your account has been suspended. Contact support.";
            die($error);
        } else {
            if (strcmp(NOT_CONFIRMED, $user->getStatus()) == 0) {
                $_SESSION[USER_] = serialize($user);
                $_SESSION['gid'] = true;
                $error = "Verify your email to continue. <a href='gid'>Click to continue</a>";
                die("verify");
            } else {
                die("OK");
            }
        }
    }
}
$title = 'Log In ' . APP_NAME;
?>
