<?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 getItemByPhone(int $phone)
    {
        $user = $this->userRepository->getItemByPhone($phone);
        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 = [
            'phone' => $requestData['phone'],
            '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['phone'];
        $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
            {
                if ($this->getItemByPhone($requestData['phone']))
                {
                    $route = route('web.resetpassword');
                    Session::put(['resetpassword'=>'on']);
                }
                else
                {
                    $route = route('web.regist');
                    Session::put(['regist'=>'on']);
                }
                Session::put(['phone'=>$requestData['phone']]);
                $response = [
                    'status' => 200,
                    'message' => __('web.success'),
                    'data' => $route
                ];
            }
        }
        return $response;
    }
}
