<?php

namespace App\Domains\UserManagement\Controllers;

use App\Domains\UserManagement\Actions\UserActivation\UserActivationCommand;
use App\Domains\UserManagement\Actions\UserActivation\UserActivationRequest;
use App\Domains\UserManagement\Actions\UserActivation\UserDeactivationCommand;
use App\Domains\UserManagement\Actions\UserActivation\UserDeactivationRequest;
use App\Domains\UserManagement\Actions\UserChangePassword\UserChangePasswordCommand;
use App\Domains\UserManagement\Actions\UserChangePassword\UserChangePasswordRequest;
use App\Domains\UserManagement\Actions\UserRegistration\UserRegistrationCommand;
use App\Domains\UserManagement\Actions\UserRegistration\UserRegistrationRequest;
use App\Enumerations\UserRole;
use App\Models\User;
use App\Providers\InertiaProvider;
use App\Scopes\CommonController;
use App\ValueObjects\EmailAddress;
use Carbon\Traits\Date;
use DateTime;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class EmailVerificationController extends CommonController
{
    public function verifyEmailForm(Request $request) {
        $inertiaProvider = new InertiaProvider('UserManagement/VerifyEmail', ['appName' => app('tenant')->name]);
        return $inertiaProvider->render();
    }

    public function doVerification(Request $request) : JsonResponse
    {

        $user = $this->users->findByUsername($request->get('email'));
        if ($user === null) {
            return response()->json([
                'error_types' => [
                    'email' => 'Die E-Mail-Adresse konnte nicht zugeordnet werden.',
                ],
            ]);
        }

        if (new DateTime() > DateTime::createFromFormat('Y-m-d H:i:s', $user->activation_token_expires_at)) {
            return response()->json([
                'error_types' => [
                     'verificationToken' => 'Der Sicherheitsschlüssel ist abgelaufen.',
                ],
            ]);
        }

        if (!$this->users->checkVerificationToken($user, $request->get('verificationToken'))) {
            return response()->json([
                'error_types' => [
                    'verificationToken' => 'Der Sicherheitsschlüssel ist nicht korrekt',
                ],
            ]);
        }

        $userActivationRequest = new UserActivationRequest($user);
        $userActivationCommand = new UserActivationCommand($userActivationRequest);
        $activationResult = $userActivationCommand->execute();


        if (!$activationResult->success) {
            return response()->json([
                'error_types' => [
                    'verificationToken' => 'Ein allgemeiner Fehler ist aufgetreten. Bitte versuche es später noch einmal.',
                ],
            ]);
        }

        $userPasswordResetRequest = new UserChangePasswordRequest($user, $request->get('password'));
        $userPasswordResetCommand = new UserChangePasswordCommand($userPasswordResetRequest);
        $userPasswordResetCommand->execute();

        return response()->json([
            'status' => 'success',
            'message' => 'Dein Account wurde aktiviert.Du kannst dich nun mit deinem neuen Passwort einloggen.'
        ]);
    }
}
