<?php

namespace App\Core\Auth\Models;

use CodeIgniter\Model;

class RoleModel extends Model
{
    protected $table = 'core_roles';
    protected $primaryKey = 'id';
    protected $returnType = 'array';
    protected $useTimestamps = true;
    protected $allowedFields = ['name', 'label'];

    public function permissions(int $roleId): array
    {
        return $this->db->table('core_permissions p')
            ->select('p.*')
            ->join('core_role_permissions rp', 'rp.permission_id = p.id')
            ->where('rp.role_id', $roleId)
            ->orderBy('p.group_name', 'ASC')
            ->orderBy('p.name', 'ASC')
            ->get()
            ->getResultArray();
    }
}
