<?php
/**
 * Custom Pagination Template.
 *
 * Renders numeric pagination links with Bootstrap styling.
 *
 * @package    Wira
 * @since      1.0.0
 */

namespace Wira\Templates;

if ( ! class_exists( 'Custom_Pagination' ) ) {

	/**
	 * Custom_Pagination Class.
	 *
	 * Handles rendering of pagination with Bootstrap markup.
	 *
	 * @since 1.0.0
	 */
	class Custom_Pagination {

		/**
		 * Render pagination links.
		 *
		 * Generates numbered pagination links and wraps them in
		 * Bootstrap `.pagination` and `.page-item` classes.
		 *
		 * @since 1.0.0
		 * @return void
		 */
		public function render() {
			$args = array(
				'mid_size'           => 2,
				'prev_text'          => false, // Left empty → icons/text can be injected via CSS.
				'next_text'          => false, // Same here.
				'screen_reader_text' => __( 'Posts navigation', 'wira' ),
				'type'               => 'array', // Return array for easier looping.
			);

			$pages = paginate_links( $args );

			// Bail if no pagination.
			if ( ! is_array( $pages ) ) {
				return;
			}

			echo '<nav aria-label="' . esc_attr__( 'Pagination', 'wira' ) . '">';
			echo '<ul class="pagination theme-pagination justify-content-center">';

			foreach ( $pages as $page ) {
				// Detect active state.
				$active      = strpos( $page, 'current' ) !== false ? ' active' : '';
				$active_attr = esc_attr( trim( $active ) );

				// Replace WP default "page-numbers" class with Bootstrap's "page-link".
				$pagelink = str_replace( 'page-numbers', 'page-link', $page );

				echo '<li class="page-item ' . $active_attr . '">' . wp_kses_post( $pagelink ) . '</li>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Safe: already escaped by Elementor
			}

			echo '</ul>';
			echo '</nav>';
		}
	}
}
