<?php

namespace App\Models;

use App\Domains\Auth\Models\Traits\Attribute\UserAttribute;
use App\Domains\Auth\Models\Traits\Method\UserMethod;
use App\Domains\Auth\Models\Traits\Relationship\UserRelationship;
use App\Domains\Auth\Models\Traits\Scope\UserScope;
use Illuminate\Auth\MustVerifyEmail as MustVerifyEmailTrait;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Lab404\Impersonate\Models\Impersonate;
use Spatie\Permission\Traits\HasRoles;

class User extends Authenticatable
{
    use HasFactory,
        HasRoles,
        Impersonate,
        MustVerifyEmailTrait,
        Notifiable,
        SoftDeletes,
        UserAttribute,
        UserMethod,
        UserRelationship,
        UserScope;

    public const TYPE_ADMIN = 'admin';
    public const TYPE_USER = 'user';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name',
        'email',
        'password',
        'username',
        'pronunciation_ja',
        'classification',
        'comment',
        'item01',
        'item02',
        'item03',
        'item04',
        'item05',
        'item06',
        'item07',
        'item08',
        'item09',
        'item10',
        'display_order',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    /**
     * @var array
     */
    protected $appends = [
        'is_admin',
        'new_role_admin_or_is_admin',
    ];

    /**
     * Check User is Admin
     *
     * @return bool
     */
    public function getIsAdminAttribute()
    {
        return $this->hasRole(config('boilerplate.access.role.admin'));
    }

    public function getNewRoleAdminOrIsAdminAttribute()
    {
        $roleAdmin = config('boilerplate.access.role.admin');

        if ($this->attributes['new_role']) {
            return $this->attributes['new_role'] == $roleAdmin ?? false;
        }

        return $this->hasRole($roleAdmin);
    }

    /**
     * Get the phone associated with the user.
     */
    public function faceImage()
    {
        return $this->hasOne(ImageFaceCapture::class);
    }

    public function checkinHistories()
    {
        return $this->hasMany(CheckinHistory::class);
    }
}
