<?php

namespace App\Http\Controllers\Shop\Auth;

use App\Http\Controllers\Controller;
use App\Repositories\Shop\CustomerAddresses;
use App\Repositories\Shop\Customers;
use Illuminate\Auth\Events\Registered;
use Illuminate\Foundation\Auth\EmailVerificationRequest;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class RegisterController extends Controller
{
    // use RegistersUsers;

    protected $redirectTo;

    protected function guard()
    {
        return Auth::guard('customer');
    }

    protected function redirectTo()
    {
        return route(config('boilerplate.app.redirectTo', 'boilerplate.dashboard'));
    }

    public function showRegistrationForm()
    {
        return view('Shop.auth.register');
    }

    public function register(Request $request)
    {
        dump('ici');
        exit;
        // event(new Registered($user = $this->create($request->all())));
        $user = $this->create($request->all());
        dump($user);
        exit;

        $this->guard()->login($user);

        if ($response = $this->registered($request, $user)) {
            return $response;
        }

        return $request->wantsJson()
                    ? new JsonResponse([], 201)
                    : redirect($this->redirectPath());
    }

    protected function create(array $data)
    {
        $user = Customers::create($data);
        CustomerAddresses::add($user->id, $data);

        return $user;
    }

    public function emailVerify()
    {
        if (Auth::user()->hasVerifiedEmail()) {
            return redirect(route(config('boilerplate.app.redirectTo', 'boilerplate.dashboard')));
        }

        return view('boilerplate::auth.verify-email');
    }

    public function emailVerifyRequest(EmailVerificationRequest $request)
    {
        $request->fulfill();

        return redirect(route(config('boilerplate.app.redirectTo', 'boilerplate.dashboard')));
    }

    public function emailSendVerification(Request $request)
    {
        $request->user()->sendEmailVerificationNotification();

        return back()->with('message', 'Verification link sent!');
    }
}
