<?php

namespace App\Domains\UserManagement\Controllers;

use App\Domains\UserManagement\Actions\GenerateActivationToken\GenerateActivationTokenCommand;
use App\Domains\UserManagement\Actions\GenerateActivationToken\GenerateActivationTokenRequest;
use App\Domains\UserManagement\Actions\GenerateActivationToken\GenerateActivationTokenResponse;
use App\Domains\UserManagement\Actions\UserActivation\UserActivationCommand;
use App\Domains\UserManagement\Actions\UserActivation\UserActivationRequest;
use App\Providers\InertiaProvider;
use App\Scopes\CommonController;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class ResetPasswordController extends CommonController {
    public function resetPasswordForm() {
        $inertiaProvider = new InertiaProvider('UserManagement/ResetPassword', []);
        return $inertiaProvider->render();
    }

    public function doResetPassword(Request $request) : JsonResponse {
        $user = $this->users->findByUsername($request->get('email'));

        if (null !== $user) {
            $expirationDate = new \DateTime()->modify('+15 Minutes');

            $resetAccountRequest = new GenerateActivationTokenRequest($user, $expirationDate);
            $resetAccountCommand = new GenerateActivationTokenCommand($resetAccountRequest);
            $resetAccountCommand->execute();
        }


        return response()->json([
            'status' => 'success',
            'message' => 'Falls deine E-Mail-Adresse gefunden wurde, erhältst du nun eine E-Mail mit weiteren Schritten zum Zurücksetzen deines Passwortes.'
        ]);

    }
}
