<?php

declare(strict_types=1);

namespace Blax\WebRtc\Rooms;

/**
 * Room types, mirroring the laravel-websockets channel model and derived from the
 * room NAME prefix (so no extra config is needed):
 *
 *   public            anyone joins; no member list
 *   private-*         join gated by a RoomAuthorizer (host-provided)
 *   presence-*        private + the member list (with per-member info) is shared
 *   open-presence-*   public presence: anyone joins, everyone sees the members
 */
enum RoomType: string
{
    case Public = 'public';
    case Private = 'private';
    case Presence = 'presence';
    case OpenPresence = 'open-presence';

    public static function fromName(string $room): self
    {
        return match (true) {
            str_starts_with($room, 'open-presence-') => self::OpenPresence,
            str_starts_with($room, 'presence-') => self::Presence,
            str_starts_with($room, 'private-') => self::Private,
            default => self::Public,
        };
    }

    /** Private + presence rooms require the host to authorize the join. */
    public function requiresAuth(): bool
    {
        return $this === self::Private || $this === self::Presence;
    }

    /** Presence + open-presence rooms share the member list with the room. */
    public function hasPresence(): bool
    {
        return $this === self::Presence || $this === self::OpenPresence;
    }
}
