<?php
/**
 * Dashboard page view
 *
 * @package WebEquipe\PDFSearch
 * @since 1.1.0
 */

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

$webequipe_pdf_dashboard = \WebEquipe\PDFSearch\Database::get_dashboard_data();
$webequipe_pdf_stats     = $webequipe_pdf_dashboard['stats'];
$webequipe_pdf_activity  = $webequipe_pdf_dashboard['activity_stats'];
$webequipe_pdf_recent    = $webequipe_pdf_dashboard['recent_activity']['records'];

$webequipe_pdf_settings_url = admin_url( 'admin.php?page=webequipe-pdf-search-settings' );
$webequipe_pdf_help_url     = admin_url( 'admin.php?page=webequipe-pdf-search-help' );
$webequipe_pdf_manage_url   = admin_url( 'admin.php?page=webequipe-pdf-search-manage' );
$webequipe_pdf_activity_url = admin_url( 'admin.php?page=webequipe-pdf-search-activity' );

$webequipe_pdf_shortcode_main = '[webequipe_pdf_search_form]';
$webequipe_pdf_shortcode_full = '[webequipe_pdf_search_form placeholder="Search our documents..." button_text="Find Documents" results_per_page="5"]';

if ( ! function_exists( 'webequipe_pdf_dashboard_format_time' ) ) {
	/**
	 * Format activity timestamp for dashboard display.
	 *
	 * @param string $mysql_date MySQL datetime.
	 * @return string
	 */
	function webequipe_pdf_dashboard_format_time( $mysql_date ) {
		if ( empty( $mysql_date ) ) {
			return '—';
		}

		$timestamp = strtotime( $mysql_date );
		if ( ! $timestamp ) {
			return $mysql_date;
		}

		/* translators: %s: human-readable time difference. */
		return sprintf( __( '%s ago', 'webequipe-pdf-search' ), human_time_diff( $timestamp, current_time( 'timestamp' ) ) );
	}
}

