<?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.', 'wp-powersuite' ) );
}

$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.', 'wp-powersuite' ); ?></p></div>
	<?php endif; ?>

	<?php
	PageHero::render_for_module(
		[
			'icon'    => 'dashicons-format-image',
			'title'   => __( 'AI Alt Text', 'wp-powersuite' ),
			'tagline' => __( 'Generate accessible alt text with AI, review suggestions, and keep your media library optimized.', 'wp-powersuite' ),
			'stats'   => [
				[
					'value' => (string) ( $summary['total'] ?? 0 ),
					'label' => __( 'Images', 'wp-powersuite' ),
				],
				[
					'value' => (string) ( $summary['missing'] ?? 0 ),
					'label' => __( 'Missing alt', 'wp-powersuite' ),
				],
				[
					'value' => (string) ( $summary['weak'] ?? 0 ),
					'label' => __( 'Weak alt', 'wp-powersuite' ),
				],
				[
					'value' => (string) ( $summary['pending_review'] ?? 0 ),
					'label' => __( 'Pending review', 'wp-powersuite' ),
				],
			],
			'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', 'wp-powersuite' ),
				esc_url( add_query_arg( [ 'tab' => 'dashboard', 'refresh' => '1' ], $base_url ) ),
				esc_html__( 'Refresh scan', 'wp-powersuite' )
			),
		]
	);
	?>

	<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.', 'wp-powersuite' ); ?>
					<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', 'wp-powersuite' ); ?></a>
				</p>
			</div>
		<?php endif; ?>
	</div>

	<nav class="wpps-ai-alt-tabs" aria-label="<?php esc_attr_e( 'AI Alt Text sections', 'wp-powersuite' ); ?>">
		<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', 'wp-powersuite' ); ?></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', 'wp-powersuite' ); ?></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', 'wp-powersuite' ); ?>
				<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', 'wp-powersuite' ); ?></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', 'wp-powersuite' ); ?></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>
