<?php

namespace FleekDash\Realtime;

defined('ABSPATH') || exit;

class RealtimeConfig
{
    public const DEFAULT_MAX_SUBSCRIPTIONS = 20;
    public const DEFAULT_MAX_PAYLOAD_BYTES = 16384;
    public const DEFAULT_RATE_LIMIT_PER_MINUTE = 120;

    public static function isEnabled(): bool
    {
        return (bool) \get_option('fleekdash_realtime_enabled', false);
    }

    public static function isKillSwitchEnabled(): bool
    {
        return (bool) \get_option('fleekdash_realtime_killswitch', false);
    }

    public static function getRolloutCohort(): string
    {
        $cohort = (string) \get_option('fleekdash_realtime_rollout_cohort', 'off');
        $allowed = ['off', 'permissions', 'notifications', 'full'];
        return in_array($cohort, $allowed, true) ? $cohort : 'off';
    }

    public static function getWsUrl(): string
    {
        return (string) \get_option('fleekdash_realtime_ws_url', '');
    }

    public static function getProvider(): string
    {
        $provider = (string) \get_option('fleekdash_realtime_provider', 'managed');
        return in_array($provider, ['managed', 'byo'], true) ? $provider : 'managed';
    }

    public static function getRelayEndpoint(): string
    {
        return (string) \get_option('fleekdash_realtime_publish_endpoint', '');
    }

    public static function getRelayToken(): string
    {
        return (string) \get_option('fleekdash_realtime_publish_token', '');
    }

    public static function isWsConfigured(): bool
    {
        return self::getWsUrl() !== '';
    }

    /**
     * Browser can use managed Supabase Realtime (anon key + project URL from plugin constants).
     */
    public static function isManagedSupabaseClientConfigured(): bool
    {
        $hasUrl = defined('FLEEKDASH_SUPABASE_URL') && (string) \FLEEKDASH_SUPABASE_URL !== '';
        $hasAnon = defined('FLEEKDASH_SUPABASE_KEY') && (string) \FLEEKDASH_SUPABASE_KEY !== '';

        return $hasUrl && $hasAnon;
    }

    /**
     * True when the SPA has enough configuration to open a realtime client (managed or BYO WebSocket).
     */
    public static function isClientTransportConfigured(): bool
    {
        $result = false;
        if (self::getProvider() === 'managed') {
            $result = self::isManagedSupabaseClientConfigured();
        } else {
            $result = self::isWsConfigured();
        }

        return $result;
    }

    public static function isRelayConfigured(): bool
    {
        return self::getRelayEndpoint() !== '' && self::getRelayToken() !== '';
    }

    public static function getTenantId(): string
    {
        $site = \site_url();
        return hash('sha256', $site);
    }

    public static function getMaxSubscriptionsPerClient(): int
    {
        $value = (int) \get_option('fleekdash_realtime_max_subscriptions', self::DEFAULT_MAX_SUBSCRIPTIONS);
        return max(1, min(100, $value));
    }

    public static function getMaxPayloadBytes(): int
    {
        $value = (int) \get_option('fleekdash_realtime_max_payload_bytes', self::DEFAULT_MAX_PAYLOAD_BYTES);
        return max(1024, min(65536, $value));
    }

    public static function getRateLimitPerMinute(): int
    {
        $value = (int) \get_option('fleekdash_realtime_publish_rate_limit_per_minute', self::DEFAULT_RATE_LIMIT_PER_MINUTE);
        return max(1, min(5000, $value));
    }
}

