<?php

namespace App\Http\Controllers\Web;

use App\Http\Requests\Web\CodeRequest;
use App\Http\Requests\Web\LoginRequest;
use App\Models\Order;
use App\Models\User;
use App\Services\Interfaces\CategoryRepositoryInterface;
use App\Services\Interfaces\MemberRepositoryInterface;
use App\Services\Interfaces\OrderRepositoryInterface;
use App\Services\Interfaces\ProductRepositoryInterface;
use App\Services\Interfaces\ReservationRepositoryInterface;
use App\Services\Interfaces\SliderRepositoryInterface;
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\View\View;

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


    public function __construct(
        MemberRepositoryInterface $memberRepository,
        OrderRepositoryInterface $orderRepository,
        ReservationRepositoryInterface $reservationRepository
    )
    {
        $this->memberRepository = $memberRepository;
        $this->orderRepository = $orderRepository;
        $this->reservationRepository = $reservationRepository;
    }

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

    public function check(LoginRequest $request) :JsonResponse
    {
        $response = [
            'status'=>200,
            'message'=>'',
            'data'=> route('web.code').'?phone='.$request->phone,
        ];
        return ResponseService::response($response);
    }

    public function code(LoginRequest $request) :View
    {
        $title = __('web.verify');
        $phone = $request->phone;
        $request->session()->forget('success');
        $request->session()->forget('error');
        if($this->memberRepository->canSendCode($request)){
            $this->memberRepository->sendCode($request);
            session()->flash('success',__('web.send_success'));
        }else{
            session()->flash('error',__('web.exceed_sent_limit'));
        }
        return view($this->view.'code', get_defined_vars());
    }

    public function codeCheck(CodeRequest $request) :JsonResponse
    {
        $response = $this->memberRepository->checkCode($request);
        if($response['status'] == 200)
        {
            if(session()->get('book'))
            {
                $response['data'] = route('web.reservation');
                session()->forget('book');
            }
        }
        return ResponseService::response($response);
    }

    public function logout()
    {
        $user = Auth::user();
        Auth::logout($user);
        return redirect(route('web.home'));
    }

    public function profile(Request $request)
    {
        $title = __('web.profile');
        $user = Auth::user();
        $request->merge(['user_id'=>$user->id]);
        $orders = $this->orderRepository->get($request);
        $reservations = $this->reservationRepository->get($request);
        return view($this->view.'profile', get_defined_vars());
    }

    public function orderDetails(Request $request, $id)
    {
        $user = Auth::user();
        $order = Order::where('id',$id)->where('user_id',$user->id)->firstOrFail();
        $order = $this->orderRepository->getItem($order);
        return view($this->view.'order_details', get_defined_vars());
    }
}
