<?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', 'wp-powersuite' ); ?></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', 'wp-powersuite' ); ?></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', 'wp-powersuite' ); ?></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', 'wp-powersuite' ); ?></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.', 'wp-powersuite' ); ?>
			<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', 'wp-powersuite' ); ?></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', 'wp-powersuite' ); ?></h2>
					<p class="wpps-ai-alt-card-desc">
						<?php
						if ( $missing_count > 0 && $weak_count > 0 ) {
							printf(
								esc_html__( '%1$d images are missing alt text and %2$d have weak or short alt text. Use the Bulk Generate tab for live progress.', 'wp-powersuite' ),
								$missing_count,
								$weak_count
							);
						} elseif ( $missing_count > 0 ) {
							printf(
								esc_html(
									_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,
										'wp-powersuite'
									)
								),
								$missing_count
							);
						} else {
							printf(
								esc_html(
									_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,
										'wp-powersuite'
									)
								),
								$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;
						printf(
							esc_html(
								_n(
									'Generate alt text for %d image',
									'Generate alt text for %d images',
									$cta_count,
									'wp-powersuite'
								)
							),
							$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', 'wp-powersuite' ); ?></h2>
					<p class="wpps-ai-alt-card-desc">
						<?php
						printf(
							esc_html(
								_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,
									'wp-powersuite'
								)
							),
							$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', 'wp-powersuite' ); ?>
					</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', 'wp-powersuite' ); ?></h2>
	</div>
	<div class="wpps-ai-alt-card-body">
		<ul class="wpps-ai-alt-meta-list">
			<li><?php printf( esc_html__( 'Generations: %d', 'wp-powersuite' ), (int) $usage['generations'] ); ?></li>
			<?php if ( ! $hide_costs ) : ?>
				<li><?php printf( esc_html__( 'Estimated cost: $%s', 'wp-powersuite' ), esc_html( number_format( (float) $usage['estimated_cost'], 4 ) ) ); ?></li>
			<?php endif; ?>
			<?php if ( $free_limit > 0 ) : ?>
				<li><?php printf( esc_html__( 'Monthly limit: %d', 'wp-powersuite' ), $free_limit ); ?></li>
			<?php elseif ( $limit > 0 ) : ?>
				<li><?php printf( esc_html__( 'Monthly limit: %d', 'wp-powersuite' ), $limit ); ?></li>
			<?php endif; ?>
			<?php if ( (int) ( $queue['remaining'] ?? 0 ) > 0 || ( $queue['status'] ?? '' ) !== 'idle' ) : ?>
				<li><?php printf( esc_html__( 'Background queue: %1$d%% complete (%2$d remaining)', 'wp-powersuite' ), (int) ( $queue['percent'] ?? 0 ), (int) ( $queue['remaining'] ?? 0 ) ); ?></li>
			<?php endif; ?>
			<?php if ( $pending_count > 0 ) : ?>
				<li>
					<?php printf( esc_html__( 'Pending review: %d', 'wp-powersuite' ), $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', 'wp-powersuite' ); ?></a>
				</li>
			<?php endif; ?>
		</ul>
		<?php if ( ! $is_pro ) : ?>
			<p class="wpps-ai-alt-help">
				<?php
				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.', 'wp-powersuite' ),
						$free_limit,
						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', 'wp-powersuite' ); ?></h2>
			<p class="wpps-ai-alt-card-desc"><?php printf( esc_html__( '%d groups of images share the same alt text.', 'wp-powersuite' ), (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', 'wp-powersuite' ); ?></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', 'wp-powersuite' ); ?></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', 'wp-powersuite' ); ?>
				<span class="wpps-pro-badge"><?php esc_html_e( 'Premium', 'wp-powersuite' ); ?></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.', 'wp-powersuite' ),
						(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', 'wp-powersuite' ); ?></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.', 'wp-powersuite' ); ?></p>
			<a class="button button-primary" href="<?php echo esc_url( $missing_url ); ?>"><?php esc_html_e( 'Open missing images', 'wp-powersuite' ); ?></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', 'wp-powersuite' ); ?></a>
	<?php if ( ! empty( $summary['last_scan'] ) ) : ?>
		<span class="wpps-ai-alt-help"><?php printf( esc_html__( 'Last scan: %s UTC', 'wp-powersuite' ), 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)', 'wp-powersuite' ); ?></a>
	</p>
<?php endif; ?>
