<?php

namespace App\Models;

// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use Itstructure\LaRbac\Interfaces\RbacUserInterface;
use Itstructure\LaRbac\Traits\Administrable;
use Illuminate\Support\Facades\Validator;

class User extends Authenticatable implements RbacUserInterface
{
    use HasApiTokens, HasFactory, Notifiable, Administrable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'name',
        'email',
        'password',
        'user_type',
        'phone_number',
        'roles'
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array<int, string>
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
    public function hasRole($hasRole)
    {
        foreach ($this->roles()->get() as $role) {
            if($role->name == $hasRole){
                return true;
            }else{
                return false;
            }
        }
    }
    public function userData(){
        return $this->hasOne('App\Models\UserData', 'user_id');
    }
    public function userDocuments(){
        return $this->hasMany('App\Models\UserDocuments', 'user_id');
    }
    public function signupValidate($request){
        $validate = Validator::make($request->all(), [
            'email' => 'required|email',
            'first_name' => 'required',
            'last_name' => 'required',
            'phone_number' => 'required',
            'user_type' => 'required',
        ]);
        if($validate->fails()){
            return response()->json([
                'status' => false,
                'message' => 'Validation errors in your request',
                'errors' => $validate->errors()
            ], 400);
        }
        $postData = $request->all();
        $userPhone = User::with(['UserData'])->whereRelation('UserData', 'phone_number', '=', $postData['phone_number'])->first();
        if($userPhone){
            return response()->json([
                'status' => false,
                'message' => 'Validation errors in your request',
                'errors' => 'Phone number already exist'
            ], 400);
        }
        $userEmail = User::with(['UserData'])->where('email', '=', $postData['email'])->first();
        if($userEmail){
            return response()->json([
                'status' => false,
                'message' => 'Validation errors in your request',
                'errors' => 'Email already exist'
            ], 400);
        }
        return response()->json([
            'status' => true,
            'message' => 'sucess',
            'errors' => ''
        ], 200);
    }
}
