<?php
/**
 * Points Economy - Tier-Aware Version
 *
 * Gets point values from cached API response, with fallbacks to defaults.
 * Pro users get better rates than Free users.
 *
 * @package Consolety
 */

namespace Consolety;

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

class PointsEconomy {
    // Default values (Free tier)
    const DEFAULT_SUBMIT_COST = 30;
    const DEFAULT_HOST_REWARD = 15;
    const DEFAULT_DAILY_DRIP = 1;
    const DEFAULT_WELCOME_BONUS = 30;
    const DEFAULT_MONTHLY_PRO_BONUS = 60;
    const CONNECTION_COST_FREE = 5;
    const CONNECTION_COST_PRO = 0;

    // Pro tier values (submit cost now flat rate for both tiers)
    const PRO_SUBMIT_COST = 30;  // Was 20, now flat rate
    const PRO_HOST_REWARD = 20;
    const PRO_DAILY_DRIP = 2;
    const PRO_WELCOME_BONUS = 60;

    /**
     * Check if current site is Pro tier
     * First checks Freemius SDK, then falls back to cached hub tier
     *
     * SECURITY: Null-safe check for Freemius to prevent fatal errors
     * if SDK files are missing or incomplete.
     */
    public static function isPro(): bool {
        // Check Freemius SDK first (if available and properly initialized)
        // Wrapped in try/catch for \Throwable to handle corrupt SDK edge cases
        if (function_exists('consolety_is_freemius_pro')) {
            try {
                if (consolety_is_freemius_pro()) {
                    return true;
                }
            } catch (\Throwable $e) {
                // Freemius error (Exception or Error), fall through to hub tier check
            }
        }
        // Fallback to cached hub tier
        $siteInfo = get_option('consolety_site_info', []);
        $tier = $siteInfo['subscription_tier'] ?? $siteInfo['tier'] ?? 'free';
        return in_array(strtolower($tier), ['pro', 'plus', 'max', 'agency'], true);
    }

    /**
     * Get cost to submit a guest post (tier-aware)
     */
    public static function getSubmitCost(): int {
        return self::isPro() ? self::PRO_SUBMIT_COST : self::DEFAULT_SUBMIT_COST;
    }

    /**
     * Get reward for hosting a guest post (tier-aware)
     */
    public static function getHostReward(): int {
        return self::isPro() ? self::PRO_HOST_REWARD : self::DEFAULT_HOST_REWARD;
    }

    /**
     * Get daily drip amount (tier-aware)
     */
    public static function getDailyDrip(): int {
        return self::isPro() ? self::PRO_DAILY_DRIP : self::DEFAULT_DAILY_DRIP;
    }

    /**
     * Get welcome bonus amount (tier-aware)
     */
    public static function getWelcomeBonus(): int {
        return self::isPro() ? self::PRO_WELCOME_BONUS : self::DEFAULT_WELCOME_BONUS;
    }

    /**
     * Get current tier label
     */
    public static function getTierLabel(): string {
        return self::isPro() ? 'Pro' : 'Free';
    }

    /**
     * Get all economy values for display
     */
    public static function getEconomyInfo(): array {
        $isPro = self::isPro();
        return [
            'tier' => $isPro ? 'Pro' : 'Free',
            'submit_cost' => self::getSubmitCost(),
            'host_reward' => self::getHostReward(),
            'daily_drip' => self::getDailyDrip(),
            'welcome_bonus' => self::getWelcomeBonus(),
            // Show what Pro users get (for upgrade prompt)
            'pro_benefits' => [
                'submit_cost' => self::PRO_SUBMIT_COST,
                'host_reward' => self::PRO_HOST_REWARD,
                'daily_drip' => self::PRO_DAILY_DRIP,
            ],
        ];
    }

    /**
     * Format points for display
     */
    public static function format(int $amount, bool $showSign = false): string {
        $sign = $showSign && $amount >= 0 ? '+' : '';
        return sprintf('%s%s %s', $sign, number_format($amount),
            _n('point', 'points', abs($amount), 'consolety'));
    }
}
