<?php
/**
 * Dashboard page for AnWP Sports Leagues
 *
 * @link       https://anwp.pro
 * @since      0.11.0
 * @since      0.14.1 Redesigned as Dashboard with stats and changelog.
 *
 * @package    Sports_Leagues
 * @subpackage Sports_Leagues/admin/views
 */

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

if ( ! current_user_can( 'manage_options' ) ) {
	wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'sports-leagues' ) );
}

/*
|--------------------------------------------------------------------------
| Debug Mode - Show Banners for Testing
|--------------------------------------------------------------------------
|
| Uncomment ONE of the lines below to force-show banners for visual testing.
|
| Options:
|   true           - Show ALL banners at once
|   'welcome'      - Welcome Banner (normally: new users with 0 games)
|   'api_import'   - API Import Banner (normally: premium + no API key)
|   'premium_promo'- Premium Promotion Banner (normally: free users only)
|   'support'      - Premium Support Ticket button (normally: premium only)
|
| Examples:
|   define( 'ANWPSL_DEBUG_BANNERS', true );           // All banners
|   define( 'ANWPSL_DEBUG_BANNERS', 'welcome' );      // Only welcome
|   define( 'ANWPSL_DEBUG_BANNERS', 'premium_promo' );// Only premium promo
|
| IMPORTANT: Re-comment after testing!
|
*/
// define( 'ANWPSL_DEBUG_BANNERS', true );

$debug_banners = defined( 'ANWPSL_DEBUG_BANNERS' ) ? ANWPSL_DEBUG_BANNERS : false;
$debug_all     = true === $debug_banners;
$debug_banner  = function( $name ) use ( $debug_banners, $debug_all ) {
	return $debug_all || $debug_banners === $name;
};

$data    = sports_leagues()->get_dashboard_data();
$counts  = $data['counts'];
$can_add = $data['can_add'];

// Sport configuration data.
$sports_available = Sports_Leagues_Config::get_sports_available();
$current_sport    = '';

if ( $data['has_sport_config'] && ! empty( $data['current_sport'] ) ) {
	foreach ( $sports_available as $sport ) {
		if ( $sport['slug'] === $data['current_sport'] ) {
			$current_sport = $sport;
			break;
		}
	}
}

// Player Statistics data.
$player_stats_raw = get_option( 'sl_columns_game', '[]' );
$player_stats     = json_decode( $player_stats_raw, true ) ?: [];
$stats_names      = array_slice( array_column( $player_stats, 'name' ), 0, 4 );

// Game Events data.
$events_data = Sports_Leagues_Event::get_value( 'all' );
$event_names = [];
if ( ! empty( $events_data['events'] ) ) {
	$event_names = array_slice( array_column( $events_data['events'], 'name' ), 0, 4 );
}

// Sport Config summary data.
$sport_config_all   = Sports_Leagues_Config::get_value( 'all' );
$config_summary     = [];
$config_has_more    = false;
$config_items_shown = 0;

if ( ! empty( $sport_config_all ) && is_array( $sport_config_all ) ) {
	// Player Positions
	if ( ! empty( $sport_config_all['position'] ) && is_array( $sport_config_all['position'] ) ) {
		$position_names         = array_column( $sport_config_all['position'], 'name' );
		$config_summary[]       = [
			'label' => __( 'Player Position', 'sports-leagues' ),
			'value' => implode( ', ', array_slice( $position_names, 0, 5 ) ) . ( count( $position_names ) > 5 ? '...' : '' ),
		];
		$config_items_shown++;
	}

	// Roster Status
	if ( ! empty( $sport_config_all['roster_status'] ) && is_array( $sport_config_all['roster_status'] ) && $config_items_shown < 3 ) {
		$config_summary[]       = [
			'label' => __( 'Player Roster Status', 'sports-leagues' ),
			'value' => implode( ', ', $sport_config_all['roster_status'] ),
		];
		$config_items_shown++;
	}

	// Game Player Groups
	if ( ! empty( $sport_config_all['game_player_groups'] ) && is_array( $sport_config_all['game_player_groups'] ) && $config_items_shown < 3 ) {
		$group_names            = array_column( $sport_config_all['game_player_groups'], 'name' );
		$config_summary[]       = [
			'label' => __( 'Player Game Groups', 'sports-leagues' ),
			'value' => implode( ', ', $group_names ),
		];
		$config_items_shown++;
	}

	// Check if there's more config data
	$config_keys = [ 'roster_groups', 'game_team_stats', 'num_of_periods', 'points_ft_win' ];
	foreach ( $config_keys as $key ) {
		if ( ! empty( $sport_config_all[ $key ] ) ) {
			$config_has_more = true;
			break;
		}
	}
}

