<?php
/**
 * AI Alt Text settings form (embedded in dashboard tab).
 *
 * @package AIAltTextGenerator
 */

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

use AIAltTextGenerator\Capabilities;
use AIAltTextGenerator\FeatureGate;
use AIAltTextGenerator\ImageFilter;
use AIAltTextGenerator\Languages;
use AIAltTextGenerator\Logger;
use AIAltTextGenerator\Models;
use AIAltTextGenerator\PromptStyles;
use AIAltTextGenerator\ProviderModels;
use AIAltTextGenerator\ProviderRegistry;
use AIAltTextGenerator\Settings;

require_once __DIR__ . '/helpers.php';
require_once dirname( __DIR__, 2 ) . '/includes/dependencies.php';

if ( ! Capabilities::canManage() ) {
	return;
}

$module_id = 'ai-alt-text-generator';
$settings  = Settings::get();
$provider  = (string) ( $settings['provider'] ?? 'openai' );
$model     = ProviderModels::sanitize( (string) ( $settings['model'] ?? Models::DEFAULT ), $provider, $settings );
$is_pro    = FeatureGate::isPro();
$has_woo   = class_exists( 'WooCommerce' );
$license_page_url = admin_url( 'admin.php?page=powersuite-license' );
$free_monthly_limit = FeatureGate::effectiveMonthlyLimit( $settings );

$providers = ProviderRegistry::labels();
$all_model_groups = [];
foreach ( array_keys( $providers ) as $provider_id ) {
	$all_model_groups[ $provider_id ] = ProviderModels::getGroups( $provider_id, $settings );
}
$model_groups = $all_model_groups[ $provider ] ?? [];

$prompt_style_groups  = PromptStyles::groupedLabels();
$current_prompt_style = PromptStyles::sanitize( (string) ( $settings['prompt_style'] ?? 'concise' ) );

$role_caps = Capabilities::normalizeRoleCaps( $settings['role_caps'] ?? [] );
$wp_roles  = wp_roles()->get_names();
$admin_only_access = ! empty( $settings['admin_only_access'] );
$recent_errors = array_values(
	array_filter(
		Logger::getRecent( 10 ),
		static fn( array $row ): bool => (string) ( $row['status'] ?? '' ) === 'failed'
	)
);
$configured_providers = Settings::getConfiguredProviders();
$global_settings_url  = admin_url( 'admin.php?page=powersuite-settings&tab=ai-connections' );
$selected_has_key     = Settings::hasApiKey( $provider );

