<?php

namespace App\Models;

use App\Core\Model;

class User extends Model
{
    protected static string $table = 'iw_users';

    /**
     * All users for admin listing, ordered by role then username.
     * Excludes password_hash from the result set.
     */
    public static function allForAdmin(): array
    {
        return static::db()->fetchAll(
            "SELECT id, username, email, role, is_active, created_at, updated_at
             FROM iw_users
             ORDER BY role ASC, username ASC"
        );
    }

    /**
     * Count active admin users.
     * Used for safety checks (prevent deleting or demoting the last admin).
     */
    public static function countAdmins(): int
    {
        return (int) static::db()->fetchColumn(
            "SELECT COUNT(*) FROM iw_users WHERE role = 'admin' AND is_active = 1"
        );
    }
}