// Entity definitions for Stats Grid
$entities = [
	'leagues'     => [
		'label' => __( 'Leagues', 'sports-leagues' ),
		'icon'  => 'dashicons-networking',
		'url'   => 'edit-tags.php?taxonomy=sl_league&post_type=sl_tournament',
	],
	'seasons'     => [
		'label' => __( 'Seasons', 'sports-leagues' ),
		'icon'  => 'dashicons-calendar-alt',
		'url'   => 'edit-tags.php?taxonomy=sl_season&post_type=sl_tournament',
	],
	'teams'       => [
		'label' => __( 'Teams', 'sports-leagues' ),
		'icon'  => 'dashicons-shield',
		'url'   => 'post-new.php?post_type=sl_team',
	],
	'players'     => [
		'label' => __( 'Players', 'sports-leagues' ),
		'icon'  => 'dashicons-groups',
		'url'   => 'post-new.php?post_type=sl_player',
	],
	'tournaments' => [
		'label'   => __( 'Tournaments', 'sports-leagues' ),
		'icon'    => 'dashicons-awards',
		'url'     => 'post-new.php?post_type=sl_tournament',
		'tooltip' => __( 'Create League and Season first', 'sports-leagues' ),
	],
	'games'       => [
		'label'   => __( 'Games', 'sports-leagues' ),
		'icon'    => 'dashicons-tickets',
		'url'     => 'post-new.php?post_type=sl_game',
		'tooltip' => __( 'Create Tournament and at least 2 Teams first', 'sports-leagues' ),
	],
	'standings'   => [
		'label'   => __( 'Standings', 'sports-leagues' ),
		'icon'    => 'dashicons-editor-ol',
		'url'     => 'post-new.php?post_type=sl_standing',
		'tooltip' => __( 'Create Tournament first', 'sports-leagues' ),
	],
	'venues'      => [
		'label' => __( 'Venues', 'sports-leagues' ),
		'icon'  => 'dashicons-location',
		'url'   => 'post-new.php?post_type=sl_venue',
	],
	'staff'       => [
		'label' => __( 'Staff', 'sports-leagues' ),
		'icon'  => 'dashicons-businessman',
		'url'   => 'post-new.php?post_type=sl_staff',
	],
	'officials'   => [
		'label' => __( 'Officials', 'sports-leagues' ),
		'icon'  => 'dashicons-id',
		'url'   => 'post-new.php?post_type=sl_official',
	],
];

// Parse changelogs
$core_changelog = sports_leagues()->parse_changelog( sports_leagues()->path . 'changelog.txt', 3 );

$premium_changelog = [];
if ( function_exists( 'sports_leagues_premium' ) ) {
	$premium_changelog = sports_leagues()->parse_changelog( sports_leagues_premium()->path . 'changelog.txt', 3 );
}

