<?php
/**
 * Widget Configurations — Loader
 *
 * Provides stub functions for site dependencies, then loads the exact same
 * helper files used on vunelix.com to build widget settings.
 *
 * Helper files are direct copies from:
 *   App/Modules/Widgets/Helpers/widgets/
 */

defined('ABSPATH') || exit;

// ================================================================
// STUBS — functions the helper files expect from the site framework
// ================================================================

if (!defined('VUNELIX_AVAILABLE_LANGS')) {
    define('VUNELIX_AVAILABLE_LANGS', [
        'en' => ['hreflang' => 'en', 'name' => 'English'],
        'ar' => ['hreflang' => 'ar', 'name' => 'العربية'],
        'es' => ['hreflang' => 'es', 'name' => 'Español'],
        'cn' => ['hreflang' => 'zh', 'name' => '中文'],
        'ru' => ['hreflang' => 'ru', 'name' => 'Русский'],
        'de' => ['hreflang' => 'de', 'name' => 'Deutsch'],
        'fr' => ['hreflang' => 'fr', 'name' => 'Français'],
        'ja' => ['hreflang' => 'ja', 'name' => '日本語'],
    ]);
}

if (!defined('VUNELIX_SHOW_PREMIUM_SETTINGS')) {
    define('VUNELIX_SHOW_PREMIUM_SETTINGS', false);
}

if (!function_exists('vunelix_time_period_list')) {
    function vunelix_time_period_list() {
        return [
            '1' => '1 Minute', '5' => '5 Minutes', '15' => '15 Minutes',
            '30' => '30 Minutes', '60' => '1 Hour', '120' => '2 Hours',
            '240' => '4 Hours', '1d' => '1 Day', '1w' => '1 Week', '1month' => '1 Month',
        ];
    }
}

if (!function_exists('vunelix_load_lang')) {
    function vunelix_load_lang($key = '') {
        // Plugin doesn't need server-side translations — return countries for selects
        if ($key === 'common') {
            return [
                // Exact copy from App/Languages/en/common.php
                'countries' => [
                    'US' => 'United States', 'GB' => 'United Kingdom', 'CN' => 'China', 'JP' => 'Japan',
                    'DE' => 'Germany', 'IN' => 'India', 'FR' => 'France', 'CA' => 'Canada', 'AU' => 'Australia',
                    'KR' => 'South Korea', 'IT' => 'Italy', 'BR' => 'Brazil', 'ES' => 'Spain', 'NL' => 'Netherlands',
                    'CH' => 'Switzerland', 'SE' => 'Sweden', 'SG' => 'Singapore', 'HK' => 'Hong Kong',
                    'AE' => 'United Arab Emirates', 'SA' => 'Saudi Arabia', 'PK' => 'Pakistan', 'AR' => 'Argentina',
                    'AT' => 'Austria', 'BD' => 'Bangladesh', 'BE' => 'Belgium', 'CO' => 'Colombia',
                    'CY' => 'Cyprus', 'CZ' => 'Czech Republic', 'DK' => 'Denmark', 'EG' => 'Egypt',
                    'FI' => 'Finland', 'HU' => 'Hungary', 'IE' => 'Ireland', 'IL' => 'Israel',
                    'LK' => 'Sri Lanka', 'MX' => 'Mexico', 'MY' => 'Malaysia', 'NG' => 'Nigeria',
                    'NO' => 'Norway', 'PL' => 'Poland', 'PT' => 'Portugal', 'QA' => 'Qatar',
                    'RU' => 'Russia', 'TR' => 'Turkey', 'TW' => 'Taiwan', 'ZA' => 'South Africa',
                ],
            ];
        }
        return [];
    }
}

