<?php
/**
 * AI Alt Text unified admin page (dashboard + settings).
 *
 * @package AIAltTextGenerator
 */

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

use AIAltTextGenerator\Capabilities;
use AIAltTextGenerator\Module;
use AIAltTextGenerator\Scanner;
use AIAltTextGenerator\Settings;
use WP_PowerSuite\Admin\PageHero;

if ( ! Capabilities::canView() ) {
	wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'wppowersuite-lite' ) );
}

$tab = isset( $_GET['tab'] ) ? sanitize_key( wp_unslash( (string) $_GET['tab'] ) ) : 'dashboard';
$tab = Module::resolveTab( $tab );

$base_url       = admin_url( 'admin.php?page=' . Module::DASHBOARD_SLUG );
$summary        = Scanner::getSummary();
$pending_review = (int) ( $summary['pending_review'] ?? 0 );
$has_key        = Settings::hasApiKey();
?>
<div class="wrap wpps-ai-alt-wrap">
	<hr class="wp-header-end">

	<?php if ( isset( $_GET['settings_saved'] ) && $_GET['settings_saved'] === '1' ) : ?>
		<div class="notice notice-success is-dismissible"><p><?php esc_html_e( 'Settings saved.', 'wppowersuite-lite' ); ?></p></div>
	<?php endif; ?>

	<?php
	PageHero::render_for_module(
		[
			'icon'    => 'dashicons-format-image',
			'title'   => __( 'AI Alt Text', 'wppowersuite-lite' ),
			'tagline' => __( 'Generate accessible alt text with AI, review suggestions, and keep your media library optimized.', 'wppowersuite-lite' ),
			'stats'   => [
				[
					'value' => (string) ( $summary['total'] ?? 0 ),
					'label' => __( 'Images', 'wppowersuite-lite' ),
				],
				[
					'value' => (string) ( $summary['missing'] ?? 0 ),
					'label' => __( 'Missing alt', 'wppowersuite-lite' ),
				],
				[
					'value' => (string) ( $summary['weak'] ?? 0 ),
					'label' => __( 'Weak alt', 'wppowersuite-lite' ),
				],
				[
					'value' => (string) ( $summary['pending_review'] ?? 0 ),
					'label' => __( 'Pending review', 'wppowersuite-lite' ),
				],
			],
			'meta_html' => sprintf(
				'<a href="%1$s" class="button button-primary">%2$s</a> <a href="%3$s" class="button">%4$s</a>',
				esc_url( admin_url( 'upload.php' ) ),
				esc_html__( 'Media Library', 'wppowersuite-lite' ),
				esc_url( add_query_arg( [ 'tab' => 'dashboard', 'refresh' => '1' ], $base_url ) ),
				esc_html__( 'Refresh scan', 'wppowersuite-lite' )
			),
		]
	);
	?>

	<div class="wpps-ai-alt-notices" id="wpps-ai-alt-tab-notices">
		<?php if ( ! $has_key && $tab === 'dashboard' && Capabilities::canManage() ) : ?>
			<div class="wpps-ai-alt-notice wpps-ai-alt-notice--warning" data-wpps-ai-alt-notice="api-key">
				<p>
					<?php esc_html_e( 'Connect your AI provider to start generating alt text.', 'wppowersuite-lite' ); ?>
					<a class="wpps-ai-alt-tab-link" data-tab="settings" href="<?php echo esc_url( add_query_arg( 'tab', 'settings', $base_url ) ); ?>"><?php esc_html_e( 'Open Settings', 'wppowersuite-lite' ); ?></a>
				</p>
			</div>
		<?php endif; ?>
	</div>

	<nav class="wpps-ai-alt-tabs" aria-label="<?php esc_attr_e( 'AI Alt Text sections', 'wppowersuite-lite' ); ?>">
		<a href="<?php echo esc_url( add_query_arg( 'tab', 'dashboard', $base_url ) ); ?>" class="wpps-ai-alt-tab<?php echo $tab === 'dashboard' ? ' is-active' : ''; ?>" data-tab="dashboard"><?php esc_html_e( 'Dashboard', 'wppowersuite-lite' ); ?></a>
		<?php if ( Capabilities::canGenerate() && $has_key ) : ?>
			<a href="<?php echo esc_url( add_query_arg( 'tab', 'bulk', $base_url ) ); ?>" class="wpps-ai-alt-tab<?php echo $tab === 'bulk' ? ' is-active' : ''; ?>" data-tab="bulk"><?php esc_html_e( 'Bulk Generate', 'wppowersuite-lite' ); ?></a>
			<a href="<?php echo esc_url( add_query_arg( 'tab', 'review', $base_url ) ); ?>" class="wpps-ai-alt-tab<?php echo $tab === 'review' ? ' is-active' : ''; ?>" data-tab="review">
				<?php esc_html_e( 'Review', 'wppowersuite-lite' ); ?>
				<span class="wpps-ai-alt-tab-badge" id="wpps-ai-alt-review-badge"<?php echo $pending_review <= 0 ? ' hidden' : ''; ?>><?php echo esc_html( (string) $pending_review ); ?></span>
			</a>
		<?php endif; ?>
		<?php if ( Capabilities::canView() ) : ?>
			<a href="<?php echo esc_url( add_query_arg( 'tab', 'history', $base_url ) ); ?>" class="wpps-ai-alt-tab<?php echo $tab === 'history' ? ' is-active' : ''; ?>" data-tab="history"><?php esc_html_e( 'History', 'wppowersuite-lite' ); ?></a>
		<?php endif; ?>
		<?php if ( Capabilities::canManage() ) : ?>
			<a href="<?php echo esc_url( add_query_arg( 'tab', 'settings', $base_url ) ); ?>" class="wpps-ai-alt-tab<?php echo $tab === 'settings' ? ' is-active' : ''; ?>" data-tab="settings"><?php esc_html_e( 'Settings', 'wppowersuite-lite' ); ?></a>
		<?php endif; ?>
	</nav>

	<div class="wpps-ai-alt-tab-panel" id="wpps-ai-alt-tab-panel" data-current-tab="<?php echo esc_attr( $tab ); ?>">
		<?php echo Module::renderTabContent( $tab ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- escaped in partials. ?>
	</div>
</div>
