<?php

namespace Lycoreco\Apps\Users\Controllers;

use Lycoreco\Apps\Users\Models\{
    UserModel,
    ActivateCodeModel
};
use Lycoreco\Includes\BaseController;
use Lycoreco\Includes\Model\ValidationError;

class RegisterController extends BaseController
{
    protected $template_name = APPS_PATH . '/Users/Templates/register.php';

    protected function distinct()
    {
        // If current user is authorized, redirect to homepage
        if (!empty(CURRENT_USER)) {
            $home = get_permalink('index:home');
            redirect_to($home);
        }
    }

    protected function post()
    {
        $username = $_POST['username'] ?? null;
        $email = $_POST['email'] ?? null;
        $pass = $_POST['password'] ?? null;
        $repeat = $_POST['repeat'] ?? null;

        $fname = $_POST['fname'] ?? '';
        $lname = $_POST['lname'] ?? '';

        // If password is not valid
        if (UserModel::valid_password($pass) !== true) {
            $context['error_form'] = new ValidationError(UserModel::valid_password($pass));
            return;
        }
        if ($pass != $repeat) {
            $context['error_form'] = new ValidationError(["Passwords don't match"]);
            return;
        }
        $pass_hash = UserModel::password_hash($pass);

        $new_user = new UserModel(array(
            'username' => $username,
            'email' => $email,
            'password' => $pass_hash,
            'fname' => $fname,
            'lname' => $lname
        ));
        try {
            $new_user->save();
            ActivateCodeModel::send_activation($new_user);
            redirect_to(get_permalink('index:home'));
        } catch (ValidationError $ex) {
            $this->context['error_form'] = $ex;
            return;
        } catch (\Exception $ex) {
            $this->context['error_message'] = 'Unexpected error';
            return;
        }
    }
}
