<?php

namespace modules\subusers\classes;

use Exception;
use modules\subusers\interfaces\subusers_user_i;
use modules\subusers\traits\subusers_user_t;
use objects\subusers_o;

class subusers_user implements subusers_user_i
{
    use subusers_user_t;
    public int $subuserId;

    /**
     * Get a subuser by their ID.
     * @param int $subuserId
     * @return subusers_user|null
     */
    public static function bySubuserId(int $subuserId): ?subusers_user
    {
        $subuser = new subusers_user();
        $subuser->subuserId = $subuserId;
        return $subuser;
    }

    /**
     * Get a subuser by their username.
     * @param string $username
     * @return subusers_user
     * @throws Exception If there was an error getting the subuser by username or if the subuser does not exist
     */
    public static function byUsername(string $username): subusers_user
    {
        return self::bySubuserId((new subusers_o())->getSubuserByUsername($username)->id);
    }

    /**
     * Get a subuser by their session token.
     * @param string $token
     * @return subusers_user|null
     * @throws Exception If there was an error getting the subuser by session token
     */
    public static function bySessionToken(string $token): ?subusers_user
    {
        $subusers_o = new subusers_o();
        $subusers_o->getSubuserBySessionToken($token);
        return self::bySubuserId($subusers_o->id);
    }
}