<?php

namespace Modules\Auth\app\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Traits\IsAPI;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Modules\Auth\app\Models\User;

class RegisterController extends Controller
{
    use IsAPI;

    protected function respondWithToken()
    {
        return response()->json([
            'access_token' => User::getTokenByAuth(),
            'expires_in' => auth('api')->factory()->getTTL() * 60,
            'user' => auth()->user(),
            'status' => true
        ]);
    }

    public function register(Request $request)
    {
        // Validation logic
        $this->apiValidation($request, [
            'name' => 'required|string',
            'email' => 'required|email|unique:users',
            'password' => 'required|string|min:6',
        ]);

        // Create user
        $user = User::create([
            'name' => $request->name,
            'email' => $request->email,
            'password' => bcrypt($request->password),
        ]);

        $token = auth()->login($user);
        return $this->respondWithToken($token);

    }
}