if ( ! function_exists( 'webequipe_pdf_dashboard_status_pill' ) ) {
	/**
	 * Render status pill for dashboard activity rows.
	 *
	 * @param string $status Index status slug.
	 * @return void
	 */
	function webequipe_pdf_dashboard_status_pill( $status ) {
		$labels = array(
			'completed'  => __( 'completed', 'webequipe-pdf-search' ),
			'processing' => __( 'processing', 'webequipe-pdf-search' ),
			'pending'    => __( 'pending', 'webequipe-pdf-search' ),
			'failed'     => __( 'failed', 'webequipe-pdf-search' ),
			'cancelled'  => __( 'cancelled', 'webequipe-pdf-search' ),
		);

		$label = isset( $labels[ $status ] ) ? $labels[ $status ] : $status;
		printf(
			'<span class="webequipe-pdf-activity-status status-%1$s"><span class="webequipe-pdf-dashboard-status-dot" aria-hidden="true"></span>%2$s</span>',
			esc_attr( $status ),
			esc_html( $label )
		);
	}
}
?>
<div class="wrap webequipe-pdf-dashboard-wrap" id="webequipe-pdf-dashboard">
	<?php settings_errors(); ?>

	<?php \WebEquipe\PDFSearch\Admin_Settings::render_pro_banner(); ?>

	<div class="webequipe-pdf-dashboard-header">
		<div class="webequipe-pdf-dashboard-header-text">
			<h1><?php esc_html_e( 'WebEquipe PDF Search', 'webequipe-pdf-search' ); ?></h1>
			<p class="webequipe-pdf-dashboard-subtitle">
				<?php esc_html_e( 'Manage PDF indexing, search health, and recent activity from one place.', 'webequipe-pdf-search' ); ?>
			</p>
		</div>
		<div class="webequipe-pdf-dashboard-header-actions">
			<a href="<?php echo esc_url( $webequipe_pdf_settings_url ); ?>" class="button button-secondary">
				<?php esc_html_e( 'Settings', 'webequipe-pdf-search' ); ?>
			</a>
			<a href="<?php echo esc_url( $webequipe_pdf_help_url ); ?>" class="button button-secondary">
				<?php esc_html_e( 'Help', 'webequipe-pdf-search' ); ?>
			</a>
		</div>
	</div>

	<div class="webequipe-pdf-dashboard-banner webequipe-pdf-dashboard-banner--<?php echo esc_attr( $webequipe_pdf_dashboard['status_tone'] ); ?>">
		<div class="webequipe-pdf-dashboard-banner-main">
			<span class="dashicons dashicons-media-document webequipe-pdf-dashboard-banner-icon" aria-hidden="true"></span>
			<div>
				<h2 class="webequipe-pdf-dashboard-banner-title"><?php echo esc_html( $webequipe_pdf_dashboard['status_message'] ); ?></h2>
				<?php if ( ! empty( $webequipe_pdf_stats['last_index_date'] ) ) : ?>
					<p class="webequipe-pdf-dashboard-banner-meta">
						<?php
						printf(
							/* translators: %s: formatted date/time. */
							esc_html__( 'Last index: %s', 'webequipe-pdf-search' ),
							esc_html(
								date_i18n(
									get_option( 'date_format' ) . ' ' . get_option( 'time_format' ),
									strtotime( $webequipe_pdf_stats['last_index_date'] )
								)
							)
						);
						?>
					</p>
				<?php elseif ( (int) $webequipe_pdf_stats['total_indexed'] > 0 ) : ?>
					<p class="webequipe-pdf-dashboard-banner-meta">
						<?php
						printf(
							/* translators: %d: number of indexed PDF documents. */
							esc_html( _n( '%d PDF indexed', '%d PDFs indexed', (int) $webequipe_pdf_stats['total_indexed'], 'webequipe-pdf-search' ) ),
							(int) $webequipe_pdf_stats['total_indexed']
						);
						?>
					</p>
				<?php else : ?>
					<p class="webequipe-pdf-dashboard-banner-meta"><?php esc_html_e( 'No documents indexed yet.', 'webequipe-pdf-search' ); ?></p>
				<?php endif; ?>
				<div class="webequipe-pdf-dashboard-pills">
					<span class="webequipe-pdf-dashboard-pill webequipe-pdf-dashboard-pill--<?php echo $webequipe_pdf_dashboard['indexing_enabled'] ? 'ok' : 'warn'; ?>">
						<?php echo $webequipe_pdf_dashboard['indexing_enabled'] ? esc_html__( 'Indexing On', 'webequipe-pdf-search' ) : esc_html__( 'Indexing Off', 'webequipe-pdf-search' ); ?>
					</span>
					<span class="webequipe-pdf-dashboard-pill webequipe-pdf-dashboard-pill--<?php echo $webequipe_pdf_dashboard['cron_pending'] ? 'ok' : 'neutral'; ?>">
						<?php echo $webequipe_pdf_dashboard['cron_pending'] ? esc_html__( 'Cron Running', 'webequipe-pdf-search' ) : esc_html__( 'Cron Idle', 'webequipe-pdf-search' ); ?>
					</span>
					<?php if ( $webequipe_pdf_dashboard['failed_count'] > 0 ) : ?>
						<span class="webequipe-pdf-dashboard-pill webequipe-pdf-dashboard-pill--warn">
							<?php
							printf(
								/* translators: %d: number of failed documents. */
								esc_html( _n( '%d Failed Document', '%d Failed Documents', $webequipe_pdf_dashboard['failed_count'], 'webequipe-pdf-search' ) ),
								(int) $webequipe_pdf_dashboard['failed_count']
							);
							?>
						</span>
					<?php endif; ?>
				</div>
			</div>
		</div>
	</div>

	<div class="webequipe-pdf-dashboard-metrics">
		<div class="webequipe-pdf-dashboard-metric-card">
			<span class="webequipe-pdf-dashboard-metric-value"><?php echo esc_html( $webequipe_pdf_stats['total_indexed'] ); ?></span>
			<span class="webequipe-pdf-dashboard-metric-label"><?php esc_html_e( 'Indexed PDFs', 'webequipe-pdf-search' ); ?></span>
			<span class="webequipe-pdf-dashboard-metric-hint"><?php esc_html_e( 'Completed documents', 'webequipe-pdf-search' ); ?></span>
		</div>
		<div class="webequipe-pdf-dashboard-metric-card">
			<span class="webequipe-pdf-dashboard-metric-value"><?php echo esc_html( $webequipe_pdf_activity['total_pages'] ); ?></span>
			<span class="webequipe-pdf-dashboard-metric-label"><?php esc_html_e( 'Pages Indexed', 'webequipe-pdf-search' ); ?></span>
			<span class="webequipe-pdf-dashboard-metric-hint"><?php esc_html_e( 'Across your PDF library', 'webequipe-pdf-search' ); ?></span>
		</div>
		<div class="webequipe-pdf-dashboard-metric-card">
			<span class="webequipe-pdf-dashboard-metric-value"><?php echo esc_html( $webequipe_pdf_stats['percentage'] ); ?>%</span>
			<span class="webequipe-pdf-dashboard-metric-label"><?php esc_html_e( 'Index Coverage', 'webequipe-pdf-search' ); ?></span>
			<span class="webequipe-pdf-dashboard-metric-hint">
				<?php
				printf(
					/* translators: 1: indexed count, 2: library count. */
					esc_html__( '%1$d of %2$d library PDFs indexed', 'webequipe-pdf-search' ),
					(int) $webequipe_pdf_stats['total_indexed'],
					(int) $webequipe_pdf_stats['total_in_library']
				);
				?>
			</span>
		</div>
		<div class="webequipe-pdf-dashboard-metric-card">
			<span class="webequipe-pdf-dashboard-metric-value"><?php echo esc_html( $webequipe_pdf_dashboard['health_percent'] ); ?>%</span>
			<span class="webequipe-pdf-dashboard-metric-label"><?php esc_html_e( 'Search Health', 'webequipe-pdf-search' ); ?></span>
			<span class="webequipe-pdf-dashboard-metric-hint">
				<?php
				if ( $webequipe_pdf_dashboard['needs_attention'] > 0 ) {
					printf(
						/* translators: %d: number of documents needing attention. */
						esc_html( _n( '%d document needs attention', '%d documents need attention', $webequipe_pdf_dashboard['needs_attention'], 'webequipe-pdf-search' ) ),
						(int) $webequipe_pdf_dashboard['needs_attention']
					);
				} else {
					esc_html_e( 'All clear', 'webequipe-pdf-search' );
				}
				?>
			</span>
		</div>
	</div>

	<div class="webequipe-pdf-dashboard-grid">
		<div class="webequipe-pdf-dashboard-main">
			<div class="webequipe-pdf-dashboard-panel">
				<div class="webequipe-pdf-dashboard-panel-header">
					<h2><?php esc_html_e( 'Recent Index Activity', 'webequipe-pdf-search' ); ?></h2>
					<a href="<?php echo esc_url( $webequipe_pdf_activity_url ); ?>" class="webequipe-pdf-dashboard-panel-link">
						<?php esc_html_e( 'View All History', 'webequipe-pdf-search' ); ?> &rarr;
					</a>
				</div>

				<?php if ( ! empty( $webequipe_pdf_recent ) ) : ?>
					<table class="webequipe-pdf-dashboard-table widefat">
						<thead>
							<tr>
								<th class="col-document"><?php esc_html_e( 'Document', 'webequipe-pdf-search' ); ?></th>
								<th class="col-status"><?php esc_html_e( 'Status', 'webequipe-pdf-search' ); ?></th>
								<th class="col-pages"><?php esc_html_e( 'Pages', 'webequipe-pdf-search' ); ?></th>
								<th class="col-indexed"><?php esc_html_e( 'Indexed', 'webequipe-pdf-search' ); ?></th>
								<th class="col-action"><?php esc_html_e( 'Action', 'webequipe-pdf-search' ); ?></th>
							</tr>
						</thead>
						<tbody>
							<?php foreach ( $webequipe_pdf_recent as $webequipe_pdf_row ) : ?>
								<tr>
									<td class="col-document">
										<div class="webequipe-pdf-dashboard-doc">
											<span class="dashicons dashicons-pdf webequipe-pdf-dashboard-doc-icon" aria-hidden="true"></span>
											<div>
												<span class="webequipe-pdf-dashboard-doc-title"><?php echo esc_html( $webequipe_pdf_row['title'] ? $webequipe_pdf_row['title'] : $webequipe_pdf_row['filename'] ); ?></span>
												<span class="webequipe-pdf-dashboard-doc-meta">
													<?php
													printf(
														/* translators: %d: attachment ID. */
														esc_html__( 'Media ID: %d', 'webequipe-pdf-search' ),
														(int) $webequipe_pdf_row['post_id']
													);
													?>
												</span>
											</div>
										</div>
									</td>
									<td class="col-status">
										<?php webequipe_pdf_dashboard_status_pill( $webequipe_pdf_row['index_status'] ); ?>
									</td>
									<td class="col-pages"><?php echo esc_html( (int) $webequipe_pdf_row['page_count'] ); ?></td>
									<td class="col-indexed"><?php echo esc_html( webequipe_pdf_dashboard_format_time( $webequipe_pdf_row['display_date'] ) ); ?></td>
									<td class="col-action">
										<a href="<?php echo esc_url( $webequipe_pdf_row['edit_link'] ); ?>" class="webequipe-pdf-dashboard-view-link">
											<?php esc_html_e( 'View', 'webequipe-pdf-search' ); ?>
										</a>
									</td>
								</tr>
							<?php endforeach; ?>
						</tbody>
					</table>
				<?php else : ?>
					<p class="webequipe-pdf-dashboard-empty"><?php esc_html_e( 'No indexing activity yet. Upload a PDF or run Re-index All to get started.', 'webequipe-pdf-search' ); ?></p>
				<?php endif; ?>
			</div>

			<div class="webequipe-pdf-dashboard-panel webequipe-pdf-dashboard-shortcodes">
				<h2><?php esc_html_e( 'Search Shortcode', 'webequipe-pdf-search' ); ?></h2>
				<div class="webequipe-pdf-dashboard-shortcode-block">
					<label><?php esc_html_e( 'Main Shortcode', 'webequipe-pdf-search' ); ?></label>
					<div class="webequipe-pdf-dashboard-shortcode-row">
						<code id="webequipe-pdf-shortcode"><?php echo esc_html( $webequipe_pdf_shortcode_main ); ?></code>
						<button type="button" class="button button-small copy-shortcode" data-shortcode="<?php echo esc_attr( $webequipe_pdf_shortcode_main ); ?>">
							<?php esc_html_e( 'Copy', 'webequipe-pdf-search' ); ?>
						</button>
					</div>
				</div>
				<div class="webequipe-pdf-dashboard-shortcode-block">
					<label><?php esc_html_e( 'Full Customization Example', 'webequipe-pdf-search' ); ?></label>
					<div class="webequipe-pdf-dashboard-shortcode-row">
						<code><?php echo esc_html( $webequipe_pdf_shortcode_full ); ?></code>
						<button type="button" class="button button-small copy-shortcode" data-shortcode="<?php echo esc_attr( $webequipe_pdf_shortcode_full ); ?>">
							<?php esc_html_e( 'Copy', 'webequipe-pdf-search' ); ?>
						</button>
					</div>
				</div>
				<p class="webequipe-pdf-dashboard-shortcode-help">
					<a href="<?php echo esc_url( $webequipe_pdf_help_url . '#shortcode-section' ); ?>">
						<?php esc_html_e( 'View all shortcode options', 'webequipe-pdf-search' ); ?> &rarr;
					</a>
				</p>
			</div>
		</div>

		<aside class="webequipe-pdf-dashboard-sidebar">
			<div class="webequipe-pdf-dashboard-panel">
				<h2><?php esc_html_e( 'System Health', 'webequipe-pdf-search' ); ?></h2>
				<ul class="webequipe-pdf-dashboard-health-list">
					<li>
						<span class="webequipe-pdf-dashboard-health-label"><?php esc_html_e( 'Search Engine', 'webequipe-pdf-search' ); ?></span>
						<span class="webequipe-pdf-dashboard-health-value webequipe-pdf-dashboard-health-value--<?php echo $webequipe_pdf_dashboard['search_enabled'] ? 'ok' : 'bad'; ?>">
							<?php echo $webequipe_pdf_dashboard['search_enabled'] ? esc_html__( 'Enabled', 'webequipe-pdf-search' ) : esc_html__( 'Disabled', 'webequipe-pdf-search' ); ?>
						</span>
					</li>
					<li>
						<span class="webequipe-pdf-dashboard-health-label"><?php esc_html_e( 'Indexing', 'webequipe-pdf-search' ); ?></span>
						<span class="webequipe-pdf-dashboard-health-value webequipe-pdf-dashboard-health-value--<?php echo $webequipe_pdf_dashboard['indexing_enabled'] ? 'ok' : 'bad'; ?>">
							<?php echo $webequipe_pdf_dashboard['indexing_enabled'] ? esc_html__( 'Enabled', 'webequipe-pdf-search' ) : esc_html__( 'Disabled', 'webequipe-pdf-search' ); ?>
						</span>
					</li>
					<li>
						<span class="webequipe-pdf-dashboard-health-label"><?php esc_html_e( 'Background Indexing', 'webequipe-pdf-search' ); ?></span>
						<span class="webequipe-pdf-dashboard-health-value webequipe-pdf-dashboard-health-value--<?php echo $webequipe_pdf_dashboard['background_enabled'] ? 'ok' : 'neutral'; ?>">
							<?php echo $webequipe_pdf_dashboard['background_enabled'] ? esc_html__( 'Enabled', 'webequipe-pdf-search' ) : esc_html__( 'Disabled', 'webequipe-pdf-search' ); ?>
						</span>
					</li>
					<li>
						<span class="webequipe-pdf-dashboard-health-label"><?php esc_html_e( 'Index Queue', 'webequipe-pdf-search' ); ?></span>
						<span class="webequipe-pdf-dashboard-health-value webequipe-pdf-dashboard-health-value--<?php echo $webequipe_pdf_dashboard['processing_count'] > 0 ? 'warn' : 'ok'; ?>">
							<?php
							if ( $webequipe_pdf_dashboard['processing_count'] > 0 ) {
								printf(
									/* translators: %d: number of jobs in queue. */
									esc_html( _n( '%d processing', '%d processing', $webequipe_pdf_dashboard['processing_count'], 'webequipe-pdf-search' ) ),
									(int) $webequipe_pdf_dashboard['processing_count']
								);
							} else {
								esc_html_e( 'Empty', 'webequipe-pdf-search' );
							}
							?>
						</span>
					</li>
				</ul>
			</div>

			<?php if ( $webequipe_pdf_dashboard['failed_count'] > 0 ) : ?>
				<div class="webequipe-pdf-dashboard-panel webequipe-pdf-dashboard-attention">
					<h2><?php esc_html_e( 'Needs Attention', 'webequipe-pdf-search' ); ?></h2>
					<p>
						<?php
						printf(
							/* translators: %d: number of failed documents. */
							esc_html( _n( '%d failed document in your library.', '%d failed documents in your library.', $webequipe_pdf_dashboard['failed_count'], 'webequipe-pdf-search' ) ),
							(int) $webequipe_pdf_dashboard['failed_count']
						);
						?>
					</p>
					<?php if ( ! empty( $webequipe_pdf_dashboard['last_failed_message'] ) ) : ?>
						<p class="webequipe-pdf-dashboard-attention-error"><?php echo esc_html( $webequipe_pdf_dashboard['last_failed_message'] ); ?></p>
					<?php endif; ?>
					<p>
						<a href="<?php echo esc_url( $webequipe_pdf_manage_url ); ?>" class="button button-secondary">
							<?php esc_html_e( 'Manage PDFs', 'webequipe-pdf-search' ); ?>
						</a>
					</p>
				</div>
			<?php endif; ?>

			<div class="webequipe-pdf-dashboard-panel">
				<h2><?php esc_html_e( 'Quick Actions', 'webequipe-pdf-search' ); ?></h2>
				<div class="webequipe-pdf-dashboard-actions-grid">
					<a href="<?php echo esc_url( $webequipe_pdf_manage_url ); ?>" class="button button-secondary button-large">
						<?php esc_html_e( 'Manage PDFs', 'webequipe-pdf-search' ); ?>
					</a>
					<a href="<?php echo esc_url( $webequipe_pdf_settings_url ); ?>" class="button button-secondary button-large">
						<?php esc_html_e( 'Settings', 'webequipe-pdf-search' ); ?>
					</a>
					<a href="<?php echo esc_url( $webequipe_pdf_help_url ); ?>" class="button button-secondary button-large">
						<?php esc_html_e( 'Documentation', 'webequipe-pdf-search' ); ?>
					</a>
					<a href="<?php echo esc_url( $webequipe_pdf_activity_url ); ?>" class="button button-secondary button-large">
						<?php esc_html_e( 'Index Activity', 'webequipe-pdf-search' ); ?>
					</a>
					<button type="button" id="webequipe-pdf-reindex-all" class="button button-secondary button-large">
						<?php esc_html_e( 'Re-index All', 'webequipe-pdf-search' ); ?>
					</button>
					<button type="button" id="webequipe-pdf-clear-index" class="button button-secondary button-large webequipe-pdf-dashboard-btn-danger">
						<?php esc_html_e( 'Clear Index', 'webequipe-pdf-search' ); ?>
					</button>
				</div>
				<div id="webequipe-pdf-progress" class="webequipe-pdf-dashboard-progress" style="display: none;">
					<div class="progress-bar">
						<div class="progress-fill"></div>
					</div>
					<p class="progress-text"></p>
				</div>
			</div>
		</aside>
	</div>
</div>