if (!function_exists('vunelix_get_heatmap_options')) {
    function vunelix_get_heatmap_options(string $heatmapType = 'crypto', array $hmLang = []): array {
        $typeOptions = [
            ['value' => 'coin', 'label' => 'Crypto Coins', 'selected' => true, 'params' => ['type' => 'coin']],
            ['value' => 'coin_no_btc', 'label' => 'Crypto Coins (Excluding Bitcoin)', 'params' => ['type' => 'coin']],
            ['value' => 'coin_no_stablecoins', 'label' => 'Crypto Coins (Excluding Stablecoins)', 'params' => ['type' => 'coin']],
            ['value' => 'defi', 'label' => 'Coins DeFi', 'params' => ['type' => 'coin']],
            ['value' => 'memes', 'label' => 'Coins Memes', 'params' => ['type' => 'coin']],
            ['value' => 'gaming', 'label' => 'Coins Gaming', 'params' => ['type' => 'coin']],
            ['value' => 'stablecoins', 'label' => 'Stablecoins', 'params' => ['type' => 'coin']],
        ];
        $sortOptions = [
            'crypto' => [
                ['value' => 'market_cap', 'label' => 'Market Cap', 'selected' => true, 'params' => ['columns' => 'perf.market_cap', 'sort_by' => 'perf.market_cap_desc']],
                ['value' => '24h_vol', 'label' => '24h Volume', 'params' => ['columns' => 'perf.24h_vol_cmc', 'sort_by' => 'perf.24h_vol_cmc_desc']],
                ['value' => 'txs_vol', 'label' => 'Transaction Vol', 'params' => ['columns' => 'perf.txs_volume_usd', 'sort_by' => 'perf.txs_volume_usd_desc']],
                ['value' => 'mono', 'label' => 'Mono Size', 'params' => ['columns' => 'perf.market_cap', 'sort_by' => 'perf.market_cap_desc']],
            ],
            'stock' => [
                ['value' => 'market_cap', 'label' => 'Market Cap', 'selected' => true, 'params' => ['columns' => 'earnings.market_cap', 'sort_by' => 'earnings.market_cap_desc', 'period' => '1d']],
                ['value' => 'volume_d', 'label' => 'Volume 1D', 'params' => ['columns' => 'active.v', 'sort_by' => 'active.v_desc', 'period' => '1d']],
                ['value' => 'volume_w', 'label' => 'Volume 1W', 'params' => ['columns' => 'active.v', 'sort_by' => 'active.v_desc', 'period' => '1w']],
                ['value' => 'volume_m', 'label' => 'Volume 1M', 'params' => ['columns' => 'active.v', 'sort_by' => 'active.v_desc', 'period' => 'month']],
                ['value' => 'mono', 'label' => 'Mono Size', 'params' => ['columns' => 'earnings.market_cap', 'sort_by' => 'earnings.market_cap_desc']],
            ],
        ];
        $colorOptions = [
            'crypto' => [
                ['label' => 'Change 30m, %', 'params' => ['columns' => 'active.chp', 'period' => '30m']],
                ['label' => 'Change 1h, %', 'params' => ['columns' => 'active.chp', 'period' => '1h']],
                ['label' => 'Change 4h, %', 'params' => ['columns' => 'active.chp', 'period' => '4h']],
                ['label' => 'Change 1D, %', 'selected' => true, 'params' => ['columns' => 'active.chp', 'period' => '1d']],
                ['label' => 'Performance 1W, %', 'params' => ['columns' => 'perf.perf_1w', 'period' => '1d']],
                ['label' => 'Performance 1M, %', 'params' => ['columns' => 'perf.perf_1m', 'period' => '1d']],
                ['label' => 'Performance 3M, %', 'params' => ['columns' => 'perf.perf_3m', 'period' => '1d']],
                ['label' => 'Performance 6M, %', 'params' => ['columns' => 'perf.perf_6m', 'period' => '1d']],
                ['label' => 'Performance YTD, %', 'params' => ['columns' => 'perf.perf_ytd', 'period' => '1d']],
                ['label' => 'Performance Y, %', 'params' => ['columns' => 'perf.perf_1y', 'period' => '1d']],
            ],
            'stock' => [
                ['label' => 'Change 30m, %', 'params' => ['columns' => 'active.chp', 'period' => '30m']],
                ['label' => 'Change 1h, %', 'params' => ['columns' => 'active.chp', 'period' => '1h']],
                ['label' => 'Change 4h, %', 'params' => ['columns' => 'active.chp', 'period' => '4h']],
                ['label' => 'Change 1D, %', 'selected' => true, 'params' => ['columns' => 'active.chp', 'period' => '1d']],
                ['label' => 'Performance 1W, %', 'params' => ['columns' => 'perf.perf_1w', 'period' => '1d']],
                ['label' => 'Performance 1M, %', 'params' => ['columns' => 'perf.perf_1m', 'period' => '1d']],
                ['label' => 'Performance 3M, %', 'params' => ['columns' => 'perf.perf_3m', 'period' => '1d']],
                ['label' => 'Performance 6M, %', 'params' => ['columns' => 'perf.perf_6m', 'period' => '1d']],
                ['label' => 'Performance YTD, %', 'params' => ['columns' => 'perf.perf_ytd', 'period' => '1d']],
                ['label' => 'Performance Y, %', 'params' => ['columns' => 'perf.perf_1y', 'period' => '1d']],
            ],
        ];
        return [
            'type'    => $heatmapType === 'crypto' ? $typeOptions : [],
            'sort'    => $sortOptions[$heatmapType] ?? $sortOptions['crypto'],
            'color'   => $colorOptions[$heatmapType] ?? $colorOptions['crypto'],
            'perPage' => [],
        ];
    }
}




