<?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::FREE_MONTHLY_LIMIT;

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

	<div class="wpps-ai-alt-accordions">
		<?php
		wpps_ai_alt_accordion_open(
			[
				'id'    => 'connection',
				'title' => __( 'AI provider', 'wp-powersuite' ),
				'tip'   => __( 'Pick one of the AI providers configured in PowerSuite Settings. Add or update keys under Settings → AI Connections.', 'wp-powersuite' ),
				'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', 'wp-powersuite' ); ?></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', 'wp-powersuite' ) . ')';
						}
						?>
						<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.', 'wp-powersuite' ),
					'<a href="' . esc_url( $global_settings_url ) . '">' . esc_html__( 'Settings → AI Connections', 'wp-powersuite' ) . '</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.', 'wp-powersuite' ),
						'<a href="' . esc_url( $global_settings_url ) . '">' . esc_html__( 'Settings → AI Connections', 'wp-powersuite' ) . '</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.', 'wp-powersuite' ),
						'<a href="' . esc_url( $global_settings_url ) . '">' . esc_html__( 'Settings → AI Connections', 'wp-powersuite' ) . '</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.', 'wp-powersuite' ),
					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
				echo wp_kses_post(
					sprintf(
						/* translators: %s: 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.', 'wp-powersuite' ),
						$free_monthly_limit,
						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', 'wp-powersuite' ); ?></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.', 'wp-powersuite' ); ?></p>
		<?php wpps_ai_alt_accordion_close(); ?>

		<?php
		wpps_ai_alt_accordion_open(
			[
				'id'    => 'generation',
				'title' => __( 'Generation & automation', 'wp-powersuite' ),
				'tip'   => __( 'Model, writing style, custom prompt, output options, and automation.', 'wp-powersuite' ),
				'open'  => true,
			]
		);
		?>
		<div class="wpps-ai-alt-settings-group">
			<h3 class="wpps-ai-alt-settings-group-title"><?php esc_html_e( 'Writing', 'wp-powersuite' ); ?></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', 'wp-powersuite' ); ?></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', 'wp-powersuite' ); ?></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', 'wp-powersuite' ) . ')';
								}
								?>
								<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)', 'wp-powersuite' ); ?></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', 'wp-powersuite' ); ?></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.', 'wp-powersuite' ); ?></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', 'wp-powersuite' ); ?></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.', 'wp-powersuite' ); ?>"><?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.', 'wp-powersuite' ); ?></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:', 'wp-powersuite' ); ?></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.', 'wp-powersuite' ); ?></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.', 'wp-powersuite' ); ?></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.', 'wp-powersuite' ); ?></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:', 'wp-powersuite' ); ?></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:', 'wp-powersuite' ); ?></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', 'wp-powersuite' ); ?></h3>
			<p class="wpps-ai-alt-settings-group-desc"><?php esc_html_e( 'Choose when alt text is created and how results are saved.', 'wp-powersuite' ); ?></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', 'wp-powersuite' ); ?></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.', 'wp-powersuite' ); ?></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.', 'wp-powersuite' ); ?></span>
					<span class="wpps-ai-alt-option-note"><?php esc_html_e( 'Bulk Generate and single-image actions are always available too.', 'wp-powersuite' ); ?></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', 'wp-powersuite' ); ?></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.', 'wp-powersuite' ); ?></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', 'wp-powersuite' ); ?></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.', 'wp-powersuite' ); ?></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', 'wp-powersuite' ); ?></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.', 'wp-powersuite' ); ?></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.', 'wp-powersuite' ); ?></span>
					</span>
				</label>
			</div>
		</div>
		<?php wpps_ai_alt_accordion_close(); ?>

		<?php
		wpps_ai_alt_accordion_open(
			[
				'id'    => 'seo-keywords',
				'title' => __( 'SEO keywords', 'wp-powersuite' ),
				'tip'   => __( 'Use focus keyphrases from SEO plugins or post titles when generating alt text.', 'wp-powersuite' ),
			]
		);
		?>
		<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.', 'wp-powersuite' ); ?></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.', 'wp-powersuite' ); ?></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.', 'wp-powersuite' ); ?></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.', 'wp-powersuite' ); ?></label>
			</div>
		</div>
		<?php wpps_ai_alt_accordion_close(); ?>

		<?php
		wpps_ai_alt_accordion_open(
			[
				'id'    => 'ai-rewrite',
				'title' => __( 'AI rewrite prompt', 'wp-powersuite' ),
				'tip'   => __( 'Optionally rewrite generated alt text with a custom prompt.', 'wp-powersuite' ),
			]
		);
		?>
		<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', 'wp-powersuite' ); ?></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}}', 'wp-powersuite' ); ?>"><?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.', 'wp-powersuite' ); ?></span>
			<span class="wpps-ai-alt-help"><?php esc_html_e( 'Leave blank to skip the rewrite step.', 'wp-powersuite' ); ?></span>
		</div>
		<?php wpps_ai_alt_accordion_close(); ?>

		<?php
		wpps_ai_alt_accordion_open(
			[
				'id'    => 'post-refresh',
				'title' => __( 'Post & page refresh', 'wp-powersuite' ),
				'tip'   => __( 'Bulk refresh images embedded in post and page content from the Posts/Pages list.', 'wp-powersuite' ),
			]
		);
		?>
		<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.', 'wp-powersuite' ); ?></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.', 'wp-powersuite' ); ?></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.', 'wp-powersuite' ); ?></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', 'wp-powersuite' ); ?></label>
			</div>
		</div>
		<?php wpps_ai_alt_accordion_close(); ?>

		<?php if ( $has_woo ) : ?>
			<?php
			wpps_ai_alt_accordion_open(
				[
					'id'    => 'woocommerce',
					'title' => __( 'WooCommerce', 'wp-powersuite' ),
					'tip'   => __( 'Control how product context is used for WooCommerce images.', 'wp-powersuite' ),
				]
			);
			?>
			<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.', 'wp-powersuite' ); ?></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.', 'wp-powersuite' ); ?></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.', 'wp-powersuite' ); ?></p>
				</div>
			</div>
			<?php wpps_ai_alt_accordion_close(); ?>
		<?php endif; ?>

		<?php
		wpps_ai_alt_accordion_open(
			[
				'id'    => 'filtering',
				'title' => __( 'Image filtering', 'wp-powersuite' ),
				'tip'   => __( 'Limit which images are eligible for automatic, bulk, and on-demand generation.', 'wp-powersuite' ),
			]
		);
		?>
		<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:', 'wp-powersuite' ); ?></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', 'wp-powersuite' ); ?>" />
				</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', 'wp-powersuite' ); ?></span>
				<span class="wpps-ai-alt-help"><?php esc_html_e( 'Leave blank to process all image types.', 'wp-powersuite' ); ?></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.', 'wp-powersuite' ); ?></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:', 'wp-powersuite' ); ?></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.', 'wp-powersuite' ); ?></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...', 'wp-powersuite' ) ); ?>"
							data-label-single="<?php echo esc_attr( __( '1 post type excluded', 'wp-powersuite' ) ); ?>"
							data-label-multi="<?php echo esc_attr( __( '%d post types excluded', 'wp-powersuite' ) ); ?>">
							<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...', 'wp-powersuite' ); ?></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...', 'wp-powersuite' ); ?>" 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', 'wp-powersuite' ); ?></button>
									<button type="button" class="wpps-ct-multiselect__action" data-action="clear"><?php esc_html_e( 'Clear', 'wp-powersuite' ); ?></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.', 'wp-powersuite' ); ?></span>
				<span class="wpps-ai-alt-help"><?php esc_html_e( 'Leave empty to process images from all post types.', 'wp-powersuite' ); ?></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.', 'wp-powersuite' ); ?></span>
					<span class="wpps-ai-alt-option-desc"><?php esc_html_e( 'Useful if WordPress references old images which have been deleted.', 'wp-powersuite' ); ?></span>
				</span>
			</label>
		</div>
		<?php wpps_ai_alt_accordion_close(); ?>

		<?php
		wpps_ai_alt_accordion_open(
			[
				'id'    => 'advanced',
				'title' => __( 'Advanced', 'wp-powersuite' ),
				'tip'   => __( 'Batch size, timeouts, metadata, lazy-loaded images, and multisite API keys.', 'wp-powersuite' ),
			]
		);
		?>
		<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', 'wp-powersuite' ); ?></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.', 'wp-powersuite' ); ?></span>
		</div>

		<div class="wpps-ai-alt-settings-group">
			<h3 class="wpps-ai-alt-settings-group-title"><?php esc_html_e( 'Generation', 'wp-powersuite' ); ?></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', 'wp-powersuite' ); ?></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.', 'wp-powersuite' ); ?></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.', 'wp-powersuite' ); ?></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.', 'wp-powersuite' ); ?></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', 'wp-powersuite' ); ?></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', 'wp-powersuite' ); ?></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', 'wp-powersuite' ); ?>" />
				</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.', 'wp-powersuite' ); ?></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 disabled( ! FeatureGate::featureEnabled( 'client_safe' ) ); ?> />
				<?php esc_html_e( 'Hide token counts and cost estimates', 'wp-powersuite' ); ?>
				<?php if ( ! FeatureGate::featureEnabled( 'client_safe' ) ) : ?>
					<span class="wpps-pro-badge"><?php esc_html_e( 'Premium', 'wp-powersuite' ); ?></span>
				<?php endif; ?>
			</label>
		</div>

		<div class="wpps-ai-alt-settings-group">
			<h3 class="wpps-ai-alt-settings-group-title"><?php esc_html_e( 'History & logs', 'wp-powersuite' ); ?></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', 'wp-powersuite' ); ?></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).', 'wp-powersuite' ); ?></span>
			</div>

			<h4 class="wpps-ai-alt-settings-group-title"><?php esc_html_e( 'Recent errors', 'wp-powersuite' ); ?></h4>
			<?php if ( $recent_errors === [] ) : ?>
				<p class="wpps-ai-alt-help"><?php esc_html_e( 'No recent generation failures.', 'wp-powersuite' ); ?></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', 'wp-powersuite' ); ?></th>
								<th><?php esc_html_e( 'Image', 'wp-powersuite' ); ?></th>
								<th><?php esc_html_e( 'Error', 'wp-powersuite' ); ?></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', 'wp-powersuite' ) ) ); ?></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', 'wp-powersuite' ); ?></a></p>
			<?php endif; ?>
		</div>
		<?php wpps_ai_alt_accordion_close(); ?>

		<?php
		wpps_ai_alt_accordion_open(
			[
				'id'    => 'access',
				'title' => __( 'Access', 'wp-powersuite' ),
				'tip'   => __( 'Who can use the module, monthly limits, and optional scheduled reports.', 'wp-powersuite' ),
			]
		);
		?>
		<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)', 'wp-powersuite' ); ?></span>
					<span class="wpps-ai-alt-option-desc"><?php esc_html_e( 'Only administrators can access the AI Alt Text menu and save settings.', 'wp-powersuite' ); ?></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', 'wp-powersuite' ); ?>
				<?php if ( ! $is_pro ) : ?>
					<span class="wpps-pro-badge"><?php esc_html_e( 'Premium', 'wp-powersuite' ); ?></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', 'wp-powersuite' ); ?></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 else : ?>
					<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 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.', 'wp-powersuite' ); ?>
				<?php else : ?>
					<?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.', 'wp-powersuite' ),
							$free_monthly_limit,
							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', 'wp-powersuite' ); ?></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', 'wp-powersuite' ); ?></th>
						<th><?php esc_html_e( 'Generate', 'wp-powersuite' ); ?></th>
						<th><?php esc_html_e( 'View dashboard', 'wp-powersuite' ); ?></th>
						<th><?php esc_html_e( 'Manage settings', 'wp-powersuite' ); ?></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', 'wp-powersuite' ); ?>
				<?php if ( ! FeatureGate::featureEnabled( 'scheduled_scan' ) ) : ?>
					<span class="wpps-pro-badge"><?php esc_html_e( 'Premium', 'wp-powersuite' ); ?></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.', 'wp-powersuite' ),
							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', 'wp-powersuite' ); ?></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', 'wp-powersuite' ); ?></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', 'wp-powersuite' ); ?></option>
					<option value="daily" <?php selected( $settings['scan_schedule'] ?? '', 'daily' ); ?>><?php esc_html_e( 'Daily', 'wp-powersuite' ); ?></option>
					<option value="weekly" <?php selected( $settings['scan_schedule'] ?? '', 'weekly' ); ?>><?php esc_html_e( 'Weekly', 'wp-powersuite' ); ?></option>
					<option value="monthly" <?php selected( $settings['scan_schedule'] ?? '', 'monthly' ); ?>><?php esc_html_e( 'Monthly', 'wp-powersuite' ); ?></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', 'wp-powersuite' ); ?></label>
		</div>
		</div>

		<div class="wpps-ai-alt-settings-group">
			<h3 class="wpps-ai-alt-settings-group-title"><?php esc_html_e( 'Diagnostics', 'wp-powersuite' ); ?></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)', 'wp-powersuite' ); ?>
				</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;">
				<li><?php echo esc_html( sprintf( /* translators: 1: WP version, 2: PHP version */ __( 'WordPress %1$s · PHP %2$s', 'wp-powersuite' ), (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', 'wp-powersuite' ), (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', 'wp-powersuite' ), (string) $diag['provider'], (string) $diag['model'] ) ); ?></li>
				<li><?php echo esc_html( ! empty( $diag['has_api_key'] ) ? __( 'API key: configured', 'wp-powersuite' ) : __( 'API key: missing', 'wp-powersuite' ) ); ?></li>
				<li><?php echo esc_html( sprintf( /* translators: %s: queue status */ __( 'Background queue: %s', 'wp-powersuite' ), (string) $diag['queue_status'] ) ); ?></li>
				<li><?php echo esc_html( ! empty( $diag['cron_disabled'] ) ? __( 'WP-Cron: disabled (DISABLE_WP_CRON)', 'wp-powersuite' ) : __( 'WP-Cron: enabled', 'wp-powersuite' ) ); ?></li>
				<li><?php echo esc_html( ! empty( $diag['uploads_writable'] ) ? __( 'Uploads directory: readable', 'wp-powersuite' ) : __( 'Uploads directory: error', 'wp-powersuite' ) ); ?></li>
				<li><?php echo esc_html( ( ! empty( $diag['gd_loaded'] ) || ! empty( $diag['imagick_loaded'] ) ) ? __( 'Image editor: available', 'wp-powersuite' ) : __( 'Image editor: missing (GD/Imagick)', 'wp-powersuite' ) ); ?></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', 'wp-powersuite' ),
								(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', 'wp-powersuite' ); ?></button>
	</div>
</form>
