<?php
defined("ABSPATH") || exit;
/**
 * Ticker Base — shared config for ticker-tape (scrolling) and ticker (static)
 * Same pattern as market_movers.php
 *
 * Usage:
 *   return vunelix_build_ticker_widget('ticker-tape', 'scrolling');
 *   return vunelix_build_ticker_widget('ticker', 'static');
 */

function vunelix_build_ticker_widget(string $slug, string $mode): array {
    $vunelix_shared = require __DIR__ . '/../_shared_fields.php';

    $isScrolling = ($mode === 'scrolling');

    return [
        'slug'      => $slug,
        'component' => 'vunelix-' . $slug,
        'js'        => 'widgets/ticker/vunelix-' . $slug . '.js',

        // View — just a container div, JS builds the UI
        'view' => 'Widgets/Views/widgets/marquee_ticker',

        // SVG icons needed
        'icons' => [],

        // Branding
        'branding_position' => 'bottom-right',

        // Max symbols per plan
        'limits' => [
            'symbols' => [1 => 10, 2 => 15, 3 => 30],
        ],

        // Default values
        'defaults' => array_merge([
            'symbols'          => 'BINANCE:BTCUSD,BINANCE:ETHUSD,BINANCE:BNBUSD,BINANCE:XRPUSD,BINANCE:SOLUSD',

            // Display
            'show_symbol'      => true,
            'show_exchange'    => false,
            'show_flag'        => true,
            'show_full_name'   => true,
            'show_metric_label'=> true,
            'metrics'          => 'active.c,active.ch,active.chp',
            'time_period'      => '1d',

            // Animation — only for scrolling
            ...($isScrolling ? [
                'speed'          => 30,
                'direction'      => 'auto',
                'pause_on_hover' => true,
            ] : []),

            // Stop marquee — hardcoded per mode
            'stop_marquee'     => !$isScrolling,

            'animation_mode' => 'digits',

            // UI
            'ui_template'      => $isScrolling ? 'inline' : 'ticker-block',
            'card_style'       => $isScrolling ? 'bordered' : 'card',
            'item_spacing'     => 10,
            'show_border'      => $isScrolling,

        ], $vunelix_shared['defaults']['links'], $vunelix_shared['defaults']['live_data']),

        // Config map
        'config_map' => array_merge([
            'symbols'           => 'symbols',
            'show_symbol'       => 'showSymbol',
            'show_exchange'     => 'showExchange',
            'show_flag'         => 'showFlag',
            'show_full_name'    => 'showFullName',
            'show_metric_label' => 'showMetricLabel',
            'metrics'           => 'metrics',
            'time_period'       => 'timePeriod',
            'speed'             => 'speed',
            'direction'         => 'direction',
            'pause_on_hover'    => 'pauseOnHover',
            'stop_marquee'      => 'stopMarquee',
            'ui_template'       => 'uiTemplate',
            'card_style'        => 'cardStyle',
            'item_spacing'      => 'itemSpacing',
            'show_border'       => 'showBorder',
        ], $vunelix_shared['config_map']['links'], $vunelix_shared['config_map']['live_data']),

        // Settings groups
        'settings' => [
            'data' => [
                'label' => 'Data',
                'pkg'   => 1,
                'fields' => [
                    'symbols' => [
                        'type'        => 'symbol-picker',
                        'full_width'  => true,
                        'label'       => 'Symbols',
                        'description' => 'Search and select symbols. Drag to reorder.',
                        'default'     => 'BINANCE:BTCUSD,BINANCE:ETHUSD,BINANCE:BNBUSD,BINANCE:XRPUSD,BINANCE:SOLUSD',
                        'key'         => 'symbols',
                        'pkg'         => 1,
                        'multiple'    => true,
                    ],
                    'time_period' => [
                        'type'        => 'select',
                        'label'       => 'Time Period',
                        'description' => 'Select a time range to show data for (e.g. 1 Day = today\'s prices and changes).',
                        'default'     => '1d',
                        'key'         => 'time_period',
                        'pkg'         => 1,
                        'options'     => vunelix_time_period_list(),
                    ],
                ],
            ],
            'display' => [
                'label' => 'Display',
                'pkg'   => 1,
                'fields' => [
                    'show_symbol' => [
                        'type'        => 'toggle',
                        'label'       => 'Show Symbol',
                        'description' => 'Display the ticker symbol (e.g. BTCUSD, EURUSD).',
                        'default'     => true,
                        'key'         => 'show_symbol',
                        'pkg'         => 1,
                    ],
                    'show_full_name' => [
                        'type'        => 'toggle',
                        'label'       => 'Show Full Name',
                        'description' => 'Display the full asset name (e.g. Bitcoin / US Dollar).',
                        'default'     => true,
                        'key'         => 'show_full_name',
                        'pkg'         => 1,
                    ],
                    'show_exchange' => [
                        'type'        => 'toggle',
                        'label'       => 'Show Exchange',
                        'description' => 'Display the exchange name (e.g. BINANCE, NASDAQ).',
                        'default'     => false,
                        'key'         => 'show_exchange',
                        'pkg'         => 1,
                    ],
                    'show_flag' => [
                        'type'        => 'toggle',
                        'label'       => 'Show Flag/Logo',
                        'description' => 'Display the currency flag or crypto logo.',
                        'default'     => true,
                        'key'         => 'show_flag',
                        'pkg'         => 1,
                    ],
                    'show_metric_label' => [
                        'type'        => 'toggle',
                        'label'       => 'Show Data Labels',
                        'description' => 'Show labels (Price, Change, etc.) inside each card. Only visible when Template is set to Ticker Block.',
                        'default'     => true,
                        'key'         => 'show_metric_label',
                        'pkg'         => 1,
                    ],
                    'metrics' => [
                        'type'       => 'sortable-select',
                        'full_width' => true,
                        'label'      => 'Data Fields',
                        'description' => 'Choose which values to show (e.g. Price, Change, Volume). Drag to reorder. Max 4, Inline layout shows max 3.',
                        'default'  => 'active.c,active.ch,active.chp',
                        'key'      => 'metrics',
                        'pkg'      => 1,
                        'max'      => 4,
                        'options'  => [
                            'active.c'   => 'Price',
                            'active.ch'  => 'Change',
                            'active.chp' => 'Change %',
                            'active.o'   => 'Open',
                            'active.h'   => 'High',
                            'active.l'   => 'Low',
                            'active.v'   => 'Volume',
                            'active.a'   => 'Ask',
                            'active.b'   => 'Bid',
                            'spread'     => 'Spread',
                        ],
                    ],
                ],
            ],
            'layout' => [
                'label' => 'Layout',
                'pkg'   => 1,
                'fields' => array_merge(
                    [
                        'ui_template' => [
                            'type'        => 'select',
                            'label'       => 'Template',
                            'description' => 'Inline shows compact single-line items, Ticker Block shows detailed cards.',
                            'default'     => $isScrolling ? 'inline' : 'ticker-block',
                            'key'         => 'ui_template',
                            'pkg'         => 1,
                            'options' => [
                                'inline'       => 'Inline (compact)',
                                'ticker-block' => 'Ticker Block (cards)',
                            ],
                        ],
                    ],
                    [
                        'card_style' => [
                            'type'        => 'select',
                            'label'       => 'Card Style',
                            'description' => 'Card shows a filled background with border. Bordered shows only a left accent line.',
                            'default'     => $isScrolling ? 'bordered' : 'card',
                            'key'         => 'card_style',
                            'pkg'         => 1,
                            'options' => [
                                'card'     => 'Card (background + border)',
                                'bordered' => 'Bordered (left border only)',
                            ],
                        ],
                        'item_spacing' => [
                            'type'        => 'number',
                            'label'       => 'Item Spacing',
                            'description' => 'Gap between items in pixels. Default is 10. Use 0 for no gap, or 20+ for more breathing room.',
                            'default'     => 10,
                            'key'         => 'item_spacing',
                            'pkg'         => 1,
                        ],
                        'show_border' => [
                            'type'        => 'toggle',
                            'label'       => 'Show Border',
                            'description' => 'Show a border around the entire widget container.',
                            'default'     => $isScrolling,
                            'key'         => 'show_border',
                            'pkg'         => 1,
                        ],
                    ]
                ),
            ],
            ...($isScrolling ? [
                'animation' => [
                    'label' => 'Animation',
                    'pkg'   => 1,
                    'fields' => [
                        'speed' => [
                            'type'        => 'number',
                            'label'       => 'Scroll Speed',
                            'description' => 'How fast the ticker scrolls in pixels per second. Higher value = faster. Try 20 for slow, 30 for normal, 60 for fast.',
                            'default'     => 30,
                            'key'         => 'speed',
                            'pkg'         => 1,
                        ],
                        'direction' => [
                            'type'        => 'select',
                            'label'       => 'Direction',
                            'description' => 'Which direction the ticker scrolls. Auto picks the best direction based on your site language.',
                            'default'     => 'auto',
                            'key'         => 'direction',
                            'pkg'         => 1,
                            'options' => [
                                'auto' => 'Auto (based on page language)',
                                'rtl'  => 'Right to Left',
                                'ltr'  => 'Left to Right',
                            ],
                        ],
                        'pause_on_hover' => [
                            'type'        => 'toggle',
                            'label'       => 'Pause on Hover',
                            'description' => 'Pause scrolling when the user hovers over the ticker.',
                            'default' => true,
                            'key'     => 'pause_on_hover',
                            'pkg'     => 1,
                        ],
                    ],
                ],
            ] : []),
            'links'     => $vunelix_shared['groups']['links'],
            'live_data' => $vunelix_shared['groups']['live_data'],
        ],
    ];
}
