<?php
/**
 * AI Alt Text dashboard tab content.
 *
 * @package AIAltTextGenerator
 */

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

use AIAltTextGenerator\Capabilities;
use AIAltTextGenerator\FeatureGate;
use AIAltTextGenerator\Queue;
use AIAltTextGenerator\Scanner;
use AIAltTextGenerator\Settings;
use AIAltTextGenerator\Usage;

require_once __DIR__ . '/helpers.php';

$refresh       = isset( $_GET['refresh'] ) && $_GET['refresh'] === '1';
$summary       = Scanner::getSummary( $refresh, $refresh );
$usage         = Usage::getSummary();
$queue         = Queue::getStatus();
$pending_count = (int) ( $summary['pending_review'] ?? 0 );
$settings      = Settings::get();
$limit         = (int) ( $settings['monthly_limit'] ?? 0 );
$hide_costs    = FeatureGate::shouldHideCosts();
$is_pro        = FeatureGate::isPro();
$free_limit    = FeatureGate::effectiveMonthlyLimit( $settings );
$page_base     = admin_url( 'admin.php?page=' . \AIAltTextGenerator\Module::DASHBOARD_SLUG );
$missing_count = (int) ( $summary['missing'] ?? 0 );
$weak_count    = (int) ( $summary['weak'] ?? 0 );
$missing_url   = admin_url( 'upload.php?wpps_ai_alt_filter=missing' );
$has_key       = Settings::hasApiKey();
?>

<div class="wpps-ai-alt-stats wpps-ai-alt-stats--dashboard" id="wpps-ai-alt-library-stats">
	<div class="wpps-ai-alt-stat-card">
		<span class="wpps-ai-alt-stat-label"><?php esc_html_e( 'Total images', 'wppowersuite-lite' ); ?></span>
		<span class="wpps-ai-alt-stat-value" id="wpps-ai-alt-stat-total"><?php echo esc_html( (string) ( $summary['total'] ?? 0 ) ); ?></span>
	</div>
	<div class="wpps-ai-alt-stat-card is-warning">
		<span class="wpps-ai-alt-stat-label"><?php esc_html_e( 'Missing alt', 'wppowersuite-lite' ); ?></span>
		<span class="wpps-ai-alt-stat-value" id="wpps-ai-alt-stat-missing"><?php echo esc_html( (string) $missing_count ); ?></span>
	</div>
	<div class="wpps-ai-alt-stat-card is-warning">
		<span class="wpps-ai-alt-stat-label"><?php esc_html_e( 'Weak / short alt', 'wppowersuite-lite' ); ?></span>
		<span class="wpps-ai-alt-stat-value" id="wpps-ai-alt-stat-weak"><?php echo esc_html( (string) ( $summary['weak'] ?? 0 ) ); ?></span>
	</div>
	<div class="wpps-ai-alt-stat-card is-success">
		<span class="wpps-ai-alt-stat-label"><?php esc_html_e( 'With alt text', 'wppowersuite-lite' ); ?></span>
		<span class="wpps-ai-alt-stat-value" id="wpps-ai-alt-stat-optimized"><?php echo esc_html( (string) ( $summary['optimized'] ?? 0 ) ); ?></span>
	</div>
</div>

<?php if ( ! $has_key && Capabilities::canManage() ) : ?>
	<div class="wpps-ai-alt-card wpps-ai-alt-setup-notice">
		<p class="wpps-ai-alt-card-desc">
			<?php esc_html_e( 'Add an AI provider API key in Settings to enable generation.', 'wppowersuite-lite' ); ?>
			<a class="wpps-ai-alt-tab-link" data-tab="settings" href="<?php echo esc_url( add_query_arg( 'tab', 'settings', $page_base ) ); ?>"><?php esc_html_e( 'Open Settings', 'wppowersuite-lite' ); ?></a>
		</p>
	</div>
<?php endif; ?>

