<?php

declare(strict_types=1);

namespace App\Models;

use App\Enums\AdminRoleStatus;
use DBCore\Permission\Models\Role;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

class DberpAdminRole extends Role
{
    /**
     * @var list<string>
     */
    protected $fillable = [
        'name',
        'display_name',
        'guard_name',
        'description',
        'sort_order',
        'status',
        'is_system',
        'is_super_admin',
    ];

    /**
     * @return array<string, string>
     */
    protected function casts(): array
    {
        return [
            'sort_order' => 'integer',
            'status' => AdminRoleStatus::class,
            'is_system' => 'boolean',
            'is_super_admin' => 'boolean',
        ];
    }

    public function users(): BelongsToMany
    {
        return $this->belongsToMany(User::class, 'core_user_roles', 'role_id', 'user_id')
            ->withPivot(['user_type', 'guard_name'])
            ->withTimestamps()
            ->wherePivot('user_type', User::class);
    }

    public function isEnabled(): bool
    {
        return $this->status === AdminRoleStatus::Enabled;
    }

    public function isSystemPreset(): bool
    {
        return (bool) $this->is_system;
    }
}
