<?php
/**
 * Per-configuration access policy.
 *
 * Centralizes the guest-access rule used by /bootstrap and re-validated by
 * every nonce-protected endpoint (chat, memory/*, profile/get), so a session
 * bootstrapped on one configuration cannot be replayed against another
 * configuration whose policy forbids guests.
 *
 * @package SlideMind\Core
 * @since   1.0.0
 */

namespace SlideMind\Core;

if (!defined('ABSPATH')) {
    exit;
}

class ConfigAccess {

    /**
     * Whether anonymous (guest) access is allowed for a configuration.
     *
     * Per-slide `_sm_allow_guests` meta (allow|deny|inherit) overrides the
     * global `slidemind_settings['allow_guests']` setting (default false).
     *
     * @param int $post_id Configuration post ID.
     * @return bool
     */
    public static function guest_allowed(int $post_id): bool {
        $slide_meta = get_post_meta($post_id, '_sm_allow_guests', true);
        if ($slide_meta === 'allow') {
            return true;
        }
        if ($slide_meta === 'deny') {
            return false;
        }
        // 'inherit' (or empty) → fall back to global.
        $settings = get_option('slidemind_settings', []);
        return !empty($settings['allow_guests']);
    }
}
