<?php
/**
 * Reusable Pagination Partial
 *
 * Expected variables:
 * - $current_page:  int    Current page number (1-based)
 * - $total_pages:   int    Total number of pages
 * - $total_items:   int    Total number of items
 * - $per_page:      int    Items per page
 * - $offset:        int    Current offset
 * - $base_url:      string Base URL for pagination links (with existing query params)
 * - $item_label:    string Label for items (e.g. 'clients', 'projects')
 *
 * @package Freelance_Studio
 * @since 8.5.4
 */

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

if (!isset($total_pages) || $total_pages <= 1) {
    return;
}
?>

<div class="fs-pagination">
    <span class="fs-pagination-info">
        <?php printf(
            /* translators: 1. start number, 2. end number, 3. total, 4. item label */
            esc_html__('Showing %1$d–%2$d of %3$d %4$s', 'freelance-studio'),
            $offset + 1,
            min($offset + $per_page, $total_items),
            $total_items,
            $item_label
        ); ?>
    </span>
    <div class="fs-pagination-links">
        <?php if ($current_page > 1): ?>
            <a href="<?php echo esc_url(add_query_arg('paged', $current_page - 1, $base_url)); ?>" class="fs-btn fs-btn-secondary fs-btn-small">&laquo; <?php esc_html_e('Prev', 'freelance-studio'); ?></a>
        <?php endif; ?>
        
        <?php
        $start_page = max(1, $current_page - 2);
        $end_page = min($total_pages, $current_page + 2);
        
        if ($start_page > 1): ?>
            <a href="<?php echo esc_url(add_query_arg('paged', 1, $base_url)); ?>" class="fs-pagination-link">1</a>
            <?php if ($start_page > 2): ?><span class="fs-pagination-ellipsis">&hellip;</span><?php endif; ?>
        <?php endif; ?>
        
        <?php for ($i = $start_page; $i <= $end_page; $i++): ?>
            <?php if ($i == $current_page): ?>
                <span class="fs-pagination-current"><?php echo esc_html($i); ?></span>
            <?php else: ?>
                <a href="<?php echo esc_url(add_query_arg('paged', $i, $base_url)); ?>" class="fs-pagination-link"><?php echo esc_html($i); ?></a>
            <?php endif; ?>
        <?php endfor; ?>
        
        <?php if ($end_page < $total_pages): ?>
            <?php if ($end_page < $total_pages - 1): ?><span class="fs-pagination-ellipsis">&hellip;</span><?php endif; ?>
            <a href="<?php echo esc_url(add_query_arg('paged', $total_pages, $base_url)); ?>" class="fs-pagination-link"><?php echo esc_html($total_pages); ?></a>
        <?php endif; ?>
        
        <?php if ($current_page < $total_pages): ?>
            <a href="<?php echo esc_url(add_query_arg('paged', $current_page + 1, $base_url)); ?>" class="fs-btn fs-btn-secondary fs-btn-small"><?php esc_html_e('Next', 'freelance-studio'); ?> &raquo;</a>
        <?php endif; ?>
    </div>
    <div class="fs-pagination-perpage">
        <select onchange="window.location.href=this.value;">
            <?php foreach (array(10, 25, 50, 100) as $pp): ?>
                <option value="<?php echo esc_url(add_query_arg(array('per_page' => $pp, 'paged' => 1), $base_url)); ?>" <?php selected($per_page, $pp); ?>><?php echo esc_html($pp); ?> <?php esc_html_e('per page', 'freelance-studio'); ?></option>
            <?php endforeach; ?>
        </select>
    </div>
</div>
