<?php

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

/**
 * Changeloger Dashboard Admin Page
 *
 * Practical, data-focused dashboard with actionable insights.
 */
class Changeloger_Dashboard {

	/**
	 * Cache for feedback stats to prevent redundant queries.
	 *
	 * @var array|null
	 */
	private $feedback_stats_cache = null;

	public function __construct() {
		add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_dashboard_styles' ] );
	}

	/**
	 * Enqueue dashboard-specific styles
	 *
	 * @param string $hook Current admin page hook.
	 * @return void
	 */
	public function enqueue_dashboard_styles( string $hook ): void {
		// Only load on Changeloger pages
		if ( 'toplevel_page_changeloger' !== $hook ) {
			return;
		}

		wp_enqueue_style(
			'cha-dashboard',
			plugins_url( 'assets/css/dashboard.css', dirname( __FILE__ ) ),
			[],
			defined( 'CHANGELOGER_VERSION' ) ? CHANGELOGER_VERSION : '1.0.0'
		);
	}

	/**
	 * Render the dashboard page
	 *
	 * @return void
	 */
	public function render_page(): void {
		$stats           = $this->get_all_stats();
		$feedback_status = $this->get_feedback_by_status();
		$recent_releases = $this->get_recent_releases( 5 );
		$recent_feedback = $this->get_recent_feedback( 5 );
		$system_status   = $this->get_system_status();
		$is_premium      = function_exists( 'cha_fs' ) && cha_fs()->can_use_premium_code__premium_only();
		$version         = defined( 'CHANGELOGER_VERSION' ) ? CHANGELOGER_VERSION : '1.0.0';

?>
		<div class="cha-page-wrap cha-dashboard">
			<!-- Header -->
			<div class="cha-page-header">
				<div class="cha-page-header-left">
					<div class="cha-page-header-icon">
						<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor" width="28" height="28">
							<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6a2 2 0 012-2h2a2 2 0 012 2v2a2 2 0 01-2 2H6a2 2 0 01-2-2V6zM14 6a2 2 0 012-2h2a2 2 0 012 2v2a2 2 0 01-2 2h-2a2 2 0 01-2-2V6zM4 16a2 2 0 012-2h2a2 2 0 012 2v2a2 2 0 01-2 2H6a2 2 0 01-2-2v-2zM14 16a2 2 0 012-2h2a2 2 0 012 2v2a2 2 0 01-2 2h-2a2 2 0 01-2-2v-2z" />
						</svg>
					</div>
					<div class="cha-page-header-content">
						<h1>
							<?php esc_html_e('Dashboard', 'changeloger'); ?>
							<span class="version">v<?php echo esc_html($version); ?></span>
						</h1>
						<p class="cha-page-header-subtitle"><?php esc_html_e('Overview of your Changeloger activity and statistics', 'changeloger'); ?></p>
					</div>
				</div>
				<div class="cha-page-header-right">
					<a href="<?php echo esc_url(admin_url('admin.php?page=changeloger-releases')); ?>" class="cha-btn cha-btn-primary">
						<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor" width="16" height="16">
							<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4" />
						</svg>
						<?php esc_html_e('New Release', 'changeloger'); ?>
					</a>
					<a href="<?php echo esc_url(admin_url('admin.php?page=changeloger-settings')); ?>" class="cha-btn cha-btn-secondary">
						<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor" width="16" height="16">
							<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.065 2.572c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.572 1.065c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.065-2.572c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z" />
							<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z" />
						</svg>
						<?php esc_html_e('Settings', 'changeloger'); ?>
					</a>
				</div>
			</div>

			<!-- Stats Grid -->
			<div class="cha-stats-grid">
				<!-- Products Card -->
				<a href="<?php echo esc_url(admin_url('admin.php?page=changeloger-releases')); ?>" class="cha-stat-card">
					<div class="cha-stat-icon-wrapper" style="background: #eff6ff; color: #3b82f6;">
						<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor">
							<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 11H5m14 0a2 2 0 012 2v6a2 2 0 01-2 2H5a2 2 0 01-2-2v-6a2 2 0 012-2m14 0V9a2 2 0 00-2-2M5 11V9a2 2 0 012-2m0 0V5a2 2 0 012-2h6a2 2 0 012 2v2M7 7h10" />
						</svg>
					</div>
					<div class="cha-stat-content">
						<span class="cha-stat-label"><?php esc_html_e('Total Products', 'changeloger'); ?></span>
						<div class="cha-stat-value"><?php echo esc_html($stats['products']); ?></div>
					</div>
					<div class="cha-stat-arrow">
						<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor">
							<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7" />
						</svg>
					</div>
				</a>

				<!-- Releases Card -->
				<a href="<?php echo esc_url(admin_url('admin.php?page=changeloger-releases')); ?>" class="cha-stat-card">
					<div class="cha-stat-icon-wrapper" style="background: #fdf4ff; color: #a855f7;">
						<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor">
							<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M7 7h.01M7 3h5c.512 0 1.024.195 1.414.586l7 7a2 2 0 010 2.828l-7 7a2 2 0 01-2.828 0l-7-7A1.994 1.994 0 013 12V7a4 4 0 014-4z" />
						</svg>
					</div>
					<div class="cha-stat-content">
						<span class="cha-stat-label"><?php esc_html_e('Active Releases', 'changeloger'); ?></span>
						<div class="cha-stat-value"><?php echo esc_html($stats['releases']); ?></div>
					</div>
					<div class="cha-stat-arrow">
						<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor">
							<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7" />
						</svg>
					</div>
				</a>

				<!-- Feedback Card -->
				<a href="<?php echo esc_url(admin_url('admin.php?page=changeloger-feedback')); ?>" class="cha-stat-card">
					<div class="cha-stat-icon-wrapper" style="background: #ecfdf5; color: #10b981;">
						<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor">
							<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 10h.01M12 10h.01M16 10h.01M9 16H5a2 2 0 01-2-2V6a2 2 0 012-2h14a2 2 0 012 2v8a2 2 0 01-2 2h-5l-5 5v-5z" />
						</svg>
					</div>
					<div class="cha-stat-content">
						<span class="cha-stat-label"><?php esc_html_e('User Feedback', 'changeloger'); ?></span>
						<div class="cha-stat-value"><?php echo esc_html($stats['feedback']); ?></div>
					</div>
					<div class="cha-stat-arrow">
						<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor">
							<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7" />
						</svg>
					</div>
				</a>

				<!-- Roadmap Card -->
				<a href="<?php echo esc_url(admin_url('admin.php?page=changeloger-roadmap')); ?>" class="cha-stat-card">
					<div class="cha-stat-icon-wrapper" style="background: #fff7ed; color: #f97316;">
						<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor">
							<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 17V7m0 10a2 2 0 01-2 2H5a2 2 0 01-2-2V7a2 2 0 012-2h2a2 2 0 012 2m0 10a2 2 0 002 2h2a2 2 0 002-2M9 7a2 2 0 012-2h2a2 2 0 012 2m0 10V7m0 10a2 2 0 002 2h2a2 2 0 002-2V7a2 2 0 00-2-2h-2a2 2 0 00-2 2" />
						</svg>
					</div>
					<div class="cha-stat-content">
						<span class="cha-stat-label"><?php esc_html_e('Roadmap Items', 'changeloger'); ?></span>
						<div class="cha-stat-value"><?php echo esc_html($stats['roadmap']); ?></div>
					</div>
					<div class="cha-stat-arrow">
						<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor">
							<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7" />
						</svg>
					</div>
				</a>
			</div>

			<!-- Main Grid -->
			<div class="cha-dash-grid">
				<!-- Left Column -->
				<div class="cha-dash-left">
					<!-- Recent Releases -->
					<div class="cha-dash-card">
						<div class="cha-dash-card-header">
							<h3 class="cha-dash-card-title"><?php esc_html_e('Recent Releases', 'changeloger'); ?></h3>
							<a href="<?php echo esc_url(admin_url('admin.php?page=changeloger-releases')); ?>" class="cha-stat-link"><?php esc_html_e('View All', 'changeloger'); ?></a>
						</div>
						<div class="cha-dash-card-body">
							<?php if (! empty($recent_releases)) : ?>
								<ul class="cha-activity-list">
									<?php foreach ($recent_releases as $release) : ?>
										<li class="cha-activity-item">
											<div class="cha-activity-icon" style="background: #dbeafe; color: #2563eb;">
												<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor">
													<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M7 7h.01M7 3h5c.512 0 1.024.195 1.414.586l7 7a2 2 0 010 2.828l-7 7a2 2 0 01-2.828 0l-7-7A1.994 1.994 0 013 12V7a4 4 0 014-4z" />
												</svg>
											</div>
											<div class="cha-activity-content">
												<div class="cha-activity-title"><?php echo esc_html($release['title']); ?></div>
												<div class="cha-activity-meta">
													<span><?php echo esc_html($release['product']); ?></span>
													<span>•</span>
													<span><?php echo esc_html($release['date']); ?></span>
												</div>
											</div>
										</li>
									<?php endforeach; ?>
								</ul>
							<?php else : ?>
								<div class="cha-activity-empty"><?php esc_html_e('No releases yet. Create your first release!', 'changeloger'); ?></div>
							<?php endif; ?>
						</div>
					</div>

					<!-- Feedback Status -->
					<div class="cha-dash-card">
						<div class="cha-dash-card-header">
							<h3 class="cha-dash-card-title"><?php esc_html_e('Feedback Overview', 'changeloger'); ?></h3>
							<a href="<?php echo esc_url(admin_url('admin.php?page=changeloger-feedback')); ?>" class="cha-stat-link"><?php esc_html_e('View All', 'changeloger'); ?></a>
						</div>
						<div class="cha-dash-card-body">
							<div class="cha-status-grid">
								<div class="cha-status-item">
									<div class="cha-status-count"><?php echo esc_html($feedback_status['open'] ?? 0); ?></div>
									<div class="cha-status-name"><?php esc_html_e('Open', 'changeloger'); ?></div>
								</div>
								<div class="cha-status-item">
									<div class="cha-status-count"><?php echo esc_html($feedback_status['planned'] ?? 0); ?></div>
									<div class="cha-status-name"><?php esc_html_e('Planned', 'changeloger'); ?></div>
								</div>
								<div class="cha-status-item">
									<div class="cha-status-count"><?php echo esc_html($feedback_status['in_progress'] ?? 0); ?></div>
									<div class="cha-status-name"><?php esc_html_e('In Progress', 'changeloger'); ?></div>
								</div>
								<div class="cha-status-item">
									<div class="cha-status-count"><?php echo esc_html($feedback_status['completed'] ?? 0); ?></div>
									<div class="cha-status-name"><?php esc_html_e('Completed', 'changeloger'); ?></div>
								</div>
							</div>
							<?php
							$total = array_sum($feedback_status);
							$resolved = ($feedback_status['completed'] ?? 0) + ($feedback_status['rejected'] ?? 0);
							$percent = $total > 0 ? round(($resolved / $total) * 100) : 0;
							?>
							<div class="cha-progress-bar">
								<div class="cha-progress-fill" style="width: <?php echo esc_attr($percent); ?>%;"></div>
							</div>
							<div class="cha-progress-text">
								<span><?php printf(esc_html__('%d%% resolved', 'changeloger'), $percent); ?></span>
								<span><?php printf(esc_html__('%d total items', 'changeloger'), $total); ?></span>
							</div>
						</div>
					</div>
				</div>

				<!-- Right Column -->
				<div class="cha-dash-right">
					<!-- Quick Actions -->
					<div class="cha-dash-card">
						<div class="cha-dash-card-header">
							<h3 class="cha-dash-card-title"><?php esc_html_e('Quick Actions', 'changeloger'); ?></h3>
						</div>
						<div class="cha-dash-card-body">
							<div class="cha-quick-actions">
								<a href="<?php echo esc_url(admin_url('admin.php?page=changeloger-releases')); ?>" class="cha-quick-action">
									<div class="cha-quick-action-icon" style="background: #dbeafe; color: #2563eb;">
										<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor">
											<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4" />
										</svg>
									</div>
									<span><?php esc_html_e('Create Release', 'changeloger'); ?></span>
								</a>
								<a href="<?php echo esc_url(admin_url('admin.php?page=changeloger-roadmap')); ?>" class="cha-quick-action">
									<div class="cha-quick-action-icon" style="background: #fef3c7; color: #d97706;">
										<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor">
											<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 17V7m0 10a2 2 0 01-2 2H5a2 2 0 01-2-2V7a2 2 0 012-2h2a2 2 0 012 2m0 10a2 2 0 002 2h2a2 2 0 002-2M9 7a2 2 0 012-2h2a2 2 0 012 2m0 10V7m0 10a2 2 0 002 2h2a2 2 0 002-2V7a2 2 0 00-2-2h-2a2 2 0 00-2 2" />
										</svg>
									</div>
									<span><?php esc_html_e('Add Roadmap Item', 'changeloger'); ?></span>
								</a>
								<a href="<?php echo esc_url(admin_url('admin.php?page=changeloger-settings')); ?>" class="cha-quick-action">
									<div class="cha-quick-action-icon" style="background: #f3f4f6; color: #6b7280;">
										<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor">
											<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.065 2.572c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.572 1.065c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.065-2.572c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z" />
											<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z" />
										</svg>
									</div>
									<span><?php esc_html_e('Settings', 'changeloger'); ?></span>
								</a>
								<a href="https://helpdesk.spider-themes.net/docs/changeloger/" target="_blank" class="cha-quick-action">
									<div class="cha-quick-action-icon" style="background: #ede9fe; color: #7c3aed;">
										<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor">
											<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 6.253v13m0-13C10.832 5.477 9.246 5 7.5 5S4.168 5.477 3 6.253v13C4.168 18.477 5.754 18 7.5 18s3.332.477 4.5 1.253m0-13C13.168 5.477 14.754 5 16.5 5c1.747 0 3.332.477 4.5 1.253v13C19.832 18.477 18.247 18 16.5 18c-1.746 0-3.332.477-4.5 1.253" />
										</svg>
									</div>
									<span><?php esc_html_e('Documentation', 'changeloger'); ?></span>
								</a>
							</div>
						</div>
					</div>

					<!-- System Status -->
					<div class="cha-dash-card">
						<div class="cha-dash-card-header">
							<h3 class="cha-dash-card-title"><?php esc_html_e('System Status', 'changeloger'); ?></h3>
						</div>
						<div class="cha-dash-card-body">
							<ul class="cha-system-list">
								<li class="cha-system-item">
									<span class="cha-system-label"><?php esc_html_e('REST API', 'changeloger'); ?></span>
									<?php if ($system_status['rest_api']) : ?>
										<span class="cha-system-badge cha-badge-success">
											<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor" width="12" height="12">
												<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7" />
											</svg>
											<?php esc_html_e('Active', 'changeloger'); ?>
										</span>
									<?php else : ?>
										<span class="cha-system-badge cha-badge-warning"><?php esc_html_e('Issue', 'changeloger'); ?></span>
									<?php endif; ?>
								</li>
								<li class="cha-system-item">
									<span class="cha-system-label"><?php esc_html_e('Version', 'changeloger'); ?></span>
									<span class="cha-system-value"><?php echo esc_html($version); ?></span>
								</li>
								<li class="cha-system-item">
									<span class="cha-system-label"><?php esc_html_e('License', 'changeloger'); ?></span>
									<?php if ($is_premium) : ?>
										<span class="cha-system-badge cha-badge-info"><?php esc_html_e('Pro', 'changeloger'); ?></span>
									<?php else : ?>
										<span class="cha-system-badge cha-badge-warning"><?php esc_html_e('Free', 'changeloger'); ?></span>
									<?php endif; ?>
								</li>
							</ul>
						</div>
					</div>
				</div>
			</div>
		</div>
<?php
	}