$excluded_post_types  = ImageFilter::excludedPostTypes( $settings );
$available_post_types = ImageFilter::availablePostTypes();
?>
<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" class="wpps-ai-alt-settings-form" id="wpps-ai-alt-settings-form">
	<?php wp_nonce_field( 'wpps_ai_alt_save_settings' ); ?>
	<input type="hidden" name="action" value="wpps_ai_alt_save_settings" />

	<p class="wpps-ai-alt-settings-intro"><?php esc_html_e( 'Choose which connected AI provider powers alt text generation, then configure writing style and automation. API keys are managed under Settings → AI Connections.', 'wppowersuite-lite' ); ?></p>

	<div class="wpps-ai-alt-accordions">
		<?php
		wpps_ai_alt_accordion_open(
			[
				'id'    => 'connection',
				'title' => __( 'AI provider', 'wppowersuite-lite' ),
				'tip'   => __( 'Pick one of the AI providers configured in PowerSuite Settings. Add or update keys under Settings → AI Connections.', 'wppowersuite-lite' ),
				'open'  => true,
			]
		);
		?>
		<div class="wpps-ai-alt-field-full">
			<label class="wpps-ai-alt-field-label" for="<?php echo esc_attr( $module_id ); ?>_provider"><?php esc_html_e( 'Provider', 'wppowersuite-lite' ); ?></label>
			<div class="wpps-ai-alt-field-control wpps-ai-alt-field-control--narrow">
				<select id="<?php echo esc_attr( $module_id ); ?>_provider" name="<?php echo esc_attr( $module_id ); ?>[provider]" class="wpps-ai-alt-input">
					<?php foreach ( $providers as $id => $label ) : ?>
						<?php
						$locked      = ! FeatureGate::isProviderAllowed( $id );
						$has_key     = in_array( $id, $configured_providers, true );
						$option_label = $label;
						if ( $locked ) {
							$option_label .= ' (Pro)';
						} elseif ( ! $has_key ) {
							$option_label .= ' (' . __( 'not connected', 'wppowersuite-lite' ) . ')';
						}
						?>
						<option value="<?php echo esc_attr( $id ); ?>" <?php selected( $provider, $id ); ?> <?php disabled( ( $locked && $id !== $provider ) || ( ! $has_key && ! $locked ) ); ?>><?php echo esc_html( $option_label ); ?></option>
					<?php endforeach; ?>
				</select>
			</div>
			<span class="wpps-ai-alt-help">
				<?php
				printf(
					/* translators: %s: link to AI Connections settings */
					esc_html__( 'Manage API keys in %s.', 'wppowersuite-lite' ),
					'<a href="' . esc_url( $global_settings_url ) . '">' . esc_html__( 'Settings → AI Connections', 'wppowersuite-lite' ) . '</a>'
				);
				?>
			</span>
		</div>

		<?php if ( $configured_providers === [] ) : ?>
			<div class="notice notice-warning inline wpps-ai-alt-global-connection-notice">
				<p>
					<?php
					printf(
						/* translators: %s: link to AI Connections settings */
						esc_html__( 'No AI providers are connected yet. Add at least one API key in %s before generating alt text.', 'wppowersuite-lite' ),
						'<a href="' . esc_url( $global_settings_url ) . '">' . esc_html__( 'Settings → AI Connections', 'wppowersuite-lite' ) . '</a>'
					);
					?>
				</p>
			</div>
		<?php elseif ( ! $selected_has_key ) : ?>
			<div class="notice notice-warning inline wpps-ai-alt-global-connection-notice">
				<p>
					<?php
					printf(
						/* translators: %s: link to AI Connections settings */
						esc_html__( 'The selected provider is not connected. Choose a connected provider or add its key in %s.', 'wppowersuite-lite' ),
						'<a href="' . esc_url( $global_settings_url ) . '">' . esc_html__( 'Settings → AI Connections', 'wppowersuite-lite' ) . '</a>'
					);
					?>
				</p>
			</div>
		<?php else : ?>
			<p class="wpps-ai-alt-help wpps-ai-alt-connection-status">
				<?php
				printf(
					/* translators: %s: provider label */
					esc_html__( '%s is connected and ready.', 'wppowersuite-lite' ),
					esc_html( $providers[ $provider ] ?? $provider )
				);
				?>
			</p>
		<?php endif; ?>

		<?php if ( ! $is_pro ) : ?>
			<p class="wpps-ai-alt-help wpps-ai-alt-provider-pro-note">
				<?php
				if ( $free_monthly_limit > 0 ) {
					echo wp_kses_post(
						sprintf(
							/* translators: 1: monthly generation cap, 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, scheduled reports, and more.', 'wppowersuite-lite' ),
							$free_monthly_limit,
							esc_url( $license_page_url )
						)
					);
				} 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, scheduled reports, and more.', 'wppowersuite-lite' ),
							esc_url( $license_page_url )
						)
					);
				}
				?>
			</p>
		<?php endif; ?>
		<div class="wpps-ai-alt-test-row">
			<button type="button" class="button" id="<?php echo esc_attr( $module_id ); ?>_test_connection" <?php disabled( ! $selected_has_key ); ?>><?php esc_html_e( 'Test connection', 'wppowersuite-lite' ); ?></button>
			<span id="<?php echo esc_attr( $module_id ); ?>_test_result" class="wpps-ai-alt-test-result"></span>
		</div>
		<p class="wpps-ai-alt-help wpps-ai-alt-privacy-note"><?php esc_html_e( 'Images and filenames may be sent to your AI provider. Only use images you are allowed to share externally.', 'wppowersuite-lite' ); ?></p>
		<?php wpps_ai_alt_accordion_close(); ?>

		<?php
		wpps_ai_alt_accordion_open(
			[
				'id'    => 'generation',
				'title' => __( 'Generation & automation', 'wppowersuite-lite' ),
				'tip'   => __( 'Model, writing style, custom prompt, output options, and automation.', 'wppowersuite-lite' ),
				'open'  => true,
			]
		);
		?>
		<div class="wpps-ai-alt-settings-group">
			<h3 class="wpps-ai-alt-settings-group-title"><?php esc_html_e( 'Writing', 'wppowersuite-lite' ); ?></h3>
		<div class="wpps-ai-alt-field-pair wpps-ai-alt-field-pair--has-both-help">
			<label class="wpps-ai-alt-field-label wpps-ai-alt-field-label--left" for="<?php echo esc_attr( $module_id ); ?>_model"><?php esc_html_e( 'Model', 'wppowersuite-lite' ); ?></label>
			<label class="wpps-ai-alt-field-label wpps-ai-alt-field-label--right" for="<?php echo esc_attr( $module_id ); ?>_prompt_style"><?php esc_html_e( 'Writing style', 'wppowersuite-lite' ); ?></label>

			<div class="wpps-ai-alt-field-control wpps-ai-alt-field-control--left">
				<select id="<?php echo esc_attr( $module_id ); ?>_model" name="<?php echo esc_attr( $module_id ); ?>[model]" class="wpps-ai-alt-input" data-model-groups="<?php echo esc_attr( wp_json_encode( $all_model_groups ) ); ?>">
					<?php foreach ( $model_groups as $group ) : ?>
						<?php if ( empty( $group['models'] ) || ! is_array( $group['models'] ) ) : continue; endif; ?>
						<optgroup label="<?php echo esc_attr( (string) ( $group['label'] ?? '' ) ); ?>">
							<?php foreach ( $group['models'] as $val => $label ) : ?>
								<?php
								$model_locked = $provider === 'openai' && ! FeatureGate::isOpenAiModelAllowed( (string) $val );
								$model_label  = (string) $label;
								if ( $model_locked ) {
									$model_label .= ' (' . __( 'Premium', 'wppowersuite-lite' ) . ')';
								}
								?>
								<option value="<?php echo esc_attr( (string) $val ); ?>" <?php selected( $model, (string) $val ); ?> <?php disabled( $model_locked && (string) $val !== $model ); ?>><?php echo esc_html( $model_label ); ?></option>
							<?php endforeach; ?>
						</optgroup>
					<?php endforeach; ?>
				</select>
			</div>

			<div class="wpps-ai-alt-field-control wpps-ai-alt-field-control--right">
				<select id="<?php echo esc_attr( $module_id ); ?>_prompt_style" name="<?php echo esc_attr( $module_id ); ?>[prompt_style]" class="wpps-ai-alt-input">
					<?php foreach ( $prompt_style_groups as $group_label => $styles ) : ?>
						<optgroup label="<?php echo esc_attr( $group_label ); ?>">
							<?php foreach ( $styles as $val => $label ) : ?>
								<option value="<?php echo esc_attr( $val ); ?>" <?php selected( $current_prompt_style, $val ); ?>><?php echo esc_html( $label ); ?></option>
							<?php endforeach; ?>
						</optgroup>
					<?php endforeach; ?>
				</select>
			</div>

			<span class="wpps-ai-alt-help wpps-ai-alt-field-help wpps-ai-alt-field-help--left" id="<?php echo esc_attr( $module_id ); ?>_model_hint"><?php echo esc_html( ProviderModels::getSettingsHint( $provider, $settings ) ); ?></span>
			<span class="wpps-ai-alt-help wpps-ai-alt-field-help wpps-ai-alt-field-help--right" id="<?php echo esc_attr( $module_id ); ?>_prompt_style_hint"><?php echo esc_html( PromptStyles::description( $current_prompt_style ) ); ?></span>
		</div>

		<div class="wpps-ai-alt-field-pair wpps-ai-alt-field-pair--has-left-help">
			<label class="wpps-ai-alt-field-label wpps-ai-alt-field-label--left" for="<?php echo esc_attr( $module_id ); ?>_max_chars"><?php esc_html_e( 'Max length (characters)', 'wppowersuite-lite' ); ?></label>
			<label class="wpps-ai-alt-field-label wpps-ai-alt-field-label--right" for="<?php echo esc_attr( $module_id ); ?>_language"><?php esc_html_e( 'Alt Text Language', 'wppowersuite-lite' ); ?></label>

			<div class="wpps-ai-alt-field-control wpps-ai-alt-field-control--left">
				<input type="number" id="<?php echo esc_attr( $module_id ); ?>_max_chars" name="<?php echo esc_attr( $module_id ); ?>[max_chars]" value="<?php echo esc_attr( (string) (int) ( $settings['max_chars'] ?? 125 ) ); ?>" min="50" max="500" class="wpps-ai-alt-input" />
			</div>

			<div class="wpps-ai-alt-field-control wpps-ai-alt-field-control--right">
				<select id="<?php echo esc_attr( $module_id ); ?>_language" name="<?php echo esc_attr( $module_id ); ?>[language]" class="wpps-ai-alt-input">
					<?php foreach ( Languages::getList() as $code => $name ) : ?>
						<option value="<?php echo esc_attr( $code ); ?>" <?php selected( $settings['language'] ?? '', $code ); ?>><?php echo esc_html( $name ); ?></option>
					<?php endforeach; ?>
				</select>
			</div>

			<span class="wpps-ai-alt-help wpps-ai-alt-field-help wpps-ai-alt-field-help--left"><?php esc_html_e( '125 characters or fewer is recommended for accessibility.', 'wppowersuite-lite' ); ?></span>
		</div>

		<div class="wpps-ai-alt-field-full">
			<label class="wpps-ai-alt-field-label" for="<?php echo esc_attr( $module_id ); ?>_custom_prompt"><?php esc_html_e( 'Custom prompt', 'wppowersuite-lite' ); ?></label>
			<div class="wpps-ai-alt-field-control">
				<textarea id="<?php echo esc_attr( $module_id ); ?>_custom_prompt" name="<?php echo esc_attr( $module_id ); ?>[custom_prompt]" rows="3" class="wpps-ai-alt-input" maxlength="<?php echo esc_attr( (string) Settings::CUSTOM_PROMPT_MAX ); ?>" placeholder="<?php esc_attr_e( 'Leave blank to use the writing style above.', 'wppowersuite-lite' ); ?>"><?php echo esc_textarea( (string) ( $settings['custom_prompt'] ?? '' ) ); ?></textarea>
			</div>
			<span class="wpps-ai-alt-help"><?php esc_html_e( 'Replaces the writing style instructions sent to the vision model when set. Character and output limits still apply.', 'wppowersuite-lite' ); ?></span>
		</div>

		<div class="wpps-ai-alt-settings-group wpps-ai-alt-settings-group--nested">
			<h4 class="wpps-ai-alt-settings-group-title"><?php esc_html_e( 'When alt text is generated for an image:', 'wppowersuite-lite' ); ?></h4>
			<div class="wpps-ai-alt-checklist">
				<label class="wpps-ai-alt-check"><input type="checkbox" name="<?php echo esc_attr( $module_id ); ?>[copy_to_title]" value="1" <?php checked( ! empty( $settings['copy_to_title'] ) ); ?> /> <?php esc_html_e( 'Also set the image title with the generated alt text.', 'wppowersuite-lite' ); ?></label>
				<label class="wpps-ai-alt-check"><input type="checkbox" name="<?php echo esc_attr( $module_id ); ?>[copy_to_caption]" value="1" <?php checked( ! empty( $settings['copy_to_caption'] ) ); ?> /> <?php esc_html_e( 'Also set the image caption with the generated alt text.', 'wppowersuite-lite' ); ?></label>
				<label class="wpps-ai-alt-check"><input type="checkbox" name="<?php echo esc_attr( $module_id ); ?>[copy_to_description]" value="1" <?php checked( ! empty( $settings['copy_to_description'] ) ); ?> /> <?php esc_html_e( 'Also set the image description with the generated alt text.', 'wppowersuite-lite' ); ?></label>
			</div>

			<div class="wpps-ai-alt-field-pair">
				<label class="wpps-ai-alt-field-label wpps-ai-alt-field-label--left" for="<?php echo esc_attr( $module_id ); ?>_alt_prefix"><?php esc_html_e( 'Prefix every alt text with:', 'wppowersuite-lite' ); ?></label>
				<label class="wpps-ai-alt-field-label wpps-ai-alt-field-label--right" for="<?php echo esc_attr( $module_id ); ?>_alt_suffix"><?php esc_html_e( 'Suffix every alt text with:', 'wppowersuite-lite' ); ?></label>

				<div class="wpps-ai-alt-field-control wpps-ai-alt-field-control--left">
					<input type="text" id="<?php echo esc_attr( $module_id ); ?>_alt_prefix" name="<?php echo esc_attr( $module_id ); ?>[alt_prefix]" value="<?php echo esc_attr( (string) ( $settings['alt_prefix'] ?? '' ) ); ?>" class="wpps-ai-alt-input" />
				</div>

				<div class="wpps-ai-alt-field-control wpps-ai-alt-field-control--right">
					<input type="text" id="<?php echo esc_attr( $module_id ); ?>_alt_suffix" name="<?php echo esc_attr( $module_id ); ?>[alt_suffix]" value="<?php echo esc_attr( (string) ( $settings['alt_suffix'] ?? '' ) ); ?>" class="wpps-ai-alt-input" />
				</div>
			</div>
		</div>
		</div>

		<div class="wpps-ai-alt-settings-group wpps-ai-alt-automation">
			<h3 class="wpps-ai-alt-settings-group-title"><?php esc_html_e( 'Automation', 'wppowersuite-lite' ); ?></h3>
			<p class="wpps-ai-alt-settings-group-desc"><?php esc_html_e( 'Choose when alt text is created and how results are saved.', 'wppowersuite-lite' ); ?></p>

			<label class="wpps-ai-alt-option-card wpps-ai-alt-option-card--primary" for="<?php echo esc_attr( $module_id ); ?>_auto_on_upload">
				<input type="checkbox" id="<?php echo esc_attr( $module_id ); ?>_auto_on_upload" name="<?php echo esc_attr( $module_id ); ?>[auto_on_upload]" value="1" <?php checked( ! empty( $settings['auto_on_upload'] ) ); ?> />
				<span class="wpps-ai-alt-option-copy">
					<span class="wpps-ai-alt-option-title"><?php esc_html_e( 'Auto-generate on upload', 'wppowersuite-lite' ); ?></span>
					<span class="wpps-ai-alt-option-desc"><?php esc_html_e( 'Create alt text shortly after images are added to the media library. Uploads finish first; generation runs in the background.', 'wppowersuite-lite' ); ?></span>
					<span class="wpps-ai-alt-option-note"><?php esc_html_e( 'Each image usually takes 15–90 seconds depending on your AI model and image size. Budget models (e.g. GPT-4o Mini) are fastest for uploads.', 'wppowersuite-lite' ); ?></span>
					<span class="wpps-ai-alt-option-note"><?php esc_html_e( 'Bulk Generate and single-image actions are always available too.', 'wppowersuite-lite' ); ?></span>
				</span>
			</label>

			<div class="wpps-ai-alt-option-list">
				<label class="wpps-ai-alt-option-row" for="<?php echo esc_attr( $module_id ); ?>_auto_approve_suggestions">
					<input type="checkbox" id="<?php echo esc_attr( $module_id ); ?>_auto_approve_suggestions" name="<?php echo esc_attr( $module_id ); ?>[auto_approve_suggestions]" value="1" <?php checked( ! empty( $settings['auto_approve_suggestions'] ) ); ?> />
					<span class="wpps-ai-alt-option-copy">
						<span class="wpps-ai-alt-option-title"><?php esc_html_e( 'Apply without review', 'wppowersuite-lite' ); ?></span>
						<span class="wpps-ai-alt-option-desc"><?php esc_html_e( 'Save generated alt text directly to the media library. Turn off to send suggestions to the Review tab first.', 'wppowersuite-lite' ); ?></span>
					</span>
				</label>

				<label class="wpps-ai-alt-option-row" for="<?php echo esc_attr( $module_id ); ?>_overwrite_existing">
					<input type="checkbox" id="<?php echo esc_attr( $module_id ); ?>_overwrite_existing" name="<?php echo esc_attr( $module_id ); ?>[overwrite_existing]" value="1" <?php checked( ! empty( $settings['overwrite_existing'] ) ); ?> />
					<span class="wpps-ai-alt-option-copy">
						<span class="wpps-ai-alt-option-title"><?php esc_html_e( 'Overwrite existing alt text', 'wppowersuite-lite' ); ?></span>
						<span class="wpps-ai-alt-option-desc"><?php esc_html_e( 'Regenerate and replace alt text on images that already have it. Leave off to skip images with existing alt text.', 'wppowersuite-lite' ); ?></span>
					</span>
				</label>

				<label class="wpps-ai-alt-option-row" for="<?php echo esc_attr( $module_id ); ?>_queue_enabled">
					<input type="checkbox" id="<?php echo esc_attr( $module_id ); ?>_queue_enabled" name="<?php echo esc_attr( $module_id ); ?>[queue_enabled]" value="1" <?php checked( ! empty( $settings['queue_enabled'] ) ); ?> />
					<span class="wpps-ai-alt-option-copy">
						<span class="wpps-ai-alt-option-title"><?php esc_html_e( 'Use background queue', 'wppowersuite-lite' ); ?></span>
						<span class="wpps-ai-alt-option-desc"><?php esc_html_e( 'Process media library bulk actions via WP-Cron instead of blocking the admin screen. Auto-generate on upload always uses the queue.', 'wppowersuite-lite' ); ?></span>
						<span class="wpps-ai-alt-option-note"><?php esc_html_e( 'The Bulk Generate tab always runs live in your browser — keep that tab open until it finishes.', 'wppowersuite-lite' ); ?></span>
					</span>
				</label>
			</div>
		</div>
		<?php wpps_ai_alt_accordion_close(); ?>

		<?php
		wpps_ai_alt_accordion_open(
			[
				'id'    => 'seo-keywords',
				'title' => __( 'SEO keywords', 'wppowersuite-lite' ),
				'tip'   => __( 'Use focus keyphrases from SEO plugins or post titles when generating alt text.', 'wppowersuite-lite' ),
			]
		);
		?>
		<div class="wpps-ai-alt-settings-group">
			<p class="wpps-ai-alt-settings-group-desc"><?php esc_html_e( 'Integrate SEO focus keyphrases into generated alt text when available.', 'wppowersuite-lite' ); ?></p>
			<div class="wpps-ai-alt-checklist">
				<label class="wpps-ai-alt-check"><input type="checkbox" name="<?php echo esc_attr( $module_id ); ?>[use_seo_keyphrase]" value="1" <?php checked( ! empty( $settings['use_seo_keyphrase'] ) ); ?> /> <?php esc_html_e( 'Generate alt text using focus keyphrases, if present.', 'wppowersuite-lite' ); ?></label>
				<p class="wpps-ai-alt-help"><?php esc_html_e( 'Compatible with Yoast SEO, Rank Math, All in One SEO, SEOPress, Squirrly, SmartCrawl, and The SEO Framework.', 'wppowersuite-lite' ); ?></p>
				<label class="wpps-ai-alt-check"><input type="checkbox" name="<?php echo esc_attr( $module_id ); ?>[use_post_title_keywords]" value="1" <?php checked( ! empty( $settings['use_post_title_keywords'] ) ); ?> /> <?php esc_html_e( 'Use post title as keywords if SEO keywords are not found from plugins.', 'wppowersuite-lite' ); ?></label>
			</div>
		</div>
		<?php wpps_ai_alt_accordion_close(); ?>

		<?php
		wpps_ai_alt_accordion_open(
			[
				'id'    => 'ai-rewrite',
				'title' => __( 'AI rewrite prompt', 'wppowersuite-lite' ),
				'tip'   => __( 'Optionally rewrite generated alt text with a custom prompt.', 'wppowersuite-lite' ),
			]
		);
		?>
		<div class="wpps-ai-alt-field-full">
			<label class="wpps-ai-alt-field-label" for="<?php echo esc_attr( $module_id ); ?>_rewrite_prompt"><?php esc_html_e( 'Rewrite prompt', 'wppowersuite-lite' ); ?></label>
			<div class="wpps-ai-alt-field-control">
				<textarea id="<?php echo esc_attr( $module_id ); ?>_rewrite_prompt" name="<?php echo esc_attr( $module_id ); ?>[rewrite_prompt]" rows="3" class="wpps-ai-alt-input" placeholder="<?php esc_attr_e( 'Rewrite the following text in the style of Shakespeare: {{AltText}}', 'wppowersuite-lite' ); ?>"><?php echo esc_textarea( (string) ( $settings['rewrite_prompt'] ?? '' ) ); ?></textarea>
			</div>
			<span class="wpps-ai-alt-help"><?php esc_html_e( 'Your prompt must include the macro {{AltText}}, which is replaced with the generated alt text before sending to your OpenAI API key.', 'wppowersuite-lite' ); ?></span>
			<span class="wpps-ai-alt-help"><?php esc_html_e( 'Leave blank to skip the rewrite step.', 'wppowersuite-lite' ); ?></span>
		</div>
		<?php wpps_ai_alt_accordion_close(); ?>

		<?php
		wpps_ai_alt_accordion_open(
			[
				'id'    => 'post-refresh',
				'title' => __( 'Post & page refresh', 'wppowersuite-lite' ),
				'tip'   => __( 'Bulk refresh images embedded in post and page content from the Posts/Pages list.', 'wppowersuite-lite' ),
			]
		);
		?>
		<div class="wpps-ai-alt-settings-group">
			<p class="wpps-ai-alt-settings-group-desc"><?php esc_html_e( 'Use Bulk Actions → Refresh alt text in content on the Posts or Pages screen.', 'wppowersuite-lite' ); ?></p>
			<div class="wpps-ai-alt-checklist">
				<label class="wpps-ai-alt-check"><input type="checkbox" name="<?php echo esc_attr( $module_id ); ?>[sync_overwrite_content_alt]" value="1" <?php checked( ! empty( $settings['sync_overwrite_content_alt'] ) ); ?> /> <?php esc_html_e( 'Overwrite existing alt text when refreshing posts and pages using the Bulk Action menu.', 'wppowersuite-lite' ); ?></label>
				<label class="wpps-ai-alt-check"><input type="checkbox" name="<?php echo esc_attr( $module_id ); ?>[refresh_external_images]" value="1" <?php checked( ! empty( $settings['refresh_external_images'] ) ); ?> /> <?php esc_html_e( 'Process external images when refreshing posts and pages using the Bulk Action menu.', 'wppowersuite-lite' ); ?></label>
				<label class="wpps-ai-alt-check"><input type="checkbox" id="<?php echo esc_attr( $module_id ); ?>_sync_content_alt" name="<?php echo esc_attr( $module_id ); ?>[sync_content_alt]" value="1" <?php checked( ! empty( $settings['sync_content_alt'] ) ); ?> /> <?php esc_html_e( 'Update alt attributes in post content when saving to the media library', 'wppowersuite-lite' ); ?></label>
			</div>
		</div>
		<?php wpps_ai_alt_accordion_close(); ?>

		<?php if ( $has_woo ) : ?>
			<?php
			wpps_ai_alt_accordion_open(
				[
					'id'    => 'woocommerce',
					'title' => __( 'WooCommerce', 'wppowersuite-lite' ),
					'tip'   => __( 'Control how product context is used for WooCommerce images.', 'wppowersuite-lite' ),
				]
			);
			?>
			<div class="wpps-ai-alt-settings-group">
				<div class="wpps-ai-alt-checklist">
					<label class="wpps-ai-alt-check"><input type="checkbox" name="<?php echo esc_attr( $module_id ); ?>[use_woo_context]" value="1" <?php checked( ! empty( $settings['use_woo_context'] ) ); ?> /> <?php esc_html_e( 'Use product name in generated alt text for WooCommerce product images.', 'wppowersuite-lite' ); ?></label>
					<label class="wpps-ai-alt-check"><input type="checkbox" name="<?php echo esc_attr( $module_id ); ?>[woo_use_image_title]" value="1" <?php checked( ! empty( $settings['woo_use_image_title'] ) ); ?> /> <?php esc_html_e( 'Get product name from image title, not WooCommerce product.', 'wppowersuite-lite' ); ?></label>
					<p class="wpps-ai-alt-help"><?php esc_html_e( 'Use this if your image titles are more descriptive than your WooCommerce product names.', 'wppowersuite-lite' ); ?></p>
				</div>
			</div>
			<?php wpps_ai_alt_accordion_close(); ?>
		<?php endif; ?>

		<?php
		wpps_ai_alt_accordion_open(
			[
				'id'    => 'filtering',
				'title' => __( 'Image filtering', 'wppowersuite-lite' ),
				'tip'   => __( 'Limit which images are eligible for automatic, bulk, and on-demand generation.', 'wppowersuite-lite' ),
			]
		);
		?>
		<div class="wpps-ai-alt-settings-group wpps-ai-alt-filtering">
			<div class="wpps-ai-alt-field-full">
				<label class="wpps-ai-alt-field-label" for="<?php echo esc_attr( $module_id ); ?>_allowed_extensions"><?php esc_html_e( 'Only process images with these file extensions:', 'wppowersuite-lite' ); ?></label>
				<div class="wpps-ai-alt-field-control">
					<input type="text" id="<?php echo esc_attr( $module_id ); ?>_allowed_extensions" name="<?php echo esc_attr( $module_id ); ?>[allowed_extensions]" value="<?php echo esc_attr( (string) ( $settings['allowed_extensions'] ?? '' ) ); ?>" class="wpps-ai-alt-input" placeholder="<?php esc_attr_e( 'jpg,jpeg,webp,svg,avif', 'wppowersuite-lite' ); ?>" />
				</div>
				<span class="wpps-ai-alt-help"><?php esc_html_e( 'Separate multiple extensions with commas. jpg and jpeg are treated the same. Example: jpg,jpeg,webp,svg,avif', 'wppowersuite-lite' ); ?></span>
				<span class="wpps-ai-alt-help"><?php esc_html_e( 'Leave blank to process all image types.', 'wppowersuite-lite' ); ?></span>
				<p class="wpps-ai-alt-settings-note"><?php esc_html_e( 'Note: Advanced image formats (AVIF, SVG) may use more API tokens than standard JPG or PNG files.', 'wppowersuite-lite' ); ?></p>
			</div>

			<div class="wpps-ai-alt-field-full">
				<label class="wpps-ai-alt-field-label" for="<?php echo esc_attr( $module_id ); ?>_excluded_post_types_ms"><?php esc_html_e( 'Exclude images attached to these post types:', 'wppowersuite-lite' ); ?></label>
				<input type="hidden" name="<?php echo esc_attr( $module_id ); ?>[excluded_post_types_sent]" value="1" />
				<div class="wpps-ai-alt-field-control">
					<?php if ( $available_post_types === [] ) : ?>
						<p class="wpps-ai-alt-help"><?php esc_html_e( 'No public post types were found.', 'wppowersuite-lite' ); ?></p>
					<?php else : ?>
						<div class="wpps-ct-multiselect" id="<?php echo esc_attr( $module_id ); ?>_excluded_post_types_ms"
							data-label-none="<?php echo esc_attr( __( 'Select post types to exclude...', 'wppowersuite-lite' ) ); ?>"
							data-label-single="<?php echo esc_attr( __( '1 post type excluded', 'wppowersuite-lite' ) ); ?>"
							<?php /* translators: %d: number value. */ ?>
							data-label-multi="<?php echo esc_attr( __( '%d post types excluded', 'wppowersuite-lite' ) ); ?>">
							<button type="button" class="wpps-ct-multiselect__trigger" id="<?php echo esc_attr( $module_id ); ?>_excluded_post_types_ms" aria-expanded="false" aria-haspopup="listbox" aria-controls="<?php echo esc_attr( $module_id ); ?>_excluded_post_types_list">
								<span class="wpps-ct-multiselect__trigger-text"><?php esc_html_e( 'Select post types to exclude...', 'wppowersuite-lite' ); ?></span>
								<span class="dashicons dashicons-arrow-down-alt2 wpps-ct-multiselect__chevron"></span>
							</button>
							<div class="wpps-ct-multiselect__dropdown" id="<?php echo esc_attr( $module_id ); ?>_excluded_post_types_list" role="listbox" hidden>
								<input type="text" class="wpps-ct-multiselect__search" placeholder="<?php esc_attr_e( 'Search post types...', 'wppowersuite-lite' ); ?>" autocomplete="off" />
								<div class="wpps-ct-multiselect__actions">
									<button type="button" class="wpps-ct-multiselect__action" data-action="select-all"><?php esc_html_e( 'Select all', 'wppowersuite-lite' ); ?></button>
									<button type="button" class="wpps-ct-multiselect__action" data-action="clear"><?php esc_html_e( 'Clear', 'wppowersuite-lite' ); ?></button>
								</div>
								<div class="wpps-ct-multiselect__options">
									<?php foreach ( $available_post_types as $post_type_name => $post_type_object ) : ?>
										<?php
										$label   = $post_type_object->labels->singular_name ?? $post_type_object->label ?? $post_type_name;
										$checked = in_array( $post_type_name, $excluded_post_types, true );
										?>
										<label class="wpps-ct-multiselect__option" data-slug="<?php echo esc_attr( $post_type_name ); ?>" data-label="<?php echo esc_attr( $label ); ?>">
											<input type="checkbox"
												name="<?php echo esc_attr( $module_id ); ?>[excluded_post_types][]"
												value="<?php echo esc_attr( $post_type_name ); ?>"
												<?php checked( $checked ); ?>
											/>
											<span class="wpps-ct-multiselect__option-label"><?php echo esc_html( $label ); ?></span>
											<span class="wpps-ct-multiselect__option-slug">(<?php echo esc_html( $post_type_name ); ?>)</span>
										</label>
									<?php endforeach; ?>
								</div>
							</div>
						</div>
					<?php endif; ?>
				</div>
				<span class="wpps-ai-alt-help"><?php esc_html_e( 'Images attached to the selected post types will be skipped during generation and bulk processing.', 'wppowersuite-lite' ); ?></span>
				<span class="wpps-ai-alt-help"><?php esc_html_e( 'Leave empty to process images from all post types.', 'wppowersuite-lite' ); ?></span>
			</div>

			<label class="wpps-ai-alt-option-row" for="<?php echo esc_attr( $module_id ); ?>_skip_missing_files">
				<input type="checkbox" id="<?php echo esc_attr( $module_id ); ?>_skip_missing_files" name="<?php echo esc_attr( $module_id ); ?>[skip_missing_files]" value="1" <?php checked( ! empty( $settings['skip_missing_files'] ) ); ?> />
				<span class="wpps-ai-alt-option-copy">
					<span class="wpps-ai-alt-option-title"><?php esc_html_e( 'Skip image files unable to be found on the server.', 'wppowersuite-lite' ); ?></span>
					<span class="wpps-ai-alt-option-desc"><?php esc_html_e( 'Useful if WordPress references old images which have been deleted.', 'wppowersuite-lite' ); ?></span>
				</span>
			</label>
		</div>
		<?php wpps_ai_alt_accordion_close(); ?>

		<?php
		wpps_ai_alt_accordion_open(
			[
				'id'    => 'advanced',
				'title' => __( 'Advanced', 'wppowersuite-lite' ),
				'tip'   => __( 'Batch size, timeouts, metadata, lazy-loaded images, and multisite API keys.', 'wppowersuite-lite' ),
			]
		);
		?>
		<div class="wpps-ai-alt-field-full">
			<label class="wpps-ai-alt-field-label" for="<?php echo esc_attr( $module_id ); ?>_batch_size"><?php esc_html_e( 'Bulk batch size', 'wppowersuite-lite' ); ?></label>
			<div class="wpps-ai-alt-field-control wpps-ai-alt-field-control--narrow">
				<input type="number" id="<?php echo esc_attr( $module_id ); ?>_batch_size" name="<?php echo esc_attr( $module_id ); ?>[batch_size]" value="<?php echo esc_attr( (string) (int) ( $settings['batch_size'] ?? 5 ) ); ?>" min="1" max="10" class="wpps-ai-alt-input" />
			</div>
			<span class="wpps-ai-alt-help"><?php esc_html_e( 'Images processed per Bulk Generate step (1–10). Higher values are faster but use more API quota per step.', 'wppowersuite-lite' ); ?></span>
		</div>

		<div class="wpps-ai-alt-settings-group">
			<h3 class="wpps-ai-alt-settings-group-title"><?php esc_html_e( 'Generation', 'wppowersuite-lite' ); ?></h3>
			<div class="wpps-ai-alt-field-full">
				<label class="wpps-ai-alt-field-label" for="<?php echo esc_attr( $module_id ); ?>_request_timeout"><?php esc_html_e( 'Timeout alt text generation requests after', 'wppowersuite-lite' ); ?></label>
				<div class="wpps-ai-alt-field-control wpps-ai-alt-field-control--narrow">
					<input type="number" id="<?php echo esc_attr( $module_id ); ?>_request_timeout" name="<?php echo esc_attr( $module_id ); ?>[request_timeout]" value="<?php echo esc_attr( (string) (int) ( $settings['request_timeout'] ?? 90 ) ); ?>" min="10" max="180" class="wpps-ai-alt-input" />
				</div>
				<span class="wpps-ai-alt-help"><?php esc_html_e( 'Seconds to wait for each AI request (10–180). Vision models often need 30–90 seconds per image. The default is 90 seconds. Increase this if you see timeout errors during upload or bulk generation.', 'wppowersuite-lite' ); ?></span>
			</div>

			<label class="wpps-ai-alt-option-row" for="<?php echo esc_attr( $module_id ); ?>_generate_missing_metadata">
				<input type="checkbox" id="<?php echo esc_attr( $module_id ); ?>_generate_missing_metadata" name="<?php echo esc_attr( $module_id ); ?>[generate_missing_metadata]" value="1" <?php checked( ! empty( $settings['generate_missing_metadata'] ) ); ?> />
				<span class="wpps-ai-alt-option-copy">
					<span class="wpps-ai-alt-option-title"><?php esc_html_e( 'Allow WordPress to generate missing metadata for processed images.', 'wppowersuite-lite' ); ?></span>
					<span class="wpps-ai-alt-option-desc"><?php esc_html_e( 'Performance warning: this can cause bulk processing failures and server timeouts. Only enable if images were uploaded via FTP or are missing thumbnails.', 'wppowersuite-lite' ); ?></span>
				</span>
			</label>
		</div>

		<div class="wpps-ai-alt-settings-group">
			<h3 class="wpps-ai-alt-settings-group-title"><?php esc_html_e( 'Post/page refresh', 'wppowersuite-lite' ); ?></h3>
			<div class="wpps-ai-alt-field-full">
				<label class="wpps-ai-alt-field-label" for="<?php echo esc_attr( $module_id ); ?>_lazy_load_src_attr"><?php esc_html_e( 'Image tag attribute for source URL', 'wppowersuite-lite' ); ?></label>
				<div class="wpps-ai-alt-field-control wpps-ai-alt-field-control--narrow">
					<input type="text" id="<?php echo esc_attr( $module_id ); ?>_lazy_load_src_attr" name="<?php echo esc_attr( $module_id ); ?>[lazy_load_src_attr]" value="<?php echo esc_attr( (string) ( $settings['lazy_load_src_attr'] ?? '' ) ); ?>" class="wpps-ai-alt-input" placeholder="<?php esc_attr_e( 'data-src', 'wppowersuite-lite' ); ?>" />
				</div>
				<span class="wpps-ai-alt-help"><?php esc_html_e( 'Specify the attribute on &lt;img&gt; tags which contains the image source URL. Typically needed if your posts use JavaScript to lazy load images.', 'wppowersuite-lite' ); ?></span>
			</div>
		</div>

		<div class="wpps-ai-alt-checklist">
			<label class="wpps-ai-alt-check">
				<input type="checkbox" name="<?php echo esc_attr( $module_id ); ?>[client_safe_mode]" value="1" <?php checked( ! empty( $settings['client_safe_mode'] ) ); ?> />
				<?php esc_html_e( 'Hide token counts and cost estimates', 'wppowersuite-lite' ); ?>
			</label>
		</div>

		<div class="wpps-ai-alt-settings-group">
			<h3 class="wpps-ai-alt-settings-group-title"><?php esc_html_e( 'History & logs', 'wppowersuite-lite' ); ?></h3>
			<div class="wpps-ai-alt-field-full">
				<label class="wpps-ai-alt-field-label" for="<?php echo esc_attr( $module_id ); ?>_log_retention_days"><?php esc_html_e( 'Keep log entries for', 'wppowersuite-lite' ); ?></label>
				<div class="wpps-ai-alt-field-control wpps-ai-alt-field-control--narrow">
					<input type="number" id="<?php echo esc_attr( $module_id ); ?>_log_retention_days" name="<?php echo esc_attr( $module_id ); ?>[log_retention_days]" value="<?php echo esc_attr( (string) (int) ( $settings['log_retention_days'] ?? Settings::LOG_RETENTION_DEFAULT ) ); ?>" min="0" max="<?php echo esc_attr( (string) Settings::LOG_RETENTION_MAX ); ?>" class="wpps-ai-alt-input" />
				</div>
				<span class="wpps-ai-alt-help"><?php esc_html_e( 'Days to keep generation history before automatic cleanup. Default is 7 days. Set to 0 to keep all entries (not recommended on busy sites).', 'wppowersuite-lite' ); ?></span>
			</div>

			<h4 class="wpps-ai-alt-settings-group-title"><?php esc_html_e( 'Recent errors', 'wppowersuite-lite' ); ?></h4>
			<?php if ( $recent_errors === [] ) : ?>
				<p class="wpps-ai-alt-help"><?php esc_html_e( 'No recent generation failures.', 'wppowersuite-lite' ); ?></p>
			<?php else : ?>
				<div class="wpps-ai-alt-table-wrap">
					<table class="widefat striped wpps-ai-alt-error-log-table">
						<thead>
							<tr>
								<th><?php esc_html_e( 'Time', 'wppowersuite-lite' ); ?></th>
								<th><?php esc_html_e( 'Image', 'wppowersuite-lite' ); ?></th>
								<th><?php esc_html_e( 'Error', 'wppowersuite-lite' ); ?></th>
							</tr>
						</thead>
						<tbody>
							<?php foreach ( $recent_errors as $row ) : ?>
								<tr>
									<td><?php echo esc_html( (string) ( $row['created_at'] ?? '' ) ); ?></td>
									<td><?php echo esc_html( (string) ( $row['filename'] ?? '' ) ); ?></td>
									<td><?php echo esc_html( (string) ( $row['error_message'] ?? __( 'Unknown error', 'wppowersuite-lite' ) ) ); ?></td>
								</tr>
							<?php endforeach; ?>
						</tbody>
					</table>
				</div>
				<p class="wpps-ai-alt-help"><a href="<?php echo esc_url( add_query_arg( 'tab', 'history', admin_url( 'admin.php?page=' . \AIAltTextGenerator\Module::DASHBOARD_SLUG ) ) ); ?>" class="wpps-ai-alt-tab-link" data-tab="history"><?php esc_html_e( 'View full history', 'wppowersuite-lite' ); ?></a></p>
			<?php endif; ?>
		</div>
		<?php wpps_ai_alt_accordion_close(); ?>

		<?php
		wpps_ai_alt_accordion_open(
			[
				'id'    => 'access',
				'title' => __( 'Access', 'wppowersuite-lite' ),
				'tip'   => __( 'Who can use the module, monthly limits, and optional scheduled reports.', 'wppowersuite-lite' ),
			]
		);
		?>
		<div class="wpps-ai-alt-settings-group">
			<label class="wpps-ai-alt-option-row" for="<?php echo esc_attr( $module_id ); ?>_admin_only_access">
				<input type="checkbox" id="<?php echo esc_attr( $module_id ); ?>_admin_only_access" name="<?php echo esc_attr( $module_id ); ?>[admin_only_access]" value="1" <?php checked( $admin_only_access ); ?> />
				<span class="wpps-ai-alt-option-copy">
					<span class="wpps-ai-alt-option-title"><?php esc_html_e( 'Administrators only (recommended)', 'wppowersuite-lite' ); ?></span>
					<span class="wpps-ai-alt-option-desc"><?php esc_html_e( 'Only administrators can access the AI Alt Text menu and save settings.', 'wppowersuite-lite' ); ?></span>
				</span>
			</label>
		</div>

		<div class="wpps-ai-alt-settings-group">
			<h3 class="wpps-ai-alt-settings-group-title">
				<?php esc_html_e( 'Monthly limit', 'wppowersuite-lite' ); ?>
				<?php if ( ! $is_pro ) : ?>
					<span class="wpps-pro-badge"><?php esc_html_e( 'Premium', 'wppowersuite-lite' ); ?></span>
				<?php endif; ?>
			</h3>
		<div class="wpps-ai-alt-field-full">
			<label class="wpps-ai-alt-field-label" for="<?php echo esc_attr( $module_id ); ?>_monthly_limit"><?php esc_html_e( 'Monthly generation limit', 'wppowersuite-lite' ); ?></label>
			<div class="wpps-ai-alt-field-control wpps-ai-alt-field-control--narrow">
				<?php if ( $is_pro ) : ?>
					<input type="number" id="<?php echo esc_attr( $module_id ); ?>_monthly_limit" name="<?php echo esc_attr( $module_id ); ?>[monthly_limit]" value="<?php echo esc_attr( (string) (int) ( $settings['monthly_limit'] ?? 0 ) ); ?>" min="0" class="wpps-ai-alt-input" />
				<?php elseif ( $free_monthly_limit > 0 ) : ?>
					<input type="number" id="<?php echo esc_attr( $module_id ); ?>_monthly_limit" value="<?php echo esc_attr( (string) $free_monthly_limit ); ?>" class="wpps-ai-alt-input" disabled="disabled" />
				<?php else : ?>
					<input type="number" id="<?php echo esc_attr( $module_id ); ?>_monthly_limit" value="0" class="wpps-ai-alt-input" disabled="disabled" />
				<?php endif; ?>
			</div>
			<span class="wpps-ai-alt-help">
				<?php if ( $is_pro ) : ?>
					<?php esc_html_e( '0 = unlimited. New generations stop once the limit is reached.', 'wppowersuite-lite' ); ?>
				<?php elseif ( $free_monthly_limit > 0 ) : ?>
					<?php
					echo wp_kses_post(
						sprintf(
							/* translators: 1: free monthly limit number, 2: URL to license page */
							__( 'Free plan includes %1$d generations per month. <a href="%2$s">Activate your license</a> to set a custom limit or remove the cap.', 'wppowersuite-lite' ),
							$free_monthly_limit,
							esc_url( $license_page_url )
						)
					);
					?>
				<?php else : ?>
					<?php
					echo wp_kses_post(
						sprintf(
							/* translators: %s: URL to license page */
							__( 'Generation is unlimited in this plugin. <a href="%s">Activate your license</a> to set an optional monthly cap.', 'wppowersuite-lite' ),
							esc_url( $license_page_url )
						)
					);
					?>
				<?php endif; ?>
			</span>
		</div>
		</div>

		<div class="wpps-ai-alt-settings-group<?php echo $admin_only_access ? ' is-hidden' : ''; ?>" id="<?php echo esc_attr( $module_id ); ?>_role_caps_wrap">
			<h3 class="wpps-ai-alt-settings-group-title"><?php esc_html_e( 'Role permissions', 'wppowersuite-lite' ); ?></h3>
		<div class="wpps-ai-alt-table-wrap">
			<table class="widefat striped wpps-ai-alt-role-table">
				<thead>
					<tr>
						<th><?php esc_html_e( 'Role', 'wppowersuite-lite' ); ?></th>
						<th><?php esc_html_e( 'Generate', 'wppowersuite-lite' ); ?></th>
						<th><?php esc_html_e( 'View dashboard', 'wppowersuite-lite' ); ?></th>
						<th><?php esc_html_e( 'Manage settings', 'wppowersuite-lite' ); ?></th>
					</tr>
				</thead>
				<tbody>
					<?php foreach ( $wp_roles as $role_key => $role_label ) : ?>
						<tr>
							<td><?php echo esc_html( translate_user_role( $role_label ) ); ?></td>
							<td><input type="checkbox" name="<?php echo esc_attr( $module_id ); ?>[role_caps][generate][]" value="<?php echo esc_attr( $role_key ); ?>" <?php checked( in_array( $role_key, $role_caps['generate'], true ) ); ?> /></td>
							<td><input type="checkbox" name="<?php echo esc_attr( $module_id ); ?>[role_caps][view][]" value="<?php echo esc_attr( $role_key ); ?>" <?php checked( in_array( $role_key, $role_caps['view'], true ) ); ?> /></td>
							<td><input type="checkbox" name="<?php echo esc_attr( $module_id ); ?>[role_caps][manage][]" value="<?php echo esc_attr( $role_key ); ?>" <?php checked( in_array( $role_key, $role_caps['manage'], true ) ); ?> /></td>
						</tr>
					<?php endforeach; ?>
				</tbody>
			</table>
		</div>
		</div>

		<div class="wpps-ai-alt-settings-group">
			<h3 class="wpps-ai-alt-settings-group-title">
				<?php esc_html_e( 'Scheduled reports', 'wppowersuite-lite' ); ?>
				<?php if ( ! FeatureGate::featureEnabled( 'scheduled_scan' ) ) : ?>
					<span class="wpps-pro-badge"><?php esc_html_e( 'Premium', 'wppowersuite-lite' ); ?></span>
				<?php endif; ?>
			</h3>
			<?php if ( ! FeatureGate::featureEnabled( 'scheduled_scan' ) ) : ?>
				<p class="wpps-ai-alt-help">
					<?php
					echo wp_kses_post(
						sprintf(
							/* translators: %s: URL to license page */
							__( 'Automated library scans and email summaries are Premium features. <a href="%s">Activate your license</a> to schedule them.', 'wppowersuite-lite' ),
							esc_url( $license_page_url )
						)
					);
					?>
				</p>
			<?php endif; ?>
		<div class="wpps-ai-alt-field-pair">
			<label class="wpps-ai-alt-field-label wpps-ai-alt-field-label--left" for="<?php echo esc_attr( $module_id ); ?>_scan_schedule"><?php esc_html_e( 'Scan frequency', 'wppowersuite-lite' ); ?></label>
			<label class="wpps-ai-alt-field-label wpps-ai-alt-field-label--right" for="<?php echo esc_attr( $module_id ); ?>_report_email"><?php esc_html_e( 'Send reports to', 'wppowersuite-lite' ); ?></label>

			<div class="wpps-ai-alt-field-control wpps-ai-alt-field-control--left">
				<select id="<?php echo esc_attr( $module_id ); ?>_scan_schedule" name="<?php echo esc_attr( $module_id ); ?>[scan_schedule]" class="wpps-ai-alt-input" <?php disabled( ! FeatureGate::featureEnabled( 'scheduled_scan' ) ); ?>>
					<option value="manual" <?php selected( $settings['scan_schedule'] ?? 'manual', 'manual' ); ?>><?php esc_html_e( 'Manual only', 'wppowersuite-lite' ); ?></option>
					<option value="daily" <?php selected( $settings['scan_schedule'] ?? '', 'daily' ); ?>><?php esc_html_e( 'Daily', 'wppowersuite-lite' ); ?></option>
					<option value="weekly" <?php selected( $settings['scan_schedule'] ?? '', 'weekly' ); ?>><?php esc_html_e( 'Weekly', 'wppowersuite-lite' ); ?></option>
					<option value="monthly" <?php selected( $settings['scan_schedule'] ?? '', 'monthly' ); ?>><?php esc_html_e( 'Monthly', 'wppowersuite-lite' ); ?></option>
				</select>
			</div>

			<div class="wpps-ai-alt-field-control wpps-ai-alt-field-control--right">
				<input type="email" id="<?php echo esc_attr( $module_id ); ?>_report_email" name="<?php echo esc_attr( $module_id ); ?>[report_email]" value="<?php echo esc_attr( (string) ( $settings['report_email'] ?? '' ) ); ?>" placeholder="<?php echo esc_attr( (string) get_option( 'admin_email' ) ); ?>" class="wpps-ai-alt-input" <?php disabled( ! FeatureGate::featureEnabled( 'scheduled_scan' ) ); ?> />
			</div>
		</div>
		<div class="wpps-ai-alt-checklist">
			<label class="wpps-ai-alt-check"><input type="checkbox" name="<?php echo esc_attr( $module_id ); ?>[email_reports]" value="1" <?php checked( ! empty( $settings['email_reports'] ) ); ?> <?php disabled( ! FeatureGate::featureEnabled( 'email_reports' ) ); ?> /> <?php esc_html_e( 'Email scan summary after scheduled scans', 'wppowersuite-lite' ); ?></label>
		</div>
		</div>

		<div class="wpps-ai-alt-settings-group">
			<h3 class="wpps-ai-alt-settings-group-title"><?php esc_html_e( 'Diagnostics', 'wppowersuite-lite' ); ?></h3>
			<div class="wpps-ai-alt-checklist">
				<label class="wpps-ai-alt-check">
					<input type="checkbox" name="<?php echo esc_attr( $module_id ); ?>[debug_logging]" value="1" <?php checked( ! empty( $settings['debug_logging'] ) ); ?> />
					<?php esc_html_e( 'Enable structured debug logging (never stores API keys or image data)', 'wppowersuite-lite' ); ?>
				</label>
			</div>
			<?php
			$diag = \AIAltTextGenerator\Diagnostics::report();
			$last = is_array( $diag['last_bulk_error'] ?? null ) ? $diag['last_bulk_error'] : null;
			?>
			<ul class="wpps-ai-alt-help" style="margin-top:12px;list-style:disc;padding-left:1.25em;">
				<?php /* translators: 1: string, 2: string. */ ?>
				<li><?php echo esc_html( sprintf( /* translators: 1: WP version, 2: PHP version */ __( 'WordPress %1$s · PHP %2$s', 'wppowersuite-lite' ), (string) $diag['wordpress'], (string) $diag['php'] ) ); ?></li>
				<li><?php echo esc_html( sprintf( /* translators: 1: memory limit, 2: max execution time */ __( 'Memory %1$s · Max execution %2$ss', 'wppowersuite-lite' ), (string) $diag['memory_limit'], (string) $diag['max_execution_time'] ) ); ?></li>
				<li><?php echo esc_html( sprintf( /* translators: 1: provider, 2: model */ __( 'Active provider: %1$s · Model: %2$s', 'wppowersuite-lite' ), (string) $diag['provider'], (string) $diag['model'] ) ); ?></li>
				<li><?php echo esc_html( ! empty( $diag['has_api_key'] ) ? __( 'API key: configured', 'wppowersuite-lite' ) : __( 'API key: missing', 'wppowersuite-lite' ) ); ?></li>
				<?php /* translators: 1: string, 2: string. */ ?>
				<li><?php echo esc_html( sprintf( /* translators: %s: queue status */ __( 'Background queue: %s', 'wppowersuite-lite' ), (string) $diag['queue_status'] ) ); ?></li>
				<li><?php echo esc_html( ! empty( $diag['cron_disabled'] ) ? __( 'WP-Cron: disabled (DISABLE_WP_CRON)', 'wppowersuite-lite' ) : __( 'WP-Cron: enabled', 'wppowersuite-lite' ) ); ?></li>
				<li><?php echo esc_html( ! empty( $diag['uploads_writable'] ) ? __( 'Uploads directory: readable', 'wppowersuite-lite' ) : __( 'Uploads directory: error', 'wppowersuite-lite' ) ); ?></li>
				<li><?php echo esc_html( ( ! empty( $diag['gd_loaded'] ) || ! empty( $diag['imagick_loaded'] ) ) ? __( 'Image editor: available', 'wppowersuite-lite' ) : __( 'Image editor: missing (GD/Imagick)', 'wppowersuite-lite' ) ); ?></li>
				<?php if ( is_array( $last ) && ( $last['message'] ?? '' ) !== '' ) : ?>
					<li>
						<?php
						echo esc_html(
							sprintf(
								/* translators: 1: error code, 2: error message */
								__( 'Last bulk error (%1$s): %2$s', 'wppowersuite-lite' ),
								(string) ( $last['code'] ?? '' ),
								(string) $last['message']
							)
						);
						?>
					</li>
				<?php endif; ?>
			</ul>
		</div>
		<?php wpps_ai_alt_accordion_close(); ?>
	</div>

	<div class="wpps-ai-alt-settings-footer">
		<button type="submit" class="button button-primary wpps-ai-alt-btn-primary"><?php esc_html_e( 'Save settings', 'wppowersuite-lite' ); ?></button>
	</div>
</form>

