<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Models\System\Verification;
use App\Models\User;
use App\Rules\PhoneCodeVerification;
use Illuminate\Auth\Events\Registered;
use Illuminate\Contracts\View\View;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Laravel\Nova\Nova;

class RegisterController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Register Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users as well as their
    | validation and creation. By default this controller uses a trait to
    | provide this functionality without requiring any additional code.
    |
    */

    /**
     * Show registration page
     */
    public function showNovaRegisterpageForm(): View
    {
        return view('vendor.nova.pages.register');
    }

    /**
     * Handle a registration request for the application.
     *
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
     */
    public function register(Request $request)
    {
        $this->validator($request->all())->validate();

        event(new Registered($user = $this->create($request->all())));

        Auth::guard()->login($user);

        sendSMSVerification($user->phone);

        return to_route('sms-verification');
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        $data['phone'] = unMaskPhone($data['phone'] ?? '');

        return Validator::make($data, [
            'name' => ['required', 'string', 'max:255'],
            'phone' => ['required', 'integer', 'between:61000000,671999999', 'unique:users,phone'],
            'username' => ['required', 'string', 'alpha_dash:ascii', 'max:255', 'unique:users,username'],
            'password' => ['required', 'string', 'min:8', 'confirmed'],
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @return \App\Models\User
     */
    protected function create(array $data)
    {
        $user = User::create([
            'name' => $data['name'],
            'phone' => unMaskPhone($data['phone']),
            'username' => $data['username'],
            'password' => Hash::make($data['password']),
            'active' => true,
        ]);

        return $user;
    }

    /**
     * Sms verification
     */
    public function smsVerification()
    {
        return view('vendor.nova.pages.sms-verification');
    }

    /**
     * Verify sms code
     */
    public function verifySmsCode(Request $request)
    {
        $request->validate([
            'code' => ['required', 'integer', new PhoneCodeVerification(auth()->user()->phone)],
        ]);

        auth()->user()->update([
            'phone_verified_at' => now(),
        ]);

        return redirect(Nova::path());
    }
}
