<?php

namespace App\Modules\Auth\Models;

use App\Modules\Auth\Entities\Role;
use CodeIgniter\Model;

/**
 * Role Model
 */
class RoleModel extends Model
{
    protected $table            = 'roles';
    protected $primaryKey       = 'id';
    protected $useAutoIncrement = true;
    protected $returnType       = Role::class;
    protected $useSoftDeletes   = false;
    protected $protectFields    = true;
    protected $allowedFields    = [
        'role_code',
        'role_name',
    ];

    protected $useTimestamps = false;

    protected $validationRules = [
        'role_code' => 'required|max_length[50]|is_unique[roles.role_code,id,{id}]',
        'role_name' => 'required|max_length[100]',
    ];

    protected $validationMessages = [];
    protected $skipValidation     = false;
    protected $cleanValidationRules = true;

    public function findByCode(string $code): ?Role
    {
        return $this->where('role_code', $code)->first();
    }
}
