<?php
    if (isset($block['data']['isExample']) && $block['data']['isExample']) {
        echo '<img src="' . esc_url( $block['data']['preview_image'] ) . '"style="max-width:100%; min-width:100%" alt="Preview" />';
        return;
    }

    global $rdgb;
    $rdgb = (!empty($rdgb)) ? $rdgb : Rdg_Blocks::instance();

    $block_styles = [];
    $block_name = BLOCK_RDG_TOP_CAPPERS_SPORT;
    $block_classes = [$block_name];
    $block_top_cappers_sport = $rdgb->settings->getBlock($block_name);
    $block_id = (isset($block['data']['custom_id']) && !empty($block['data']['custom_id'])) ? $block['data']['custom_id'] : $block['id'];

    $cache = new Rdg_Blocks_Cache();
    $connect = $cache->getRedisConnect();
    if (!$connect) {
        error_log(sprintf('%s / %s: Redis connection failed.', RDGBLOCKS_NAME, $block_name));
        return;
    }

    if (!empty($block['backgroundColor'])) {
        $block_classes[] = 'has-background';
        $block_classes[] = 'has-'.$block['backgroundColor'].'-background-color';
    }

    if (!empty($block['style']['color']['background'])) {
        $block_styles[] = 'background-color: '.$block['style']['color']['background'].';';
    }

    $primary_capper = 0;
    $titles = get_field('titles');
    $title = (!empty(get_field('title'))) ? get_field('title') : '';
    $cappers = (!empty(get_field('cappers'))) ? get_field('cappers') : [];
    $source = get_field('source');
    $theme = (!empty(get_field('theme'))) ? get_field('theme') : 'regular';
    $display_block = (!empty(get_field('display_block'))) ? get_field('display_block') : 'full-width';
    $position_block = $rdgb->helpers->getBlocksSpaces(get_fields());
    $sport = (!empty(get_field('sport'))) ? get_field('sport') : '';
    $league = (!empty(get_field('league'))) ? get_field('league') : '';
    $sports = (!empty(get_field('sports'))) ? get_field('sports') : $league; // Backward compatibility
    $days = (!empty(get_field('days'))) ? get_field('days') : 30;    
    $win_amount = (!empty(get_field('WinAmount'))) ? get_field('WinAmount') : 0;

    $cache_name = null;
    $api_attributes = null;
    $post_id = get_the_ID();
    $post_type = get_post_type($post_id);

    if ($source == 'api' && $block_top_cappers_sport->isCaching() && !empty($block_id)) {
        $cache_name = $rdgb->helpers->getBlocksCacheName($block_name, $post_type, $post_id, $block_id);
        $cache_block = $cache->getCacheBlock($cache_name);
        
        if ($cache_block) {
            $cache_block_array = $rdgb->helpers->isJson($cache_block);
            
            if ($cache_block_array && is_array($cache_block_array) && array_key_exists('api_response', $cache_block_array)) {
                if (is_array($cache_block_array['api_response']) && !empty($cache_block_array['api_response'])) {
                    $api_attributes = isset($cache_block_array['api_attributes']) ? $cache_block_array['api_attributes'] : null;
                    $cappers = $cache_block_array['api_response'];
                    $items = isset($cache_block_array['items']) ? $cache_block_array['items'] : 0;
                } else {
                    $cappers = [];
                }
            } else {
                $cappers = [];
            }
        }
    }
?>

<?php
// validate data to determine if there is data to render
$has_cappers_data = false;

if ($source == 'api') {
    // in API mode, verify expected data structure: $cappers['Sports'][$sport_name][...]
    if (is_array($cappers) && !empty($cappers)) {
        if (array_key_exists('Sports', $cappers) && is_array($cappers['Sports']) && !empty($cappers['Sports'])) {
            $sport_data = $cappers['Sports'];
            $sport_name = array_key_first($sport_data);
            if ($sport_name && array_key_exists($sport_name, $sport_data) && is_array($sport_data[$sport_name]) && !empty($sport_data[$sport_name])) {
                $has_cappers_data = true;
            }
        }
    }
} else {
    // in manual mode, verify that there are cappers
    if (is_array($cappers) && !empty($cappers)) {
        $has_cappers_data = true;
    }
}
?>

<?php if ($has_cappers_data): ?>
    <div class="rdg-blocks">
        <?php
            $params = ['cache_name' => $cache_name, 'api_request' => $api_attributes];
            $block_top_cappers_sport->getLogDebug($params);
        ?>
        <div class="<?php echo esc_attr(join(' ', $block_classes)); ?> <?php echo $display_block; ?>" style="<?php echo $position_block . esc_attr(join(' ', $block_styles)); ?>">
            <div class="container content grid-container">
                <?php require RDGBLOCKS_PLUGIN_BLOCKS_DIR.'partials/titles.php'; ?>
                <?php
                    if ($source == 'api') {
                        require 'partials/api.php';
                    } else {
                        require 'partials/manual.php';
                    }
                ?>
            </div>
        </div>
    </div>
<?php endif; ?>