<?php

namespace App\Models;

use App\Http\Helpers\Hasher;
use Carbon\Carbon;

use Spatie\Permission\Models\Role as SpatieRole;
use Spatie\Permission\Models\Permission;

use Illuminate\Database\Eloquent\Model;

class Role extends Model
{
	protected $table = 'roles';

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

    protected $hidden = [
        'pivot'
    ];

    public function getAllPermissions()
    {
        return SpatieRole::find($this->id)->permissions;
    }

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

    public function getPermissionListAttribute()
    {
        $permission_list = $this->getAllPermissions();
        foreach ($permission_list as $key => $row) {
            $row->hash_id = Hasher::encode('permissions', $row->id);
        }
        return $permission_list;
    }

	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') : '';
    }
}
