<?php

namespace App\Http\Controllers\Web;


use App\Http\Controllers\Controller;
use App\Services\Response\ResponseService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Session;
use Illuminate\View\View;
use Modules\User\Enums\UserRoleEnum;
use Modules\User\Requests\Web\FogetPasswordRequest;
use Modules\User\Requests\Web\PhoneRequest;
use Modules\User\Requests\Web\LoginRequest;
use Modules\User\Requests\Web\RegistRequest;
use Modules\User\Requests\Web\VerifyCodeRequest;
use Modules\User\Requests\Web\ResetPasswordRequest;
use Modules\User\Requests\Web\VerifyPhoneRequest;
use Modules\User\Services\UserService;
use Modules\Category\Services\CategoryService;
use Modules\City\Services\CityService;
use Modules\Store\Services\StoreService;

class AuthController extends Controller
{
    private $view = 'web.pages.auth.';

    private UserService $userService;
    private CategoryService $categoryService;
    private CityService $cityService;
    private StoreService $storeService;

    public function __construct(
        UserService $userService,
        CategoryService $categoryService,
        CityService $cityService,
        StoreService $storeService
    )
    {
        $this->userService = $userService;
        $this->categoryService = $categoryService;
        $this->cityService = $cityService;
        $this->storeService = $storeService;
    }

    
    
    public function registPhone(Request $request) :View
    {
        $title = __('web.regist');
        $action = route('web.regist.phone.check');
        return view($this->view.'phone', get_defined_vars());
    }

    public function registPhoneCheck(PhoneRequest $request)
    {
        $response = [
            'status' => 200,
            'message' => __('web.success'),
            'data' => $request->phone
        ];
        $this->userService->sendCode($request->all());
        return ResponseService::response($response);
    }






    public function verifyCodeCheck(VerifyCodeRequest $request) :JsonResponse
    {
        $response = $this->userService->checkCode($request->all());
        $response['data'] = route('web.regist');
        return ResponseService::response($response);
    }
    
    
    public function regist(Request $request)
    {
        if (!Session::get('regist'))
        {
            return redirect(route('web.regist.phone'));
        }
        $title = __('web.regist');
        $action = route('web.regist.store');
        $cities = $this->cityService->get($request);
        $request = $request->merge(['category_id'=>'main','store_id'=>'main']);
        $categories = $this->categoryService->get($request);
        return view($this->view.'regist', get_defined_vars());
    }
    
    
    public function providerStore(RegistRequest $request) :JsonResponse
    {
        $phone = Session::get('phone');
        $request->merge(['phone'=>$phone]);
        $this->storeService->create($request);

        Session::forget(['regist','phone']);
        
        $response = [
            'message'=>__('web.wait_management'),
            'data'=>route('web.home'),
            'status'=>200
        ];

        return ResponseService::response($response);
    }

}