<?php if ( Capabilities::canGenerate() && $has_key && ( $missing_count > 0 || $weak_count > 0 ) ) : ?>
	<div class="wpps-ai-alt-card wpps-ai-alt-bulk-cta">
		<div class="wpps-ai-alt-card-body">
			<div class="wpps-ai-alt-bulk-cta-inner">
				<div class="wpps-ai-alt-bulk-cta-copy">
					<h2 class="wpps-ai-alt-bulk-cta-title"><?php esc_html_e( 'Bulk generate alt text', 'wppowersuite-lite' ); ?></h2>
					<p class="wpps-ai-alt-card-desc">
						<?php
						if ( $missing_count > 0 && $weak_count > 0 ) {
							echo esc_html(
								sprintf(
									/* translators: 1: missing alt count, 2: weak alt count */
									__( '%1$d images are missing alt text and %2$d have weak or short alt text. Use the Bulk Generate tab for live progress.', 'wppowersuite-lite' ),
									$missing_count,
									$weak_count
								)
							);
						} elseif ( $missing_count > 0 ) {
							echo esc_html(
								(string) sprintf(
									/* translators: %d: number of images missing alt text. */
									_n(
										'%d image is missing alt text. Use the Bulk Generate tab for live progress.',
										'%d images are missing alt text. Use the Bulk Generate tab for live progress.',
										$missing_count,
										'wppowersuite-lite'
									),
									$missing_count
								)
							);
						} else {
							echo esc_html(
								(string) sprintf(
									/* translators: %d: number of images with weak or short alt text. */
									_n(
										'%d image has weak or short alt text. Use the Bulk Generate tab to improve it.',
										'%d images have weak or short alt text. Use the Bulk Generate tab to improve them.',
										$weak_count,
										'wppowersuite-lite'
									),
									$weak_count
								)
							);
						}
						?>
					</p>
				</div>
				<div class="wpps-ai-alt-bulk-cta-actions">
					<a class="button button-primary button-hero wpps-ai-alt-tab-link" data-tab="bulk" href="<?php echo esc_url( add_query_arg( 'tab', 'bulk', $page_base ) ); ?>">
						<?php
						$cta_count = $missing_count + $weak_count;
						echo esc_html(
							(string) sprintf(
								/* translators: %d: number of images to generate alt text for. */
								_n(
									'Generate alt text for %d image',
									'Generate alt text for %d images',
									$cta_count,
									'wppowersuite-lite'
								),
								$cta_count
							)
						);
						?>
					</a>
				</div>
			</div>
		</div>
	</div>
<?php endif; ?>

<?php if ( Capabilities::canGenerate() && $pending_count > 0 ) : ?>
	<div class="wpps-ai-alt-card wpps-ai-alt-review-cta">
		<div class="wpps-ai-alt-card-body">
			<div class="wpps-ai-alt-bulk-cta-inner">
				<div class="wpps-ai-alt-bulk-cta-copy">
					<h2 class="wpps-ai-alt-bulk-cta-title"><?php esc_html_e( 'Review AI suggestions', 'wppowersuite-lite' ); ?></h2>
					<p class="wpps-ai-alt-card-desc">
						<?php
						echo esc_html(
							(string) sprintf(
								/* translators: %d: number of images waiting for review. */
								_n(
									'%d image is waiting for review. Approve, edit, or reject suggestions on the Review tab.',
									'%d images are waiting for review. Approve, edit, or reject suggestions on the Review tab.',
									$pending_count,
									'wppowersuite-lite'
								),
								$pending_count
							)
						);
						?>
					</p>
				</div>
				<div class="wpps-ai-alt-bulk-cta-actions">
					<a class="button button-primary button-hero wpps-ai-alt-tab-link" data-tab="review" href="<?php echo esc_url( add_query_arg( 'tab', 'review', $page_base ) ); ?>">
						<?php esc_html_e( 'Open Review', 'wppowersuite-lite' ); ?>
					</a>
				</div>
			</div>
		</div>
	</div>
<?php endif; ?>

