<?php

declare(strict_types=1);

namespace App\Model;

use Hyperf\Database\Model\Relations\BelongsToMany;
use Hyperf\Database\Model\Relations\HasMany;
use Hyperf\DbConnection\Model\Model;

/**
 * @property int $id
 * @property string $name
 * @property string $label
 * @property string $description
 * @property \Carbon\Carbon $created_at
 * @property \Carbon\Carbon $updated_at
 */
class Role extends Model
{
    protected ?string $table = 'roles';

    protected array $fillable = [
        'name',
        'label',
        'description',
    ];

    protected array $casts = [
        'id' => 'integer',
        'created_at' => 'datetime',
        'updated_at' => 'datetime',
    ];

    /**
     * 角色下的用户
     */
    public function users(): HasMany
    {
        return $this->hasMany(User::class, 'role_id');
    }

    /**
     * 角色授权的路由分组
     */
    public function routeGroups(): BelongsToMany
    {
        return $this->belongsToMany(RouteGroup::class, 'role_route_groups', 'role_id', 'group_id');
    }

    /**
     * 角色的路由覆盖规则
     */
    public function routeOverrides(): HasMany
    {
        return $this->hasMany(RoleRouteOverride::class, 'role_id');
    }
}