	/**
	 * Get all stats
	 *
	 * @return array
	 */
	private function get_all_stats(): array {
		return [
			'products' => $this->get_products_count(),
			'releases' => $this->get_releases_count(),
			'feedback' => $this->get_feedback_count(),
			'roadmap'  => $this->get_roadmap_count(),
		];
	}

	/**
	 * Get products count
	 *
	 * @return int
	 */
	private function get_products_count(): int {
		// Optimized: Use wp_count_terms to avoid loading all term objects
		// Using ( taxonomy, args ) signature for backward compatibility < WP 5.6
		$count = wp_count_terms( 'cha_product', [
			'hide_empty' => false,
		] );
		return ! is_wp_error( $count ) ? (int) $count : 0;
	}

	/**
	 * Get releases count
	 *
	 * @return int
	 */
	private function get_releases_count(): int {
		$count = wp_count_posts( 'cha_release' );
		return isset( $count->publish ) ? $count->publish : 0;
	}

	/**
	 * Get feedback count
	 *
	 * @return int
	 */
	private function get_feedback_count(): int {
		$count = wp_count_posts( 'cha_feedback' );
		return isset( $count->publish ) ? $count->publish : 0;
	}

	/**
	 * Get roadmap items count (feedback items with roadmap statuses)
	 *
	 * @return int
	 */
	private function get_roadmap_count(): int {
		$stats = $this->get_feedback_by_status();
		return (int) ( ( $stats['planned'] ?? 0 ) + ( $stats['in_progress'] ?? 0 ) + ( $stats['completed'] ?? 0 ) );
	}