// ================================================================
// LOAD WIDGET CONFIGS — from copied helper files
// ================================================================

$vunelix_widgets_dir = __DIR__ . '/widgets/';

/**
 * Build widget list — each widget loaded from its own helper file (same as site)
 */
function vunelix_get_widgets() {
    $dir = dirname(__FILE__) . '/widgets/';

    // Load shared builders (each require_once's _shared_fields.php internally)
    require_once $dir . 'shared/ticker_base.php';
    require_once $dir . 'shared/market_movers.php';
    require_once $dir . 'shared/heatmap.php';
    require_once $dir . 'shared/screener_table.php';
    require_once $dir . 'shared/technical_builder.php';
    require_once $dir . 'shared/forex_rates.php';

    // ── Widget names/groups/descriptions (for WP admin UI) ──
    $widgetMeta = [
        'ticker-tape'          => ['name' => 'Ticker Tape',          'group' => 'Ticker',         'desc' => 'Scrolling price bar with live crypto, forex, and stock quotes.'],
        'ticker'               => ['name' => 'Ticker',              'group' => 'Ticker',         'desc' => 'Static price cards with live updates.'],
        'symbol-search'        => ['name' => 'Symbol Search',       'group' => 'Ticker',         'desc' => 'Search modal for stocks, crypto, forex — trigger from any element.'],
        'crypto-market-movers' => ['name' => 'Crypto Market Movers','group' => 'Market Movers',  'desc' => 'Trending coins ranked by price change — gainers and losers.'],
        'forex-market-movers'  => ['name' => 'Forex Market Movers', 'group' => 'Market Movers',  'desc' => 'Top forex pairs by price movement.'],
        'stock-market-movers'  => ['name' => 'Stock Market Movers', 'group' => 'Market Movers',  'desc' => 'Top stocks by price change.'],
        'currency-converter'   => ['name' => 'Currency Converter',  'group' => 'Currency Tools', 'desc' => 'Instant currency conversion with 150+ pairs.'],
        'currency-cross-rates' => ['name' => 'Currency Cross Rates','group' => 'Currency Tools', 'desc' => 'Live exchange rates matrix between currency pairs.'],
        'currency-heatmap'     => ['name' => 'Currency Heatmap',    'group' => 'Heatmaps',       'desc' => 'Color-coded forex strength grid across timeframes.'],
        'crypto-heatmap'       => ['name' => 'Crypto Heatmap',      'group' => 'Heatmaps',       'desc' => 'Market cap weighted crypto heatmap.'],
        'stock-heatmap'        => ['name' => 'Stock Heatmap',       'group' => 'Heatmaps',       'desc' => 'Stock market heatmap by sector and market cap.'],
        'crypto-screener'      => ['name' => 'Crypto Screener',     'group' => 'Screeners',      'desc' => 'Filter and sort cryptocurrencies.'],
        'stock-screener'       => ['name' => 'Stock Screener',      'group' => 'Screeners',      'desc' => 'Filter and sort stocks by metrics.'],
        'forex-screener'       => ['name' => 'Forex Screener',      'group' => 'Screeners',      'desc' => 'Filter and sort forex pairs.'],
        'fund-screener'        => ['name' => 'Fund Screener',       'group' => 'Screeners',      'desc' => 'Filter and sort mutual funds and ETFs.'],
        'cex-screener'         => ['name' => 'CEX Screener',        'group' => 'Screeners',      'desc' => 'Centralized exchange crypto pairs.'],
        'dex-screener'         => ['name' => 'DEX Screener',        'group' => 'Screeners',      'desc' => 'Decentralized exchange token pairs.'],
        'dr-screener'          => ['name' => 'DR Screener',         'group' => 'Screeners',      'desc' => 'Depositary receipts screener.'],
        'technical-analysis'   => ['name' => 'Technical Analysis',  'group' => 'Technical',      'desc' => 'Full technical analysis with oscillators, moving averages, and pivot points.'],
        'technical-summary'    => ['name' => 'Technical Summary',   'group' => 'Technical',      'desc' => 'Quick summary gauge with buy/sell signals.'],
        'oscillators'          => ['name' => 'Oscillators',         'group' => 'Technical',      'desc' => 'RSI, MACD, Stochastic, and more oscillator indicators.'],
        'moving-averages'      => ['name' => 'Moving Averages',     'group' => 'Technical',      'desc' => 'SMA, EMA, and other moving average indicators.'],
        'pivot-points'         => ['name' => 'Pivot Points',        'group' => 'Technical',      'desc' => 'Support and resistance pivot point levels.'],
    ];

    // ── Load each widget config from its helper file (same as site) ──
    $widgets = [];
    $widgetFiles = [
        'ticker/ticker_tape.php', 'ticker/ticker.php', 'symbol_search.php',
        'market_movers/crypto_market_movers.php', 'market_movers/forex_market_movers.php', 'market_movers/stock_market_movers.php',
        'currency_converter.php', 'currency_cross_rates.php',
        'heatmap/currency_heatmap.php', 'heatmap/crypto_heatmap.php', 'heatmap/stock_heatmap.php',
        'screener/crypto_screener.php', 'screener/stock_screener.php', 'screener/forex_screener.php',
        'screener/fund_screener.php', 'screener/cex_screener.php', 'screener/dex_screener.php', 'screener/dr_screener.php',
        'technical/technical_analysis.php', 'technical/technical_summary.php',
        'technical/oscillators.php', 'technical/moving_averages.php', 'technical/pivot_points.php',
    ];

    foreach ($widgetFiles as $file) {
        $config = require $dir . $file;
        $slug = $config['slug'] ?? '';
        if (!$slug || !isset($widgetMeta[$slug])) continue;

        $meta = $widgetMeta[$slug];
        $widgets[$slug] = [
            'name'      => $meta['name'],
            'group'     => $meta['group'],
            'desc'      => $meta['desc'],
            'js'        => $config['js'] ?? '',
            'component' => $config['component'] ?? '',
            'settings'  => $config['settings'] ?? [],
            'limits'    => $config['limits'] ?? [],
        ];
    }

    return $widgets;
}


/**
 * Common fields — appearance + colors (appended to every widget)
 */
function vunelix_common_fields() {
    $common = require __DIR__ . '/widgets/_common_fields.php';
    return $common['settings'] ?? [];
}


/**
 * Widget groups for admin UI sidebar/navigation
 */
function vunelix_get_widget_groups() {
    return [
        'Ticker'        => ['ticker-tape', 'ticker', 'symbol-search'],
        'Market Movers' => ['crypto-market-movers', 'forex-market-movers', 'stock-market-movers'],
        'Currency Tools' => ['currency-converter', 'currency-cross-rates'],
        'Heatmaps'      => ['currency-heatmap', 'crypto-heatmap', 'stock-heatmap'],
        'Screeners'     => ['crypto-screener', 'stock-screener', 'forex-screener', 'fund-screener', 'cex-screener', 'dex-screener', 'dr-screener'],
        'Technical'     => ['technical-analysis', 'technical-summary', 'oscillators', 'moving-averages', 'pivot-points'],
    ];
}
