<?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;
use Modules\User\Services\AddressService;
use Modules\User\Enums\AddressUserStatusEnum;
use Modules\User\Enums\UserRoleEnum;

class UserService
{
    private UserRepositoryInterface $userRepository ;
    private AddressService $addressService;

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

    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)]);
        }
        else
        {
            $request->request->remove('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):string
    {
        $requestData['code'] = 1234;
        $requestData['target'] = $requestData['phone'];
        $this->userRepository->sendCode($requestData);
        return '1234';
    }

    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' => ''
                ];
            }
        }
        return $response;
    }
    
    public function regist(Request $request)
    {
        $request->merge(['role'=>UserRoleEnum::USER->value]);
        $user = $this->save($request,null);
        $request->merge(['user_id'=>$user->id,'user_status'=>AddressUserStatusEnum::USER->value]);
        $address = $this->addressService->save($request,null);
        $user = $this->createToken($user);
        $user = $this->setAddress($user,$address->id);
        return $user;
    }
    
    public function setAddress(User $user,$address_id)
    {
        $user = $this->userRepository->setAddress($user,$address_id);
        return $user;
    }
    public function createToken(User $user)
    {
        $user = $this->userRepository->createToken($user);
        return $user;
    }
    
    public function createAdminProvider($request)
    {
        $request['role'] = UserRoleEnum::STORE_ADMIN->value;
        $user = $this->userRepository->createAdminProvider($request);
        return $user;
    }
}