<div class="wpps-ai-alt-card">
	<div class="wpps-ai-alt-card-head">
		<h2 class="wpps-ai-alt-card-title"><?php esc_html_e( 'Usage this month', 'wppowersuite-lite' ); ?></h2>
	</div>
	<div class="wpps-ai-alt-card-body">
		<ul class="wpps-ai-alt-meta-list">
			<?php /* translators: %d: number value. */ ?>
			<li><?php printf( esc_html__( 'Generations: %d', 'wppowersuite-lite' ), (int) $usage['generations'] ); ?></li>
			<?php if ( ! $hide_costs ) : ?>
				<?php /* translators: %s: string value. */ ?>
				<li><?php printf( esc_html__( 'Estimated cost: $%s', 'wppowersuite-lite' ), esc_html( number_format( (float) $usage['estimated_cost'], 4 ) ) ); ?></li>
			<?php endif; ?>
			<?php if ( $free_limit > 0 ) : ?>
				<?php /* translators: %d: number value. */ ?>
				<li><?php echo esc_html( sprintf( __( 'Monthly limit: %d', 'wppowersuite-lite' ), $free_limit ) ); ?></li>
			<?php elseif ( $limit > 0 ) : ?>
				<?php /* translators: %d: number value. */ ?>
				<li><?php echo esc_html( sprintf( __( 'Monthly limit: %d', 'wppowersuite-lite' ), $limit ) ); ?></li>
			<?php endif; ?>
			<?php if ( (int) ( $queue['remaining'] ?? 0 ) > 0 || ( $queue['status'] ?? '' ) !== 'idle' ) : ?>
				<?php /* translators: 1: number, 2: number. */ ?>
				<li><?php printf( esc_html__( 'Background queue: %1$d%% complete (%2$d remaining)', 'wppowersuite-lite' ), (int) ( $queue['percent'] ?? 0 ), (int) ( $queue['remaining'] ?? 0 ) ); ?></li>
			<?php endif; ?>
			<?php if ( $pending_count > 0 ) : ?>
				<li>
					<?php /* translators: %d: number value. */ ?>
					<?php echo esc_html( sprintf( __( 'Pending review: %d', 'wppowersuite-lite' ), $pending_count ) ); ?>
					<a class="wpps-ai-alt-tab-link" data-tab="review" href="<?php echo esc_url( add_query_arg( 'tab', 'review', $page_base ) ); ?>"><?php esc_html_e( 'Review now', 'wppowersuite-lite' ); ?></a>
				</li>
			<?php endif; ?>
		</ul>
		<?php if ( ! $is_pro ) : ?>
			<p class="wpps-ai-alt-help">
				<?php
				if ( $free_limit > 0 ) {
					echo wp_kses_post(
						sprintf(
							/* translators: 1: free monthly limit, 2: URL to license page */
							__( 'Free plan: OpenAI budget models and %1$d generations per month. <a href="%2$s">Activate your license</a> for Gemini, Claude, premium models, and automation.', 'wppowersuite-lite' ),
							$free_limit,
							esc_url( admin_url( 'admin.php?page=powersuite-license' ) )
						)
					);
				} else {
					echo wp_kses_post(
						sprintf(
							/* translators: %s: URL to license page */
							__( 'This plugin includes OpenAI budget models. <a href="%s">Activate your license</a> for Gemini, Claude, additional models, and automation.', 'wppowersuite-lite' ),
							esc_url( admin_url( 'admin.php?page=powersuite-license' ) )
						)
					);
				}
				?>
			</p>
		<?php endif; ?>
	</div>
</div>