// Tutorials & Articles with badges: 'pro', 'core', 'new', 'updated'
$tutorials = [
	[
		'title'  => __( 'AI Writer', 'sports-leagues' ),
		'url'    => 'https://anwp.pro/docs/sports-leagues/6-premium-features/ai-writer/',
		'badges' => [ 'pro', 'updated' ],
	],
	[
		'title'  => __( 'Entity Links', 'sports-leagues' ),
		'url'    => 'https://anwp.pro/docs/sports-leagues/6-premium-features/entity-links/',
		'badges' => [ 'pro', 'new' ],
	],
	[
		'title'  => __( 'Shortcode Builder', 'sports-leagues' ),
		'url'    => 'https://anwp.pro/docs/sports-leagues/3-display-shortcodes/shortcode-builder/',
		'badges' => [ 'core', 'new' ],
	],
	[
		'title'  => __( 'NFL Live Scores Website', 'sports-leagues' ),
		'url'    => 'https://anwp.pro/how-to-create-nfl-live-scores-website-in-wordpress/',
		'badges' => [ 'pro' ],
	],
	[
		'title'  => __( 'NBA Live Scores Website', 'sports-leagues' ),
		'url'    => 'https://anwp.pro/ultimate-guide-to-building-nba-basketball-live-scores-stats-website-in-wordpress/',
		'badges' => [ 'pro' ],
	],
];
?>
<style>
.anwp-sl-premium-features a {
	text-decoration: none;
}
.anwp-sl-premium-features a:hover {
	text-decoration: underline;
}
</style>
<div class="anwp-b-wrap">
	<div class="anwp-p-4 anwp-w-max-1200">

		<!-- Header -->
		<h1 class="anwp-mb-4">
			<?php echo esc_html__( 'Sports Leagues', 'sports-leagues' ); ?> :: <?php echo esc_html__( 'Dashboard', 'sports-leagues' ); ?>
			<span class="anwp-text-sm anwp-text-gray-500 anwp-ml-2">v<?php echo esc_html( Sports_Leagues::VERSION ); ?></span>
		</h1>

		<!-- Resources -->
		<div class="anwp-mb-4 anwp-p-3 anwp-bg-white anwp-border anwp-border-gray-300 anwp-rounded">
			<div class="anwp-d-flex anwp-flex-wrap anwp-gap-2">
				<a href="https://anwp.pro/docs/sports-leagues/" class="button" target="_blank">📖 <?php echo esc_html__( 'Documentation', 'sports-leagues' ); ?></a>
				<a href="https://support.anwp.pro/communities/4-sports-leagues" class="button" target="_blank">📖 <?php echo esc_html__( 'Documentation (old)', 'sports-leagues' ); ?></a>
				<a href="https://anwp.pro/docs/sports-leagues/1-getting-started/" class="button" target="_blank">🚀 <?php echo esc_html__( 'Start Guide', 'sports-leagues' ); ?></a>
				<a href="https://support.anwp.pro/communities/4-sports-leagues" class="button" target="_blank">💬 <?php echo esc_html__( 'Community & Support Forum', 'sports-leagues' ); ?></a>
				<?php if ( function_exists( 'sports_leagues_premium' ) || $debug_banner( 'support' ) ) : ?>
				<a href="<?php echo esc_url( admin_url( 'admin.php?page=support' ) ); ?>" class="button button-primary">⭐ <?php echo esc_html__( 'Premium Support Ticket', 'sports-leagues' ); ?></a>
				<?php endif; ?>
			</div>
		</div>

		<!-- Welcome Banner (new users only) -->
		<?php if ( $data['is_new_user'] || $debug_banner( 'welcome' ) ) : ?>
		<div class="anwp-mb-4 anwp-p-4 anwp-bg-blue-50 anwp-border anwp-border-blue-500 anwp-rounded" style="border-left: 4px solid #2271b1;">
			<h3 class="anwp-mt-0 anwp-mb-2 anwp-font-semibold">
				👋 <?php echo esc_html__( 'Welcome to Sports Leagues!', 'sports-leagues' ); ?>
			</h3>
			<p class="anwp-mb-2"><?php echo esc_html__( 'Get started by creating your first League, Season, and Tournament.', 'sports-leagues' ); ?></p>
			<a href="https://anwp.pro/docs/sports-leagues/1-getting-started/" target="_blank" class="button button-primary">
				<?php echo esc_html__( 'Read the Start Guide', 'sports-leagues' ); ?> →
			</a>
		</div>
		<?php endif; ?>

		<!-- Sport Configuration Section -->
		<?php if ( $data['has_sport_config'] && ! empty( $current_sport ) ) : ?>
		<!-- Configured state: compact info bar -->
		<div class="anwp-mb-4 anwp-p-3 anwp-bg-white anwp-border anwp-border-gray-300 anwp-rounded">
			<div class="anwp-d-flex anwp-items-center anwp-justify-between anwp-flex-wrap anwp-gap-2">
				<div class="anwp-d-flex anwp-items-center anwp-gap-2">
					<span class="anwp-text-xl"><?php echo esc_html( $current_sport['icon'] ); ?></span>
					<span class="anwp-font-semibold">
						<?php
						/* translators: %s: sport name */
						printf( esc_html__( 'Sport: %s', 'sports-leagues' ), esc_html( $current_sport['title'] ) );
						?>
					</span>
				</div>
				<a href="<?php echo esc_url( admin_url( 'admin.php?page=sl-configurator' ) ); ?>" class="button">
					<?php echo esc_html__( 'Sport Configurator', 'sports-leagues' ); ?>
				</a>
			</div>
		</div>
		<?php else : ?>
		<!-- Unconfigured state: sport selector -->
		<div class="anwp-mb-4 anwp-p-4 anwp-bg-white anwp-border anwp-border-gray-300 anwp-rounded" style="border-left: 4px solid #9333ea;">
			<h3 class="anwp-mt-0 anwp-mb-3 anwp-font-semibold">
				<?php echo esc_html__( 'Select Your Sport', 'sports-leagues' ); ?>
			</h3>
			<p class="anwp-mb-3 anwp-text-sm anwp-text-gray-600">
				<?php echo esc_html__( 'Choose a sport to load recommended settings for positions, roster groups, standings columns, and more.', 'sports-leagues' ); ?>
			</p>
			<div class="anwp-d-grid anwp-grid-auto-fill anwp-grid-min-140 anwp-gap-3 anwp-mb-4" id="anwpsl-sport-selector">
				<?php foreach ( $sports_available as $sport ) : ?>
				<label class="anwp-p-3 anwp-border anwp-border-gray-300 anwp-rounded anwp-cursor-pointer anwp-text-center anwp-bg-gray-50 anwpsl-sport-card" style="transition: all 0.15s ease;">
					<input type="radio" name="anwpsl_sport" value="<?php echo esc_attr( $sport['slug'] ); ?>" class="anwp-d-none anwpsl-sport-radio">
					<span class="anwp-d-block anwp-text-2xl anwp-mb-1"><?php echo esc_html( $sport['icon'] ); ?></span>
					<span class="anwp-d-block anwp-text-sm anwp-font-semibold"><?php echo esc_html( $sport['title'] ); ?></span>
				</label>
				<?php endforeach; ?>
			</div>
			<div class="anwp-d-flex anwp-items-center anwp-gap-3 anwp-flex-wrap">
				<button type="button" id="anwpsl-load-config-btn" class="button button-primary" disabled>
					<?php echo esc_html__( 'Load Default Configuration', 'sports-leagues' ); ?>
				</button>
				<span class="anwp-text-sm anwp-text-gray-500">
					<?php
					printf(
						/* translators: %s: link to configurator page */
						esc_html__( 'or %sconfigure manually%s', 'sports-leagues' ),
						'<a href="' . esc_url( admin_url( 'admin.php?page=sl-configurator' ) ) . '">',
						'</a>'
					);
					?>
				</span>
			</div>
		</div>
		<script>
		(function() {
			var cards = document.querySelectorAll('.anwpsl-sport-card');
			var radios = document.querySelectorAll('.anwpsl-sport-radio');
			var loadBtn = document.getElementById('anwpsl-load-config-btn');
			var selectedSport = null;

			cards.forEach(function(card) {
				card.addEventListener('click', function() {
					// Remove selection from all cards
					cards.forEach(function(c) {
						c.style.borderColor = '#d1d5db';
						c.style.backgroundColor = '#f9fafb';
						c.style.boxShadow = 'none';
					});
					// Highlight selected card
					card.style.borderColor = '#9333ea';
					card.style.backgroundColor = '#faf5ff';
					card.style.boxShadow = '0 0 0 2px rgba(147, 51, 234, 0.2)';

					// Get selected sport
					var radio = card.querySelector('.anwpsl-sport-radio');
					if (radio) {
						radio.checked = true;
						selectedSport = radio.value;
						loadBtn.disabled = false;
					}
				});
			});

			loadBtn.addEventListener('click', function() {
				if (!selectedSport) return;

				loadBtn.disabled = true;
				loadBtn.textContent = '<?php echo esc_js( __( 'Loading...', 'sports-leagues' ) ); ?>';

				// Fetch recommended config and save
				fetch('<?php echo esc_url( rest_url( 'sports-leagues/v1/settings/save_sport_config' ) ); ?>', {
					method: 'POST',
					headers: {
						'Content-Type': 'application/json',
						'X-WP-Nonce': '<?php echo esc_js( wp_create_nonce( 'wp_rest' ) ); ?>'
					},
					body: JSON.stringify({
						sport_config: window._slRecommendedConfigs[selectedSport]
					})
				})
				.then(function(response) { return response.json(); })
				.then(function(data) {
					if (data.result) {
						window.location.reload();
					} else {
						loadBtn.disabled = false;
						loadBtn.textContent = '<?php echo esc_js( __( 'Load Default Configuration', 'sports-leagues' ) ); ?>';
						alert('<?php echo esc_js( __( 'Error saving configuration. Please try again.', 'sports-leagues' ) ); ?>');
					}
				})
				.catch(function() {
					loadBtn.disabled = false;
					loadBtn.textContent = '<?php echo esc_js( __( 'Load Default Configuration', 'sports-leagues' ) ); ?>';
					alert('<?php echo esc_js( __( 'Error saving configuration. Please try again.', 'sports-leagues' ) ); ?>');
				});
			});

			// Store recommended configs for AJAX call
			window._slRecommendedConfigs = <?php echo wp_json_encode( Sports_Leagues_Config::get_recommended_configs() ); ?>;
		})();
		</script>
		<?php endif; ?>

		<!-- Premium API Import Banner (dismissable) -->
		<?php if ( $data['is_premium_new'] || $debug_banner( 'api_import' ) ) : ?>
		<div id="anwpsl-api-import-banner" class="anwp-mb-4 anwp-p-4 anwp-bg-green-50 anwp-border anwp-border-green-500 anwp-rounded" style="border-left: 4px solid #00a32a; display: none;">
			<div class="anwp-d-flex anwp-justify-between anwp-items-start">
				<div>
					<h3 class="anwp-mt-0 anwp-mb-2 anwp-font-semibold">
						🚀 <?php echo esc_html__( 'Set up automatic data import', 'sports-leagues' ); ?>
					</h3>
					<p class="anwp-mb-2"><?php echo esc_html__( 'Import games, standings, and player data automatically from sports APIs.', 'sports-leagues' ); ?></p>
					<p class="anwp-mb-2 anwp-text-sm">
						<?php echo esc_html__( 'Available for:', 'sports-leagues' ); ?>
						<a href="https://anwp.pro/how-to-create-nfl-live-scores-website-in-wordpress/" target="_blank" class="anwp-font-semibold">NFL</a>,
						<a href="https://anwp.pro/ultimate-guide-to-building-nba-basketball-live-scores-stats-website-in-wordpress/" target="_blank" class="anwp-font-semibold">NBA</a>
					</p>
					<div class="anwp-d-flex anwp-flex-wrap anwp-gap-2">
						<a href="<?php echo esc_url( admin_url( 'admin.php?page=sports-leagues-api' ) ); ?>" class="button button-primary">
							<?php echo esc_html__( 'Configure API Import', 'sports-leagues' ); ?> →
						</a>
						<a href="https://anwp.pro/docs/sports-leagues/pro-features/api-import/" target="_blank" class="button">
							<?php echo esc_html__( 'View Tutorial', 'sports-leagues' ); ?>
						</a>
					</div>
				</div>
				<button type="button" id="anwpsl-api-import-dismiss" class="anwp-bg-transparent anwp-border-0 anwp-cursor-pointer anwp-p-1" title="<?php echo esc_attr__( 'Dismiss', 'sports-leagues' ); ?>">
					<span class="dashicons dashicons-no-alt anwp-dashicons-20 anwp-text-gray-500"></span>
				</button>
			</div>
		</div>
		<script>
		(function() {
			var banner = document.getElementById('anwpsl-api-import-banner');
			var dismissBtn = document.getElementById('anwpsl-api-import-dismiss');
			var storageKey = 'anwpsl_api_import_banner_dismissed';

			if (banner && !localStorage.getItem(storageKey)) {
				banner.style.display = 'block';
			}

			if (dismissBtn) {
				dismissBtn.addEventListener('click', function() {
					localStorage.setItem(storageKey, '1');
					banner.style.display = 'none';
				});
			}
		})();
		</script>
		<?php endif; ?>

		<!-- Premium Promotion Banner (free users only) -->
		<?php if ( ! function_exists( 'sports_leagues_premium' ) || $debug_banner( 'premium_promo' ) ) : ?>
		<div class="anwp-mb-4 anwp-p-4 anwp-bg-orange-50 anwp-border anwp-border-orange-400 anwp-rounded" style="border-left: 4px solid #ea580c;">
			<div class="anwp-d-flex anwp-flex-wrap anwp-items-start anwp-gap-4">
				<div class="anwp-grow" style="min-width: 280px;">
					<h3 class="anwp-mt-0 anwp-mb-2 anwp-text-base anwp-font-semibold anwp-d-flex anwp-items-center">
						<span class="dashicons dashicons-star-filled anwp-dashicons-18 anwp-text-orange-600 anwp-mr-2"></span>
						<?php echo esc_html__( 'Unlock Premium Features', 'sports-leagues' ); ?>
					</h3>
					<div class="anwp-d-grid anwp-grid-auto-fill anwp-grid-min-200 anwp-gap-2 anwp-mb-3 anwp-sl-premium-features">
						<div class="anwp-d-flex anwp-items-center anwp-text-sm">
							<span class="dashicons dashicons-yes anwp-dashicons-16 anwp-text-orange-600 anwp-mr-1"></span>
							<?php echo esc_html__( 'Automatic API Import', 'sports-leagues' ); ?> *
						</div>
						<div class="anwp-d-flex anwp-items-center anwp-text-sm">
							<span class="dashicons dashicons-yes anwp-dashicons-16 anwp-text-orange-600 anwp-mr-1"></span>
							<?php echo esc_html__( 'Layout Builder', 'sports-leagues' ); ?>
						</div>
						<div class="anwp-d-flex anwp-items-center anwp-text-sm">
							<span class="dashicons dashicons-yes anwp-dashicons-16 anwp-text-orange-600 anwp-mr-1"></span>
							<?php echo esc_html__( 'Tournament Brackets', 'sports-leagues' ); ?>
						</div>
						<div class="anwp-d-flex anwp-items-center anwp-text-sm">
							<span class="dashicons dashicons-yes anwp-dashicons-16 anwp-text-orange-600 anwp-mr-1"></span>
							<a href="https://anwp.pro/docs/sports-leagues/6-premium-features/premium-shortcodes/" target="_blank" class="anwp-text-gray-900"><?php echo esc_html__( 'Premium Shortcodes', 'sports-leagues' ); ?></a>
						</div>
						<div class="anwp-d-flex anwp-items-center anwp-text-sm">
							<span class="dashicons dashicons-yes anwp-dashicons-16 anwp-text-orange-600 anwp-mr-1"></span>
							<a href="https://anwp.pro/docs/sports-leagues/6-premium-features/ai-writer/" target="_blank" class="anwp-text-gray-900"><?php echo esc_html__( 'AI Writer', 'sports-leagues' ); ?></a>
						</div>
						<div class="anwp-d-flex anwp-items-center anwp-text-sm">
							<span class="dashicons dashicons-yes anwp-dashicons-16 anwp-text-orange-600 anwp-mr-1"></span>
							<a href="https://anwp.pro/docs/sports-leagues/6-premium-features/entity-links/" target="_blank" class="anwp-text-gray-900"><?php echo esc_html__( 'Entity Links', 'sports-leagues' ); ?></a>
						</div>
						<div class="anwp-d-flex anwp-items-center anwp-text-sm">
							<span class="dashicons dashicons-yes anwp-dashicons-16 anwp-text-orange-600 anwp-mr-1"></span>
							<a href="https://anwp.pro/docs/sports-leagues/6-premium-features/premium-shortcodes/search-shortcode/" target="_blank" class="anwp-text-gray-900"><?php echo esc_html__( 'Live Search', 'sports-leagues' ); ?></a>
						</div>
						<div class="anwp-d-flex anwp-items-center anwp-text-sm">
							<span class="dashicons dashicons-yes anwp-dashicons-16 anwp-text-orange-600 anwp-mr-1"></span>
							<a href="https://anwp.pro/docs/sports-leagues/6-premium-features/results-matrix/" target="_blank" class="anwp-text-gray-900"><?php echo esc_html__( 'Results Matrix', 'sports-leagues' ); ?></a>
						</div>
						<div class="anwp-d-flex anwp-items-center anwp-text-sm">
							<span class="dashicons dashicons-yes anwp-dashicons-16 anwp-text-orange-600 anwp-mr-1"></span>
							<a href="https://support.anwp.pro/knowledge-bases/6/articles/224-bracket-layout" target="_blank" class="anwp-text-gray-900"><?php echo esc_html__( 'Bracket Layout', 'sports-leagues' ); ?></a>
						</div>
						<div class="anwp-d-flex anwp-items-center anwp-text-sm">
							<span class="dashicons dashicons-yes anwp-dashicons-16 anwp-text-orange-600 anwp-mr-1"></span>
							<a href="https://support.anwp.pro/knowledge-bases/6/articles/217-commentary-play-by-play-block" target="_blank" class="anwp-text-gray-900"><?php echo esc_html__( 'Commentary / Play-by-Play', 'sports-leagues' ); ?></a>
						</div>
						<div class="anwp-d-flex anwp-items-center anwp-text-sm">
							<span class="dashicons dashicons-yes anwp-dashicons-16 anwp-text-orange-600 anwp-mr-1"></span>
							<a href="https://support.anwp.pro/knowledge-bases/6/articles/211-team-trophies" target="_blank" class="anwp-text-gray-900"><?php echo esc_html__( 'Team Trophies', 'sports-leagues' ); ?></a>
						</div>
						<div class="anwp-d-flex anwp-items-center anwp-text-sm">
							<span class="dashicons dashicons-yes anwp-dashicons-16 anwp-text-orange-600 anwp-mr-1"></span>
							<a href="https://support.anwp.pro/knowledge-bases/6/articles/207-teams-formation" target="_blank" class="anwp-text-gray-900"><?php echo esc_html__( 'Teams Formation', 'sports-leagues' ); ?></a>
						</div>
						<div class="anwp-d-flex anwp-items-center anwp-text-sm">
							<span class="dashicons dashicons-yes anwp-dashicons-16 anwp-text-orange-600 anwp-mr-1"></span>
							<a href="https://support.anwp.pro/knowledge-bases/6/articles/227-advanced-standing-columns-builder" target="_blank" class="anwp-text-gray-900"><?php echo esc_html__( 'Advanced Standing Columns', 'sports-leagues' ); ?></a>
						</div>
						<div class="anwp-d-flex anwp-items-center anwp-text-sm">
							<span class="dashicons dashicons-yes anwp-dashicons-16 anwp-text-orange-600 anwp-mr-1"></span>
							<a href="https://support.anwp.pro/knowledge-bases/6/articles/225-conference-and-division-support" target="_blank" class="anwp-text-gray-900"><?php echo esc_html__( 'Conference / Division Support', 'sports-leagues' ); ?></a>
						</div>
						<div class="anwp-d-flex anwp-items-center anwp-text-sm">
							<span class="dashicons dashicons-yes anwp-dashicons-16 anwp-text-orange-600 anwp-mr-1"></span>
							<a href="https://support.anwp.pro/knowledge-bases/6/articles/3700-advanced-buttons" target="_blank" class="anwp-text-gray-900"><?php echo esc_html__( 'Advanced Buttons', 'sports-leagues' ); ?></a>
						</div>
						<div class="anwp-d-flex anwp-items-center anwp-text-sm">
							<span class="dashicons dashicons-yes anwp-dashicons-16 anwp-text-orange-600 anwp-mr-1"></span>
							<a href="https://support.anwp.pro/knowledge-bases/6/articles/2999-premium-layouts" target="_blank" class="anwp-text-gray-900"><?php echo esc_html__( 'Premium Layouts', 'sports-leagues' ); ?></a>
						</div>
						<div class="anwp-d-flex anwp-items-center anwp-text-sm">
							<span class="dashicons dashicons-yes anwp-dashicons-16 anwp-text-orange-600 anwp-mr-1"></span>
							<a href="https://anwp.pro/user-roles-game-edit-rights-can-be-assigned-to-any-registered-user-in-sports-leagues/" target="_blank" class="anwp-text-gray-900"><?php echo esc_html__( 'User Roles & Game Edit Rights', 'sports-leagues' ); ?></a>
						</div>
						<div class="anwp-d-flex anwp-items-center anwp-text-sm">
							<span class="dashicons dashicons-yes anwp-dashicons-16 anwp-text-orange-600 anwp-mr-1"></span>
							<a href="https://anwp.pro/seo-options-in-layout-builder-title-and-description/" target="_blank" class="anwp-text-gray-900"><?php echo esc_html__( 'SEO Options in Layout Builder', 'sports-leagues' ); ?></a>
						</div>
						<div class="anwp-d-flex anwp-items-center anwp-text-sm">
							<span class="dashicons dashicons-yes anwp-dashicons-16 anwp-text-orange-600 anwp-mr-1"></span>
							<a href="https://anwp.pro/docs/sports-leagues/6-premium-features/automatic-user-timezone/" target="_blank" class="anwp-text-gray-900"><?php echo esc_html__( 'Automatic User Timezone', 'sports-leagues' ); ?></a>
						</div>
						<div class="anwp-d-flex anwp-items-center anwp-text-sm">
							<span class="dashicons dashicons-yes anwp-dashicons-16 anwp-text-orange-600 anwp-mr-1"></span>
							<?php echo esc_html__( 'Priority Support', 'sports-leagues' ); ?>
						</div>
					</div>
					<p class="anwp-mt-0 anwp-mb-0 anwp-text-xs anwp-text-gray-600">
						<?php echo esc_html__( '* API Import requires a separate subscription to a sports data provider. Available for', 'sports-leagues' ); ?>
						<a href="https://anwp.pro/how-to-create-nfl-live-scores-website-in-wordpress/" target="_blank" class="anwp-text-gray-700">NFL</a>,
						<a href="https://anwp.pro/ultimate-guide-to-building-nba-basketball-live-scores-stats-website-in-wordpress/" target="_blank" class="anwp-text-gray-700">NBA</a>.
					</p>
				</div>
				<div class="anwp-shrink-0">
					<a href="https://anwp.pro/sports-leagues-premium/" target="_blank" class="button button-primary anwp-d-flex anwp-items-center anwp-justify-center">
						<span class="dashicons dashicons-cart anwp-dashicons-18"></span>
						<span class="anwp-ml-2"><?php echo esc_html__( 'Get Premium', 'sports-leagues' ); ?></span>
					</a>
				</div>
			</div>
		</div>
		<?php endif; ?>

		<!-- Stats Grid -->
		<div class="anwp-mb-4 anwp-p-4 anwp-bg-white anwp-border anwp-border-gray-300 anwp-rounded">
			<h2 class="anwp-mt-0 anwp-mb-3 anwp-text-base anwp-font-semibold"><?php echo esc_html__( 'Your Data', 'sports-leagues' ); ?></h2>
			<div class="anwp-d-flex anwp-flex-wrap anwp-gap-3">
				<?php foreach ( $entities as $key => $entity ) :
					$disabled = isset( $can_add[ $key ] ) && ! $can_add[ $key ];
					$count    = $counts[ $key ] ?? 0;
				?>
				<div class="anwp-p-2 anwp-bg-gray-50 anwp-border anwp-border-gray-300 anwp-rounded anwp-text-center" style="min-width: 90px; flex: 1 1 90px; max-width: 110px;">
					<span class="dashicons <?php echo esc_attr( $entity['icon'] ); ?> anwp-text-gray-500" style="font-size: 20px; width: 20px; height: 20px;"></span>
					<div class="anwp-text-base anwp-font-bold anwp-text-gray-900"><?php echo esc_html( $count ); ?></div>
					<div class="anwp-text-xs anwp-text-gray-600 anwp-mb-1"><?php echo esc_html( $entity['label'] ); ?></div>
					<?php if ( $disabled ) : ?>
						<span class="button button-small disabled" style="pointer-events: none; opacity: 0.6;" title="<?php echo esc_attr( $entity['tooltip'] ?? '' ); ?>">+ <?php echo esc_html__( 'Add', 'sports-leagues' ); ?></span>
					<?php else : ?>
						<a href="<?php echo esc_url( admin_url( $entity['url'] ) ); ?>" class="button button-small">+ <?php echo esc_html__( 'Add', 'sports-leagues' ); ?></a>
					<?php endif; ?>
				</div>
				<?php endforeach; ?>
			</div>
		</div>

		<!-- Configuration & Statistics Section -->
		<div class="anwp-mb-4">
			<h2 class="anwp-mt-0 anwp-mb-3 anwp-text-base anwp-font-semibold">
				<?php echo esc_html__( 'Configuration & Statistics', 'sports-leagues' ); ?>
			</h2>
			<div class="anwp-d-grid anwp-grid-auto-fill anwp-grid-min-300 anwp-gap-4">

				<!-- Sport Configurator Card -->
				<div class="anwp-border anwp-border-gray-300 anwp-rounded anwp-bg-white anwp-d-flex anwp-flex-col">
					<div class="anwp-border-bottom anwp-border-gray-300 anwp-px-3 anwp-py-2 anwp-d-flex anwp-items-center">
						<span class="dashicons dashicons-admin-generic anwp-text-gray-600"></span>
						<span class="anwp-ml-2 anwp-font-semibold"><?php echo esc_html__( 'Sport Configurator', 'sports-leagues' ); ?></span>
					</div>
					<div class="anwp-p-3 anwp-grow">
						<?php if ( ! empty( $config_summary ) ) : ?>
							<?php foreach ( $config_summary as $config_item ) : ?>
								<div class="anwp-text-sm anwp-mb-1">
									<span class="anwp-font-semibold"><?php echo esc_html( $config_item['label'] ); ?>:</span>
									<?php echo esc_html( $config_item['value'] ); ?>
								</div>
							<?php endforeach; ?>
							<?php if ( $config_has_more ) : ?>
								<div class="anwp-text-sm anwp-text-gray-500">.....</div>
							<?php endif; ?>
						<?php else : ?>
							<div class="anwp-text-sm anwp-text-gray-500 anwp-italic"><?php echo esc_html__( 'No sport configured yet', 'sports-leagues' ); ?></div>
						<?php endif; ?>
					</div>
					<div class="anwp-border-top anwp-border-gray-300 anwp-px-3 anwp-py-2 anwp-text-right">
						<a href="<?php echo esc_url( admin_url( 'admin.php?page=sl-configurator' ) ); ?>" class="button button-small">
							<?php echo esc_html__( 'Open Sport Configurator', 'sports-leagues' ); ?>
						</a>
					</div>
				</div>

				<!-- Player Statistics Card -->
				<div class="anwp-border anwp-border-gray-300 anwp-rounded anwp-bg-white anwp-d-flex anwp-flex-col">
					<div class="anwp-border-bottom anwp-border-gray-300 anwp-px-3 anwp-py-2 anwp-d-flex anwp-items-center">
						<span class="dashicons dashicons-chart-bar anwp-text-gray-600"></span>
						<span class="anwp-ml-2 anwp-font-semibold"><?php echo esc_html__( 'Player Statistics Setup', 'sports-leagues' ); ?></span>
					</div>
					<div class="anwp-p-3 anwp-grow">
						<?php if ( ! empty( $stats_names ) ) : ?>
							<?php foreach ( $stats_names as $stat_name ) : ?>
								<div class="anwp-text-sm"><?php echo esc_html( $stat_name ); ?></div>
							<?php endforeach; ?>
							<?php if ( count( $player_stats ) > 4 ) : ?>
								<div class="anwp-text-sm anwp-text-gray-500">.....</div>
							<?php endif; ?>
						<?php else : ?>
							<div class="anwp-text-sm anwp-text-gray-500 anwp-italic"><?php echo esc_html__( 'No statistics configured yet', 'sports-leagues' ); ?></div>
						<?php endif; ?>
					</div>
					<div class="anwp-border-top anwp-border-gray-300 anwp-px-3 anwp-py-2 anwp-text-right">
						<a href="<?php echo esc_url( admin_url( 'admin.php?page=sl-player-stats' ) ); ?>" class="button button-small">
							<?php echo esc_html__( 'Open Player Statistics Setup', 'sports-leagues' ); ?>
						</a>
					</div>
				</div>

				<!-- Game Events Card -->
				<div class="anwp-border anwp-border-gray-300 anwp-rounded anwp-bg-white anwp-d-flex anwp-flex-col">
					<div class="anwp-border-bottom anwp-border-gray-300 anwp-px-3 anwp-py-2 anwp-d-flex anwp-items-center">
						<span class="dashicons dashicons-randomize anwp-text-gray-600"></span>
						<span class="anwp-ml-2 anwp-font-semibold"><?php echo esc_html__( 'Game Events', 'sports-leagues' ); ?></span>
					</div>
					<div class="anwp-p-3 anwp-grow">
						<?php if ( ! empty( $event_names ) ) : ?>
							<?php foreach ( $event_names as $event_name ) : ?>
								<div class="anwp-text-sm"><?php echo esc_html( $event_name ); ?></div>
							<?php endforeach; ?>
							<?php if ( ! empty( $events_data['events'] ) && count( $events_data['events'] ) > 4 ) : ?>
								<div class="anwp-text-sm anwp-text-gray-500">.....</div>
							<?php endif; ?>
						<?php else : ?>
							<div class="anwp-text-sm anwp-text-gray-500 anwp-italic"><?php echo esc_html__( 'No events configured yet', 'sports-leagues' ); ?></div>
						<?php endif; ?>
					</div>
					<div class="anwp-border-top anwp-border-gray-300 anwp-px-3 anwp-py-2 anwp-text-right">
						<a href="<?php echo esc_url( admin_url( 'admin.php?page=sports_leagues_event' ) ); ?>" class="button button-small">
							<?php echo esc_html__( 'Open Game Events Setup', 'sports-leagues' ); ?>
						</a>
					</div>
				</div>

			</div>
		</div>

		<!-- Changelog Section -->
		<div class="anwp-mb-4 anwp-p-4 anwp-bg-white anwp-border anwp-border-gray-300 anwp-rounded">
			<h2 class="anwp-mt-0 anwp-mb-3 anwp-text-base anwp-font-semibold"><?php echo esc_html__( "What's New", 'sports-leagues' ); ?></h2>
			<div class="anwp-d-grid anwp-grid-auto-fill anwp-grid-min-350 anwp-gap-4">

				<!-- Core Changelog -->
				<div>
					<h3 class="anwp-mt-0 anwp-mb-2 anwp-text-sm anwp-font-semibold anwp-text-gray-700"><?php echo esc_html__( 'Core Plugin', 'sports-leagues' ); ?></h3>
					<?php foreach ( $core_changelog as $version ) : ?>
						<div class="anwp-mb-3">
							<strong class="anwp-text-sm"><?php echo esc_html( $version['version'] ); ?></strong>
							<span class="anwp-text-gray-500 anwp-text-xs"><?php echo esc_html( $version['date'] ); ?></span>
							<ul class="anwp-mt-1 anwp-mb-0 anwp-pl-4" style="list-style: none;">
								<?php foreach ( $version['changes'] as $change ) : ?>
									<li class="anwp-text-xs anwp-mb-1"><?php echo esc_html( $change ); ?></li>
								<?php endforeach; ?>
							</ul>
						</div>
					<?php endforeach; ?>
					<a href="https://support.anwp.pro/knowledge-bases/12-sl-changelog/categories/72-basic-version/articles" target="_blank" class="anwp-text-sm"><?php echo esc_html__( 'Full Changelog', 'sports-leagues' ); ?> →</a>
				</div>

				<!-- Premium Changelog -->
				<div>
					<h3 class="anwp-mt-0 anwp-mb-2 anwp-text-sm anwp-font-semibold anwp-text-gray-700 anwp-d-flex anwp-items-center">
						<?php echo esc_html__( 'Premium', 'sports-leagues' ); ?>
						<?php if ( ! function_exists( 'sports_leagues_premium' ) ) : ?>
							<span class="anwp-ml-2 anwp-px-1 anwp-py-0 anwp-bg-orange-600 anwp-text-white anwp-text-xxs anwp-rounded anwp-font-semibold">PRO</span>
						<?php endif; ?>
					</h3>
					<?php if ( ! empty( $premium_changelog ) ) : ?>
						<?php foreach ( $premium_changelog as $version ) : ?>
							<div class="anwp-mb-3">
								<strong class="anwp-text-sm"><?php echo esc_html( $version['version'] ); ?></strong>
								<span class="anwp-text-gray-500 anwp-text-xs"><?php echo esc_html( $version['date'] ); ?></span>
								<ul class="anwp-mt-1 anwp-mb-0 anwp-pl-4" style="list-style: none;">
									<?php foreach ( $version['changes'] as $change ) : ?>
										<li class="anwp-text-xs anwp-mb-1"><?php echo esc_html( $change ); ?></li>
									<?php endforeach; ?>
								</ul>
							</div>
						<?php endforeach; ?>
						<a href="https://support.anwp.pro/knowledge-bases/12-sl-changelog/categories/84-premium-addon/articles" target="_blank" class="anwp-text-sm"><?php echo esc_html__( 'Full Changelog', 'sports-leagues' ); ?> →</a>
					<?php else : ?>
						<p class="anwp-mt-0 anwp-mb-3 anwp-text-sm anwp-text-gray-600">
							<?php echo esc_html__( 'Discover new features and improvements in the Premium version.', 'sports-leagues' ); ?>
						</p>
						<a href="https://support.anwp.pro/knowledge-bases/12-sl-changelog/categories/84-premium-addon/articles" target="_blank" class="anwp-text-sm"><?php echo esc_html__( 'View Premium Changelog', 'sports-leagues' ); ?> →</a>
					<?php endif; ?>
				</div>

				<!-- Tutorials & Articles -->
				<div>
					<h3 class="anwp-mt-0 anwp-mb-2 anwp-text-sm anwp-font-semibold anwp-text-gray-700"><?php echo esc_html__( 'Latest Tutorials', 'sports-leagues' ); ?></h3>
					<ul class="anwp-mb-0 anwp-pl-0" style="list-style: none;">
						<?php foreach ( $tutorials as $tutorial ) : ?>
							<li class="anwp-mb-2 anwp-d-flex anwp-items-center anwp-flex-wrap anwp-gap-1">
								<a href="<?php echo esc_url( $tutorial['url'] ); ?>" target="_blank" class="anwp-text-sm">
									<?php echo esc_html( $tutorial['title'] ); ?>
								</a>
								<?php foreach ( $tutorial['badges'] as $badge ) : ?>
									<?php if ( 'pro' === $badge ) : ?>
										<span class="anwp-px-1 anwp-py-0 anwp-bg-orange-600 anwp-text-white anwp-text-xxs anwp-rounded anwp-font-semibold">PRO</span>
									<?php elseif ( 'new' === $badge ) : ?>
										<span class="anwp-px-1 anwp-py-0 anwp-bg-green-600 anwp-text-white anwp-text-xxs anwp-rounded anwp-font-semibold">NEW</span>
									<?php elseif ( 'updated' === $badge ) : ?>
										<span class="anwp-px-1 anwp-py-0 anwp-bg-blue-600 anwp-text-white anwp-text-xxs anwp-rounded anwp-font-semibold">UPD</span>
									<?php endif; ?>
								<?php endforeach; ?>
							</li>
						<?php endforeach; ?>
					</ul>
					<a href="https://anwp.pro/docs/sports-leagues/" target="_blank" class="anwp-text-sm anwp-mt-2 anwp-d-inline-block"><?php echo esc_html__( 'All Documentation', 'sports-leagues' ); ?> →</a>
				</div>

			</div>
		</div>

	</div>
</div>
