<?php
/**
 * Redirects Monitor Page Template
 *
 * Displays summary cards, filters, and the redirect list table.
 *
 * Available variables (set by INFYP_Admin_Pages):
 *   $redirect_list_table  INFYP_Redirect_List_Table  Prepared list table instance.
 *   $summary              array                       Summary stats from INFYP_Redirect_Table.
 *
 * @package   IPAIS\Admin
 * @since     1.1.0
 */

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

$infyp_settings = \IPAIS\Core\INFYP_Redirect_Manager::get_settings();
$infyp_enabled  = ! empty( $infyp_settings['enabled'] );
?>
<div class="wrap">
	<h1><?php esc_html_e( 'Image Redirects', 'infy-ai-image-seo-optimizer' ); ?><?php echo wp_kses_post( apply_filters( 'infyp_admin_header_badge', '' ) ); ?></h1>

	<?php if ( ! $infyp_enabled ) : ?>
		<div class="notice notice-warning">
			<p><?php esc_html_e( 'Image redirects are currently disabled. Enable them in Settings to start capturing 301 redirects when images are renamed.', 'infy-ai-image-seo-optimizer' ); ?></p>
		</div>
	<?php endif; ?>

	<!-- Summary Cards -->
	<div class="infyp-api-usage-summary">
		<div class="infyp-usage-card">
			<h3><?php esc_html_e( 'Active Redirects', 'infy-ai-image-seo-optimizer' ); ?></h3>
			<div class="infyp-usage-number"><?php echo esc_html( number_format( $summary['total_active'] ) ); ?></div>
			<div class="infyp-usage-detail"><?php esc_html_e( 'Serving 301 responses', 'infy-ai-image-seo-optimizer' ); ?></div>
		</div>

		<div class="infyp-usage-card">
			<h3><?php esc_html_e( 'Total Hits', 'infy-ai-image-seo-optimizer' ); ?></h3>
			<div class="infyp-usage-number"><?php echo esc_html( number_format( $summary['total_hits'] ) ); ?></div>
			<div class="infyp-usage-detail"><?php esc_html_e( 'All time', 'infy-ai-image-seo-optimizer' ); ?></div>
		</div>

		<div class="infyp-usage-card">
			<h3><?php esc_html_e( 'Unused Redirects', 'infy-ai-image-seo-optimizer' ); ?></h3>
			<div class="infyp-usage-number"><?php echo esc_html( number_format( $summary['zero_hit_count'] ) ); ?></div>
			<div class="infyp-usage-detail"><?php esc_html_e( 'Zero hits since creation', 'infy-ai-image-seo-optimizer' ); ?></div>
		</div>

		<div class="infyp-usage-card">
			<h3><?php esc_html_e( 'Expired / Disabled', 'infy-ai-image-seo-optimizer' ); ?></h3>
			<div class="infyp-usage-number"><?php echo esc_html( number_format( $summary['total_expired'] + $summary['total_disabled'] ) ); ?></div>
			<div class="infyp-usage-detail"><?php esc_html_e( 'Not currently serving', 'infy-ai-image-seo-optimizer' ); ?></div>
		</div>
	</div>

	<?php
	/**
	 * Fires before the redirects table.
	 *
	 * @since 1.1.0
	 *
	 * @param \IPAIS\Tables\INFYP_Redirect_List_Table $list_table The list table instance.
	 */
	do_action( 'infyp_before_redirects_table', $redirect_list_table );
	?>

	<form method="get">
		<?php // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Preserving page parameter for form submission ?>
		<input type="hidden" name="page" value="<?php echo esc_attr( isset( $_REQUEST['page'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['page'] ) ) : '' ); ?>" />
		<?php
		$redirect_list_table->search_box( __( 'Search URLs', 'infy-ai-image-seo-optimizer' ), 'redirect-search' );
		$redirect_list_table->display();
		?>
	</form>

	<?php
	/**
	 * Fires after the redirects table.
	 *
	 * @since 1.1.0
	 *
	 * @param \IPAIS\Tables\INFYP_Redirect_List_Table $list_table The list table instance.
	 */
	do_action( 'infyp_after_redirects_table', $redirect_list_table );
	?>
</div>
