<?php

namespace App\Models;

use App\Http\Helpers\Hasher;
use App\Http\Helpers\ImageHandel;

use Laravel\Passport\HasApiTokens;
use Spatie\Permission\Traits\HasRoles;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Carbon\Carbon;

class Admin extends Authenticatable
{
    use HasApiTokens, HasRoles, Notifiable;

    protected $guard_name = 'admin';
    
    protected $table = 'admins';

    protected $fillable = [
        'name', 'email', 'password',
    ];

    protected $hidden = [
        'password', 'pivot'
    ];

    protected $appends = ['hash_id', 'role_list', 'permission_list'];

    public function getHashIdAttribute()
    {
        return Hasher::encode('admins', $this->id);
    }

    public function getRoleListAttribute()
    {
        $role_list = $this->roles;
        unset($this->roles);
        return $role_list;
    }

    public function getPermissionListAttribute()
    {
        $permission_list = $this->getAllPermissions()->pluck('name');
        unset($this->permissions);
        return $permission_list;
    }

    public function getImgAttribute($value)
    {
        $file_info = [];
        $file_info = ImageHandel::getImgFullPath($value, 'person');
        return $file_info;
    }

    public function getUpdatedAtAttribute($date) 
    {
        return $date ? Carbon::parse($date)->timezone('Asia/Kuala_Lumpur')->format('Y-m-d') : '';
    }

    public function getCreatedAtAttribute($date) 
    {
        return $date ? Carbon::parse($date)->timezone('Asia/Kuala_Lumpur')->format('Y-m-d') : '';
    }
}
