<?php
/**
 * Feature usage metrics (current usage, remaining, display formatting).
 *
 * Extracted from FeatureController to satisfy Sonar maintainability rules (S1448).
 *
 * @package FleekDash\Services
 */

namespace FleekDash\Services;

defined('ABSPATH') || exit();

if (!function_exists('fleekdash_log_debug')) {
    require_once FLEEKDASH_PLUGIN_DIR . 'includes/Core/debug-helpers.php';
}

class FeatureUsageMetricsService
{
    /**
     * Get current usage for a feature (app-specific implementation).
     *
     * @param string $feature_key
     * @param string $limit_key
     * @param string $app_name
     * @return int
     */
    public function getCurrentUsage($feature_key, $limit_key, $app_name)
    {
        $usage = 0;
        switch ($app_name) {
            case 'fleekdash':
                $usage = $this->getFleekdashUsage($feature_key, $limit_key);
                break;
            case 'app2':
            case 'app3':
                $usage = $this->getExternalAppUsage($app_name);
                break;
            default:
                $usage = 0;
                break;
        }

        return $usage;
    }

    /**
     * Get usage for FleekDash features.
     *
     * @param string $feature_key
     * @param string $limit_key
     * @return int
     */
    private function getFleekdashUsage($feature_key, $limit_key)
    {
        $usage = 0;
        switch ($feature_key) {
            case 'website_activation':
                if ($limit_key === 'max_websites') {
                    $usage = (int) \get_option('fleekdash_v2_activated_websites_count', 0);
                }
                break;

            case 'api_calls_per_hour':
                if ($limit_key === 'api_calls') {
                    $usage = (int) \get_option('fleekdash_v2_api_calls_last_hour', 0);
                }
                break;

            case 'storage_gb':
                if ($limit_key === 'storage') {
                    $usage = (int) \get_option('fleekdash_v2_storage_usage_gb', 0);
                }
                break;
            default:
                $usage = 0;
                break;
        }

        return $usage;
    }

    private function getExternalAppUsage(string $app_name): int
    {
        fleekdash_log_debug('FeatureUsageMetricsService: external app usage fallback for ' . $app_name);
        return 0;
    }

    /**
     * Calculate remaining usage.
     *
     * @param array $limit_data
     * @param int $current_usage
     * @return int|null
     */
    public function calculateRemaining($limit_data, $current_usage)
    {
        if ($limit_data['is_unlimited']) {
            return null;
        }

        $remaining = $limit_data['limit_value'] - $current_usage;
        return max(0, $remaining);
    }

    /**
     * Format display value for limits.
     *
     * @param array $limit_data
     * @return string
     */
    public function formatDisplayValue($limit_data)
    {
        if ($limit_data['is_unlimited']) {
            return 'Unlimited';
        }

        $value = $limit_data['limit_value'];
        $unit = $limit_data['limit_unit'];

        return "{$value} {$unit}";
    }
}