<?php if ( Capabilities::canGenerate() && (int) ( $summary['duplicates'] ?? 0 ) > 0 && FeatureGate::featureEnabled( 'bulk_duplicates' ) && $refresh ) : ?>
	<div class="wpps-ai-alt-card">
		<div class="wpps-ai-alt-card-head">
			<h2 class="wpps-ai-alt-card-title"><?php esc_html_e( 'Duplicate alt text', 'wppowersuite-lite' ); ?></h2>
			<?php /* translators: %d: number value. */ ?>
			<p class="wpps-ai-alt-card-desc"><?php printf( esc_html__( '%d groups of images share the same alt text.', 'wppowersuite-lite' ), (int) ( $summary['duplicates'] ?? 0 ) ); ?></p>
		</div>
		<div class="wpps-ai-alt-card-body wpps-ai-alt-actions">
			<button type="button" class="button" id="wpps-ai-alt-regenerate-duplicates"><?php esc_html_e( 'Regenerate duplicates', 'wppowersuite-lite' ); ?></button>
			<a class="button" href="<?php echo esc_url( admin_url( 'upload.php?wpps_ai_alt_filter=duplicates' ) ); ?>"><?php esc_html_e( 'View in Media Library', 'wppowersuite-lite' ); ?></a>
		</div>
	</div>
<?php elseif ( Capabilities::canGenerate() && (int) ( $summary['duplicates'] ?? 0 ) > 0 && ! FeatureGate::featureEnabled( 'bulk_duplicates' ) && $refresh ) : ?>
	<div class="wpps-ai-alt-card">
		<div class="wpps-ai-alt-card-head">
			<h2 class="wpps-ai-alt-card-title">
				<?php esc_html_e( 'Duplicate alt text', 'wppowersuite-lite' ); ?>
				<span class="wpps-pro-badge"><?php esc_html_e( 'Premium', 'wppowersuite-lite' ); ?></span>
			</h2>
			<p class="wpps-ai-alt-card-desc">
				<?php
				echo wp_kses_post(
					sprintf(
						/* translators: 1: duplicate group count, 2: URL to license page */
						__( '%1$d groups of images share the same alt text. Bulk regenerate duplicates is a Premium feature. <a href="%2$s">Activate your license</a> to fix them in one pass.', 'wppowersuite-lite' ),
						(int) ( $summary['duplicates'] ?? 0 ),
						esc_url( admin_url( 'admin.php?page=powersuite-license' ) )
					)
				);
				?>
			</p>
		</div>
		<div class="wpps-ai-alt-card-body wpps-ai-alt-actions">
			<a class="button" href="<?php echo esc_url( admin_url( 'upload.php?wpps_ai_alt_filter=duplicates' ) ); ?>"><?php esc_html_e( 'View in Media Library', 'wppowersuite-lite' ); ?></a>
		</div>
	</div>
<?php endif; ?>

<?php if ( $missing_count > 0 && ! Capabilities::canGenerate() ) : ?>
	<div class="wpps-ai-alt-card">
		<div class="wpps-ai-alt-card-body">
			<p class="wpps-ai-alt-card-desc"><?php esc_html_e( 'Images in your library are missing alt text.', 'wppowersuite-lite' ); ?></p>
			<a class="button button-primary" href="<?php echo esc_url( $missing_url ); ?>"><?php esc_html_e( 'Open missing images', 'wppowersuite-lite' ); ?></a>
		</div>
	</div>
<?php endif; ?>

<div class="wpps-ai-alt-toolbar">
	<a class="button wpps-ai-alt-tab-link" data-tab="history" href="<?php echo esc_url( add_query_arg( 'tab', 'history', $page_base ) ); ?>"><?php esc_html_e( 'View history', 'wppowersuite-lite' ); ?></a>
	<?php if ( ! empty( $summary['last_scan'] ) ) : ?>
		<?php /* translators: %s: string value. */ ?>
		<span class="wpps-ai-alt-help"><?php printf( esc_html__( 'Last scan: %s UTC', 'wppowersuite-lite' ), esc_html( (string) $summary['last_scan'] ) ); ?></span>
	<?php endif; ?>
</div>

<?php if ( Capabilities::canManage() ) : ?>
	<p class="wpps-ai-alt-help wpps-ai-alt-dashboard-refresh">
		<a href="<?php echo esc_url( add_query_arg( [ 'tab' => 'dashboard', 'refresh' => '1' ], $page_base ) ); ?>"><?php esc_html_e( 'Run full library scan (includes duplicates)', 'wppowersuite-lite' ); ?></a>
	</p>
<?php endif; ?>

