<?php
/**
 * Archive template wrapper for Palace Properties.
 * Loads the active template's card.php for each property.
 */
if ( ! defined( 'ABSPATH' ) ) exit;

get_header();

$template_slug = sanitize_text_field( get_option( 'ipp_select_template', 'palace-property' ) );
$options = get_option( 'ipp_settings', [] );
$posts_per_page = absint( $options['property_per_page'] ?? 9 );
$display_fields = get_option( 'ipp_display_fields', IntegratePalace_Properties_Settings::get_default_display_fields() );
?>

<div class="ipp-archive-page ipp-container">

    <?php do_action( 'ipp_before_archive', $template_slug ); ?>

    <?php
    // Search/filter bar
    do_action( 'ipp_before_search_bar' );
    $search_template = INTEGRATE_PALACE_P_PLUGIN_DIR . 'public/templates/' . sanitize_file_name( $template_slug ) . '/search.php';
    $search_template = apply_filters( 'ipp_search_template', $search_template, $template_slug );
    if ( file_exists( $search_template ) ) {
        include $search_template;
    } else {
        // Default search bar
        include INTEGRATE_PALACE_P_PLUGIN_DIR . 'public/templates/search-default.php';
    }
    do_action( 'ipp_after_search_bar' );
    ?>

    <div class="ipp-archive-header">
        <div class="ipp-archive-count">
            <?php
            global $wp_query;
            printf(
                esc_html( _n( '%s Property', '%s Properties', $wp_query->found_posts, 'integrate-palace-properties' ) ),
                '<strong>' . esc_html( number_format_i18n( $wp_query->found_posts ) ) . '</strong>'
            );
            ?>
        </div>
        <div class="ipp-archive-view-toggle">
            <button class="ipp-btn ipp-btn--sm ipp-view-grid active" aria-label="<?php esc_attr_e( 'Grid view', 'integrate-palace-properties' ); ?>">
                <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><rect x="3" y="3" width="7" height="7"/><rect x="14" y="3" width="7" height="7"/><rect x="14" y="14" width="7" height="7"/><rect x="3" y="14" width="7" height="7"/></svg>
            </button>
            <button class="ipp-btn ipp-btn--sm ipp-view-list" aria-label="<?php esc_attr_e( 'List view', 'integrate-palace-properties' ); ?>">
                <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><line x1="8" y1="6" x2="21" y2="6"/><line x1="8" y1="12" x2="21" y2="12"/><line x1="8" y1="18" x2="21" y2="18"/><circle cx="3" cy="6" r="1" fill="currentColor"/><circle cx="3" cy="12" r="1" fill="currentColor"/><circle cx="3" cy="18" r="1" fill="currentColor"/></svg>
            </button>
        </div>
    </div>

    <div class="ipp-grid" id="ipp-properties-grid">
        <?php
        if ( have_posts() ) :
            while ( have_posts() ) :
                the_post();
                $post_id = get_the_ID();
                $prop = new IPP_Property_Helper( $post_id );

                do_action( 'ipp_before_card', $post_id, $prop );

                $card_path = INTEGRATE_PALACE_P_PLUGIN_DIR . 'public/templates/' . sanitize_file_name( $template_slug ) . '/card.php';
                $theme_card = get_stylesheet_directory() . '/palace-properties/card.php';
                if ( file_exists( $theme_card ) ) {
                    $card_path = $theme_card;
                }
                $card_path = apply_filters( 'ipp_card_template', $card_path, $template_slug );
                if ( file_exists( $card_path ) ) {
                    include $card_path;
                }

                do_action( 'ipp_after_card', $post_id, $prop );
            endwhile;
        else :
            ?>
            <div class="ipp-no-results">
                <h2><?php esc_html_e( 'No properties found', 'integrate-palace-properties' ); ?></h2>
                <p><?php esc_html_e( 'Try adjusting your search filters or check back later.', 'integrate-palace-properties' ); ?></p>
            </div>
            <?php
        endif;
        ?>
    </div>

    <?php if ( $wp_query->max_num_pages > 1 ) : ?>
        <div class="ipp-pagination">
            <button id="ipp-load-more" class="ipp-btn ipp-btn--primary ipp-btn--lg" data-page="1" data-max="<?php echo esc_attr( $wp_query->max_num_pages ); ?>">
                <?php esc_html_e( 'Load More Properties', 'integrate-palace-properties' ); ?>
                <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><polyline points="6 9 12 15 18 9"/></svg>
            </button>
        </div>
    <?php endif; ?>

    <?php do_action( 'ipp_after_archive', $template_slug ); ?>

</div>

<?php get_footer(); ?>