	/**
	 * Get feedback by status
	 *
	 * @return array
	 */
	private function get_feedback_by_status(): array {
		if ( null !== $this->feedback_stats_cache ) {
			return $this->feedback_stats_cache;
		}

		global $wpdb;

		$result = [
			'open'        => 0,
			'planned'     => 0,
			'in_progress' => 0,
			'completed'   => 0,
			'rejected'    => 0,
		];

		// Optimization: Use wp_count_posts to leverage object cache for total count
		$count_obj = wp_count_posts( 'cha_feedback' );
		$total_feedback = isset( $count_obj->publish ) ? (int) $count_obj->publish : 0;

		// Optimization: Single query to get all status counts
		// Replace N+1 queries with a single SUM(CASE...) aggregation
		$sql = "SELECT
				SUM(CASE WHEN pm.meta_value = 'planned' THEN 1 ELSE 0 END) as planned,
				SUM(CASE WHEN pm.meta_value = 'in_progress' THEN 1 ELSE 0 END) as in_progress,
				SUM(CASE WHEN pm.meta_value = 'completed' THEN 1 ELSE 0 END) as completed,
				SUM(CASE WHEN pm.meta_value = 'rejected' THEN 1 ELSE 0 END) as rejected
			FROM {$wpdb->posts} p
			INNER JOIN {$wpdb->postmeta} pm ON p.ID = pm.post_id 
			WHERE p.post_type = 'cha_feedback' 
			AND p.post_status = 'publish'
			AND pm.meta_key = '_cha_feedback_status'";

		$row = $wpdb->get_row( $sql, ARRAY_A );
		$counted_with_status = 0;

		if ( $row ) {
			foreach ( $row as $status => $count ) {
				$count = (int) $count;
				if ( isset( $result[ $status ] ) ) {
					$result[ $status ] = $count;
					$counted_with_status += $count;
				}
			}
		}

		// Open = total - those with other statuses (this includes items with no status meta)
		$result['open'] = max( 0, (int) $total_feedback - $counted_with_status );

		$this->feedback_stats_cache = $result;

		return $result;
	}

