<?php

// namespace App\Http\Controllers\Api\Auth;

// use App\Http\Controllers\Controller;
// use App\Http\Resources\UserResource;
// use App\Models\User;
// use Illuminate\Contracts\Validation\Rule;
// use Illuminate\Http\Request;


namespace App\Http\Controllers\API\Auth;

use App\Actions\Fortify\PasswordValidationRules;
use App\Http\Controllers\Controller;
use App\Http\Resources\UserResource;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\Rule;

class RegisterController extends Controller
{
    /**
     * Handle the incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function __invoke(Request $request)
    {
        $this->validate($request, [
            'name' => ['required', 'string', 'max:255'],
            'email' => [ 'required', 'string', 'email', 'max:255', Rule::unique(User::class),],
            'password' => ['required','min:8'],
        ]);

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

        $token = $user->createToken('myAppToken');

        DB::table("token_management_models")->insert([
            'access_token'  =>  $token->plainTextToken,
            'expired_at'   => date("Y-m-d"),
            'created_at'    =>  date("Y-m-d"),
        ]);
        return (new UserResource($user))->additional([
            'token' => $token->plainTextToken,
        ]);
    }
}
