<?php

namespace App\Controllers\Auth;

use App\Auth\Auth;
use App\Controllers\Controller;
use App\Lib\HmailAPI;
use App\Models\User;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Respect\Validation\Validator as v;

/**
 * PasswordController
 *
 * @author    Hezekiah O. <support@hezecom.com>
 */
class PasswordController extends Controller
{
    /**
     * @param Request $request
     * @param Response $response
     * @return mixed
     * @throws \DI\DependencyException
     * @throws \DI\NotFoundException
     */
    public function createForgotPassword(Request $request, Response $response){
        return view($response,'auth/password/forgot-password.twig');
    }

    /**
     * @param Request $request
     * @param Response $response
     * @throws \Delight\Auth\AuthError
     */
    public function forgotPassword(Request $request, Response $response){
        $data = $request->getParsedBody();
        Auth::forgotPassword($data['email']);
    }

    /**
     * @param Request $request
     * @param Response $response
     * @throws \Delight\Auth\AuthError
     */
    public function resetPassword(Request $request, Response $response){
        $data = $request->getQueryParams();
        Auth::resetPasswordVerify($data['selector'], $data['token'], $data['email']);
        return $response;
    }

    /**
     * @param Request $request
     * @param Response $response
     * @return mixed
     * @throws \DI\DependencyException
     * @throws \DI\NotFoundException
     */
    public function createUpdatePassword(Request $request, Response $response){
        $data = $request->getQueryParams();
        $selector = $data['selector'];
        $token = $data['token'];
        $email = $data['email'].'@'.envi('CPANEL_DOMAIN');
        return view($response,'auth/password/update-password.twig', compact('selector','token', 'email'));
    }

    /**
     * @param Request $request
     * @param Response $response
     * @throws \Delight\Auth\AuthError
     */
    public function updatePassword(Request $request, Response $response){
        $data = $request->getParsedBody();
        $validation = $this->validator->validate($request, [
            'password' => v::notEmpty()->stringType()->length(8),
            'password2' => v::notEmpty(),
        ]);

        if ($validation->failed()) {
            redirect()->route('update.password',[],['selector'=>urlencode($data['selector']),'token'=>urlencode($data['token']),'email'=>urlencode($data['email'])]);
        }

        if (!v::equals($data['password'])->validate($data['password2'])) {
            redirect()->route('update.password',[],['selector'=>urlencode($data['selector']),'token'=>urlencode($data['token']),'email'=>urlencode($data['email'])])->with('error','The password do not match.');
        }
        if(HmailAPI::updateEmailPassword($data['email'], $data['password'])) {
            Auth::resetPasswordUpdate($data['selector'], $data['token'], $data['password']);
            User::where('email', $data['email'])->update(['web_pass' => encrypt($data['password'])]);
            redirect()->route('login')->with('success','Password changed successfully');
        }else{
            redirect()->route('update.password',[],['selector'=>urlencode($data['selector']),'token'=>urlencode($data['token']),'email'=>urlencode($data['email'])])->with('error',lang('cpanel_password_change_error'));
        }
    }

}
