<?php
declare(strict_types=1);

namespace App\Models;

use Core\DB;
use Core\Model;

final class Role extends Model
{
    protected string $table = 'roles';
    protected string $primaryKey = 'role_id';

    public const ADMIN            = 'admin';
    public const EXECUTIVE        = 'executive';
    public const PROJECT_MANAGER  = 'project_manager';
    public const FIELD_TECHNICIAN = 'field_technician';
    public const CLIENT           = 'client';

    public static function findByCode(string $code): ?array
    {
        return DB::one('SELECT * FROM roles WHERE code = :c', ['c' => $code]);
    }

    /**
     * Static list of all roles. Named `listAll` (not `all`) because parent
     * Core\Model::all() is an instance method — PHP forbids overriding it
     * with a static of the same name.
     */
    public static function listAll(string $orderBy = 'name'): array
    {
        return DB::all("SELECT * FROM roles ORDER BY {$orderBy}");
    }
}