	/**
	 * Get recent releases
	 *
	 * @param int $limit Number of posts to retrieve.
	 * @return array
	 */
	private function get_recent_releases( int $limit = 5 ): array {
		$releases = get_posts( [
			'post_type'      => 'cha_release',
			'posts_per_page' => $limit,
			'post_status'    => 'publish',
			'orderby'        => 'date',
			'order'          => 'DESC',
		] );

		$result = [];
		foreach ( $releases as $release ) {
			$products = wp_get_object_terms( $release->ID, 'cha_product' );
			$product_name = ! empty( $products ) ? $products[0]->name : __( 'No Product', 'changeloger' );

			$result[] = [
				'id'      => $release->ID,
				'title'   => $release->post_title,
				'product' => $product_name,
				'date'    => human_time_diff( strtotime( $release->post_date ), current_time( 'timestamp' ) ) . ' ' . __( 'ago', 'changeloger' ),
			];
		}

		return $result;
	}

	/**
	 * Get recent feedback
	 *
	 * @param int $limit Number of posts to retrieve.
	 * @return array
	 */
	private function get_recent_feedback( int $limit = 5 ): array {
		$items = get_posts( [
			'post_type'      => 'cha_feedback',
			'posts_per_page' => $limit,
			'post_status'    => 'publish',
			'orderby'        => 'date',
			'order'          => 'DESC',
		] );

		$result = [];
		foreach ( $items as $item ) {
			$status = get_post_meta( $item->ID, '_cha_feedback_status', true ) ?: 'open';

			$result[] = [
				'id'     => $item->ID,
				'title'  => $item->post_title,
				'status' => $status,
				'date'   => human_time_diff( strtotime( $item->post_date ), current_time( 'timestamp' ) ) . ' ' . __( 'ago', 'changeloger' ),
			];
		}

		return $result;
	}

	/**
	 * Get system status
	 *
	 * @return array
	 */
	private function get_system_status(): array {
		return [
			'rest_api'    => $this->check_rest_api(),
			'cron_active' => (bool) wp_next_scheduled( 'cha_daily_version_check' ),
		];
	}

	/**
	 * Check REST API availability
	 *
	 * @return bool
	 */
	private function check_rest_api(): bool {
		$cached = get_transient( 'changeloger_rest_api_status' );
		if ( false !== $cached ) {
			return (bool) $cached;
		}

		$response = wp_remote_get( rest_url( 'changeloger/v1/' ), [
			'timeout'   => 1, // Reduced to 1s to prevent dashboard blocking
			'sslverify' => false,
		] );
		$status = ! is_wp_error( $response ) && wp_remote_retrieve_response_code( $response ) < 500;

		set_transient( 'changeloger_rest_api_status', (int) $status, HOUR_IN_SECONDS );

		return $status;
	}
}
