<?php

namespace Modules\User\Services;

use Illuminate\Http\Request;
use Carbon\Carbon;
use Illuminate\Support\Facades\Session;
use Modules\User\Models\User;
use Modules\User\Interfaces\UserRepositoryInterface;

class UserService
{
    private UserRepositoryInterface $userRepository ;

    public function __construct(UserRepositoryInterface $userRepository)
    {
        $this->userRepository = $userRepository;
    }

    public function get(Request $request)
    {
        $users= $this->userRepository->get($request);
        return $users;
    }
    public function getItem(User $user)
    {
        $user = $this->userRepository->getItem($user);
        return $user;
    }
    public function getItemByEmail(string $email)
    {
        $user = $this->userRepository->getItemByEmail($email);
        return $user;
    }
    public function save(Request $request, ?User $user): User
    {
        if ($request->password)
        {
            $request->merge(['password'=>bcrypt($request->password)]);
        }
        $user = $this->userRepository->save($request,$user);
        return $user;
    }
    public function destroy(Request $request, int $id): array
    {
        $response = $this->userRepository->destroy($request,$id);
        return $response;
    }
    public function destroyAll(Request $request): array
    {
        $response = $this->userRepository->destroyAll($request);
        return $response;
    }
    public function restore(Request $request, int $id): array
    {
        $response = $this->userRepository->restore($request,$id);
        return $response;
    }
    public function restoreAll(Request $request): array
    {
        $response = $this->userRepository->restoreAll($request);
        return $response;
    }

    public function login(array $requestData) :array
    {
        $response = [
            'status'=> 401,
            'message'=> __('web.check_inputs'),
            'data'=> null,
        ];
        $credentials = [
            'email' => $requestData['email'],
            'password' => $requestData['password']
        ];
        $user = $this->userRepository->login($credentials);
        if ($user)
        {
            $response = [
                'status'=>200,
                'message'=>__('web.success'),
                'data'=> route('web.home'),
            ];
        }
        return $response;
    }

    public function sendCode(array $requestData):void
    {
        $requestData['code'] = 1234;
        $requestData['target'] = $requestData['email'];
        $this->userRepository->sendCode($requestData);
    }

    public function checkCode(array $requestData)
    {
        $response = [
            'status' => 401,
            'message' => __('web.incorrect_code'),
            'data' => null
        ];
        $code = $this->userRepository->checkCode($requestData);
        if ($code)
        {
            $date = Carbon::parse($code->created_at);
            $now = Carbon::now();

            $diff = $date->diffInSeconds($now);
            if ($code->code != $requestData['code'])
            {
                $response['message'] = __('web.incorrect_code');
            }
            elseif ($diff > 120)
            {
                $response['message'] = __('web.expired_code');
            }
            else
            {
                Session::put(['email'=>$requestData['email']]);
                $response = [
                    'status' => 200,
                    'message' => __('web.success'),
                    'data' => route('web.resetpassword')
                ];
            }
        }
        return $response;
    }
}
