<?php
/**
 * Property Results Template Part
 *
 * Displays property results for archive/search
 *
 * @package HomeProz
 */

// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}

// Get filter values
$current_type = isset($_GET['property_type']) ? sanitize_text_field($_GET['property_type']) : '';
$current_status = isset($_GET['property_status']) ? sanitize_text_field($_GET['property_status']) : '';
$current_location = isset($_GET['property_location']) ? sanitize_text_field($_GET['property_location']) : '';
$current_min_price = isset($_GET['min_price']) ? intval($_GET['min_price']) : '';
$current_max_price = isset($_GET['max_price']) ? intval($_GET['max_price']) : '';
$current_beds = isset($_GET['beds']) ? intval($_GET['beds']) : '';
$current_sort = isset($_GET['sort']) ? sanitize_text_field($_GET['sort']) : 'newest';

// Build query args
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
$args = array(
    'post_type' => 'property',
    'posts_per_page' => 12,
    'paged' => $paged,
);

// Taxonomy filters
$tax_query = array();

if ($current_type) {
    $tax_query[] = array(
        'taxonomy' => 'property_type',
        'field' => 'slug',
        'terms' => $current_type,
    );
}

if ($current_status) {
    $tax_query[] = array(
        'taxonomy' => 'property_status',
        'field' => 'slug',
        'terms' => $current_status,
    );
}

if ($current_location) {
    $tax_query[] = array(
        'taxonomy' => 'property_location',
        'field' => 'slug',
        'terms' => $current_location,
    );
}

if (!empty($tax_query)) {
    $args['tax_query'] = $tax_query;
    if (count($tax_query) > 1) {
        $args['tax_query']['relation'] = 'AND';
    }
}

// Meta query for price and bedrooms
$meta_query = array();

if ($current_min_price) {
    $meta_query[] = array(
        'key' => 'property_price',
        'value' => $current_min_price,
        'type' => 'NUMERIC',
        'compare' => '>=',
    );
}

if ($current_max_price) {
    $meta_query[] = array(
        'key' => 'property_price',
        'value' => $current_max_price,
        'type' => 'NUMERIC',
        'compare' => '<=',
    );
}

if ($current_beds) {
    $meta_query[] = array(
        'key' => 'bedrooms',
        'value' => $current_beds,
        'type' => 'NUMERIC',
        'compare' => '>=',
    );
}

if (!empty($meta_query)) {
    $args['meta_query'] = $meta_query;
    if (count($meta_query) > 1) {
        $args['meta_query']['relation'] = 'AND';
    }
}

// Sorting
switch ($current_sort) {
    case 'oldest':
        $args['orderby'] = 'date';
        $args['order'] = 'ASC';
        break;
    case 'price_low':
        $args['meta_key'] = 'property_price';
        $args['orderby'] = 'meta_value_num';
        $args['order'] = 'ASC';
        break;
    case 'price_high':
        $args['meta_key'] = 'property_price';
        $args['orderby'] = 'meta_value_num';
        $args['order'] = 'DESC';
        break;
    case 'newest':
    default:
        $args['orderby'] = 'date';
        $args['order'] = 'DESC';
        break;
}

$properties = new WP_Query($args);
?>

<!-- Results Meta -->
<div class="properties-meta">
    <p class="properties-count">
        <?php if ($properties->found_posts > 0) : ?>
            Showing <strong><?php echo esc_html($properties->found_posts); ?></strong>
            <?php echo $properties->found_posts === 1 ? 'property' : 'properties'; ?>
        <?php else : ?>
            No properties found
        <?php endif; ?>
    </p>
</div>

<?php if ($properties->have_posts()) : ?>
    <div class="properties-grid">
        <?php
        while ($properties->have_posts()) :
            $properties->the_post();
            get_template_part('template-parts/property/property-card');
        endwhile;
        ?>
    </div>

    <?php
    // Pagination
    $big = 999999999;
    $pagination = paginate_links(array(
        'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))),
        'format' => '?paged=%#%',
        'current' => max(1, $paged),
        'total' => $properties->max_num_pages,
        'prev_text' => '&larr; Previous',
        'next_text' => 'Next &rarr;',
        'type' => 'array',
    ));

    if ($pagination) :
        ?>
        <nav class="pagination" aria-label="Property pagination">
            <div class="nav-links">
                <?php foreach ($pagination as $link) : ?>
                    <?php echo $link; ?>
                <?php endforeach; ?>
            </div>
        </nav>
    <?php endif; ?>

<?php else : ?>
    <div class="no-properties">
        <h3>No Properties Found</h3>
        <p>We couldn't find any properties matching your criteria. Try adjusting your filters or browse all properties.</p>
        <a href="<?php echo esc_url(get_post_type_archive_link('property')); ?>" class="btn btn-primary">View All Properties</a>
    </div>
<?php endif; ?>

<?php wp_reset_postdata(); ?>
