<?php
/**
 * Pagination Template
 * 
 * This template displays pagination links for the property search.
 * 
 * HOW TO OVERRIDE THIS TEMPLATE:
 * 1. Copy this file from: wp-content/plugins/havenlytics/templates/search/pagination.php
 * 2. Paste it to your theme at: wp-content/themes/your-theme/havenlytics/search/pagination.php
 * 3. Modify the copied file to customize the pagination display
 * 
 * @package     Havenlytics
 * @subpackage  Templates/search
 * @since       2.0.0
 */

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

global $wp_query;

$hvnly_current_page      = max( 1, get_query_var( 'paged' ) );
$hvnly_total_pages       = $wp_query->max_num_pages;
$hvnly_found_posts       = $wp_query->found_posts;
$hvnly_posts_per_page    = $wp_query->get( 'posts_per_page' );

// Don't show pagination if only one page
if ( $hvnly_total_pages <= 1 || $hvnly_found_posts <= $hvnly_posts_per_page ) {
    return;
}

$hvnly_actual_max_pages = ceil( $hvnly_found_posts / $hvnly_posts_per_page );
$hvnly_start_page       = max( 1, $hvnly_current_page - 2 );
$hvnly_end_page         = min( $hvnly_actual_max_pages, $hvnly_current_page + 2 );

if ( $hvnly_current_page <= 2 ) {
    $hvnly_end_page = min( 5, $hvnly_actual_max_pages );
}

if ( $hvnly_current_page >= $hvnly_actual_max_pages - 1 ) {
    $hvnly_start_page = max( 1, $hvnly_actual_max_pages - 4 );
}
?>

<!-- NO outer div wrapper - parent provides the container -->
<div class="hvnly-property-pagination">
    <div class="hvnly-property-pagination-info">
        <?php
        $hvnly_start = ( ( $hvnly_current_page - 1 ) * $hvnly_posts_per_page ) + 1;
        $hvnly_end   = min( $hvnly_current_page * $hvnly_posts_per_page, $hvnly_found_posts );

        $hvnly_start_span = '<span>' . esc_html( $hvnly_start ) . '</span>';
        $hvnly_end_span   = '<span>' . esc_html( $hvnly_end ) . '</span>';
        $hvnly_total_span = '<span>' . esc_html( $hvnly_found_posts ) . '</span>';

        $hvnly_pagination_text = sprintf(
            /* translators: %1$s: start, %2$s: end, %3$s: total properties */
            __( 'Showing %1$s-%2$s of %3$s properties', 'havenlytics' ),
            $hvnly_start_span,
            $hvnly_end_span,
            $hvnly_total_span
        );

        echo wp_kses_post( $hvnly_pagination_text );
        ?>
    </div>

    <div class="hvnly-property-pagination-list">
        <?php if ( $hvnly_current_page > 1 ) : ?>
        <div class="hvnly-property-pagination-item hvnly-property-pagination-prev"
            data-page="<?php echo esc_attr( $hvnly_current_page - 1 ); ?>">
            <i class="fas fa-chevron-left"></i>
        </div>
        <?php endif; ?>

        <?php for ( $hvnly_i = $hvnly_start_page; $hvnly_i <= $hvnly_end_page; $hvnly_i++ ) :
            $hvnly_active = ( $hvnly_i == $hvnly_current_page ) ? 'active' : '';
        ?>
        <div class="hvnly-property-pagination-item <?php echo esc_attr( $hvnly_active ); ?>"
            data-page="<?php echo esc_attr( $hvnly_i ); ?>">
            <?php echo esc_html( $hvnly_i ); ?>
        </div>
        <?php endfor; ?>

        <?php if ( $hvnly_current_page < $hvnly_actual_max_pages ) : ?>
        <div class="hvnly-property-pagination-item hvnly-property-pagination-next"
            data-page="<?php echo esc_attr( $hvnly_current_page + 1 ); ?>">
            <i class="fas fa-chevron-right"></i>
        </div>
        <?php endif; ?>
    </div>
</div>