<?php

namespace App\Http\Controllers\Api\Auth;

use App\Http\Controllers\Controller;
use App\Models\User;
use App\Transformers\Users\UserTransformer;
use Illuminate\Auth\Events\Registered;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class RegisterController extends Controller {
    protected User $model;

    public function __construct(User $model) {
        $this->model = $model;
    }

    public function checkEmail(Request $request): JsonResponse {
    	$this->validate($request, [
    		'email' => 'required|email|unique:users,email'
		]);
		return response()->json(null, 200);
	}

    public function store(Request $request): JsonResponse {
        $this->validate($request, [
            'name' => 'required',
            'email' => 'required|email|unique:users,email',
            'password' => 'required|min:8|confirmed',
        ]);
        $user = $this->model->create($request->all());
        $user->assignRole('User');
        event(new Registered($user));

        return fractal($user, new UserTransformer())->respond(201);
    }

}
