<?php
// Prevent direct access.
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * Shared Pagination Template for Layout 4 (Purple Theme)
 *
 * @since 4.2.14
 *
 * Expected variables:
 * @var array    $pagination       Pagination data with total_pages, current_page keys
 * @var string   $pagination_type  Type identifier ('comments' or 'posts')
 * @var callable $url_builder_func URL builder function name
 */

if ( $pagination['total_pages'] <= 1 ) {
    return;
}

// Build base URL and query args for pagination
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$parsed_url = wp_parse_url( wp_unslash( $_SERVER['REQUEST_URI'] ?? '/' ) );
$base_url   = esc_url_raw( $parsed_url['path'] ?? '/' );

// Parse current URL to get all parameters
$all_params = [];
if ( ! empty( $parsed_url['query'] ) ) {
    parse_str( $parsed_url['query'], $all_params );
}

// Create clean query args with only directory-related parameters
$clean_query_args = [];
$preserve_params  = [ 'dir_page', 'orderby', 'order', 'search' ];
foreach ( $preserve_params as $param ) {
    if ( isset( $all_params[ $param ] ) ) {
        $clean_query_args[ $param ] = $all_params[ $param ];
    }
}

$current = $pagination['current_page'];
$total   = $pagination['total_pages'];

// Calculate visible page range - show max 7 pages
$max_visible = 7;
$pages       = [];

if ( $total <= $max_visible ) {
    for ( $i = 1; $i <= $total; $i++ ) {
        $pages[] = $i;
    }
} else {
    $pages[] = 1;
    $start   = max( 2, $current - 2 );
    $end     = min( $total - 1, $current + 2 );

    if ( $start > 2 ) {
        $pages[] = '...';
    }

    for ( $i = $start; $i <= $end; $i++ ) {
        $pages[] = $i;
    }

    if ( $end < $total - 1 ) {
        $pages[] = '...';
    }

    $pages[] = $total;
}

$aria_label = ( $pagination_type === 'comments' )
    ? __( 'Comments Pagination', 'wpuf-pro' )
    : __( 'Posts Pagination', 'wpuf-pro' );
?>

<div class="wpuf-ud-<?php echo esc_attr( $pagination_type ); ?>-pagination !wpuf-mt-6">
    <nav class="!wpuf-flex !wpuf-items-center !wpuf-justify-center !wpuf-gap-2" aria-label="<?php echo esc_attr( $aria_label ); ?>">

        <!-- Previous Button -->
        <?php if ( $current > 1 ) : ?>
            <a href="<?php echo esc_url( $url_builder_func( $base_url, $clean_query_args, $current - 1 ) ); ?>"
               class="wpuf-pagination-link !wpuf-inline-flex !wpuf-items-center !wpuf-px-3 !wpuf-py-2 !wpuf-text-sm !wpuf-text-purple-600 hover:!wpuf-text-purple-800">
                <svg class="!wpuf-w-5 !wpuf-h-5 !wpuf-mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 19l-7-7 7-7"></path>
                </svg>
                <?php esc_html_e( 'Previous', 'wpuf-pro' ); ?>
            </a>
        <?php else : ?>
            <span class="!wpuf-inline-flex !wpuf-items-center !wpuf-px-3 !wpuf-py-2 !wpuf-text-sm !wpuf-text-gray-300 !wpuf-cursor-not-allowed">
                <svg class="!wpuf-w-5 !wpuf-h-5 !wpuf-mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 19l-7-7 7-7"></path>
                </svg>
                <?php esc_html_e( 'Previous', 'wpuf-pro' ); ?>
            </span>
        <?php endif; ?>

        <!-- Page Numbers -->
        <div class="!wpuf-flex !wpuf-items-center !wpuf-gap-1">
            <?php foreach ( $pages as $page ) : ?>
                <?php if ( $page === '...' ) : ?>
                    <span class="!wpuf-px-3 !wpuf-py-2 !wpuf-text-sm !wpuf-text-gray-500">
                        &hellip;
                    </span>
                <?php elseif ( $page == $current ) : ?>
                    <span aria-current="page"
                          class="!wpuf-relative !wpuf-inline-flex !wpuf-items-center !wpuf-px-4 !wpuf-py-2 !wpuf-text-sm !wpuf-font-medium !wpuf-text-purple-700 !wpuf-border-t-2 !wpuf-border-purple-600">
                        <?php echo esc_html( $page ); ?>
                    </span>
                <?php else : ?>
                    <a href="<?php echo esc_url( $url_builder_func( $base_url, $clean_query_args, $page ) ); ?>"
                       class="!wpuf-no-underline wpuf-pagination-link !wpuf-relative !wpuf-inline-flex !wpuf-items-center !wpuf-px-4 !wpuf-py-2 !wpuf-text-sm !wpuf-font-medium !wpuf-text-gray-700 hover:!wpuf-text-purple-700 hover:!wpuf-border-purple-600 hover:!wpuf-border-t-2 !wpuf-transition-colors">
                        <?php echo esc_html( $page ); ?>
                    </a>
                <?php endif; ?>
            <?php endforeach; ?>
        </div>

        <!-- Next Button -->
        <?php if ( $current < $total ) : ?>
            <a href="<?php echo esc_url( $url_builder_func( $base_url, $clean_query_args, $current + 1 ) ); ?>"
               class="wpuf-pagination-link !wpuf-inline-flex !wpuf-items-center !wpuf-px-3 !wpuf-py-2 !wpuf-text-sm !wpuf-text-purple-600 hover:!wpuf-text-purple-800">
                <?php esc_html_e( 'Next', 'wpuf-pro' ); ?>
                <svg class="!wpuf-w-5 !wpuf-h-5 !wpuf-ml-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7"></path>
                </svg>
            </a>
        <?php else : ?>
            <span class="!wpuf-inline-flex !wpuf-items-center !wpuf-px-3 !wpuf-py-2 !wpuf-text-sm !wpuf-text-gray-300 !wpuf-cursor-not-allowed">
                <?php esc_html_e( 'Next', 'wpuf-pro' ); ?>
                <svg class="!wpuf-w-5 !wpuf-h-5 !wpuf-ml-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7"></path>
                </svg>
            </span>
        <?php endif; ?>

    </nav>
</div>
