<?php
/**
 * AI Alt Text processing history tab.
 *
 * @package AIAltTextGenerator
 */

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

use AIAltTextGenerator\AdminActions;
use AIAltTextGenerator\Capabilities;
use AIAltTextGenerator\HistoryListTable;
use AIAltTextGenerator\Settings;

require_once __DIR__ . '/helpers.php';

if ( ! Capabilities::canView() ) {
	echo '<div class="wpps-ai-alt-notice wpps-ai-alt-notice--warning"><p>' . esc_html__( 'You do not have permission to view processing history.', 'wp-powersuite' ) . '</p></div>';
	return;
}

if ( isset( $_GET['bulk_done'], $_GET['bulk_count'] ) ) {
	$action = sanitize_key( wp_unslash( (string) $_GET['bulk_done'] ) );
	$count  = max( 0, (int) $_GET['bulk_count'] );
	if ( $count > 0 ) {
		printf(
			'<div class="notice notice-success is-dismissible"><p>%s</p></div>',
			esc_html(
				sprintf(
					/* translators: 1: number of log rows, 2: bulk action key */
					__( 'Bulk action complete: %1$d item(s) processed (%2$s).', 'wp-powersuite' ),
					$count,
					$action
				)
			)
		);
	}
}

$list_table = new HistoryListTable();
$list_table->prepare_items();
$retention_days = (int) ( Settings::get()['log_retention_days'] ?? Settings::LOG_RETENTION_DEFAULT );
?>
<div class="wpps-ai-alt-history-page">
	<div class="wpps-ai-alt-card">
		<div class="wpps-ai-alt-card-head">
			<h2 class="wpps-ai-alt-card-title"><?php esc_html_e( 'Processed images', 'wp-powersuite' ); ?></h2>
			<p class="wpps-ai-alt-card-desc">
				<?php esc_html_e( 'Search processing history, regenerate alt text, or remove log entries.', 'wp-powersuite' ); ?>
				<?php if ( $retention_days > 0 ) : ?>
					<?php
					printf(
						' %s',
						esc_html(
							sprintf(
								/* translators: %d: number of days */
								__( 'Entries older than %d days are removed automatically.', 'wp-powersuite' ),
								$retention_days
							)
						)
					);
					?>
				<?php endif; ?>
			</p>
		</div>
		<div class="wpps-ai-alt-card-body">
			<form method="post">
				<?php
				wp_nonce_field( AdminActions::NONCE_ACTION, AdminActions::NONCE_ACTION );
				$list_table->search_box( __( 'Search history', 'wp-powersuite' ), 'wpps-ai-alt-history' );
				$list_table->display();
				?>
			</form>
		</div>
	</div>
</div>
