<?php
/**
 * Template Name: RentPress City Term
 *
 * @package RentPress Templates
 */
add_action('wp_enqueue_scripts', 'rentpress_term_style');
function rentpress_term_style()
{
  wp_enqueue_style('city-archive', RENTPRESS_PLUGIN_PUBLIC_TEMPLATES_DIR . 'template-city-archive-styles.css');
}

get_header();

$options = get_option('rentpress_options');
$default_city_image = $options['rentpress_default_city_image_section'];
$accentColor = $options['rentpress_accent_color_section_input'];

$city_object = get_queried_object();
$city_name = $city_object->name;
$city_meta = get_term_meta($city_object->term_id);

$featured_image = $city_meta['rentpress_custom_field_city_image'][0]
    ?: $default_city_image;

$short_description = $city_object->description ?? '';
$extended_content = $city_meta['rentpress_custom_field_city_extended_content'][0] ?? '';
$shortcode = $city_meta['rentpress_custom_field_city_shortcode'][0] ?? '';

// Fetch all other cities, excluding current
$all_cities = get_terms([
    'taxonomy' => 'city',
    'hide_empty' => true,
    'exclude' => $city_object->term_id,
]);

$cities_to_show = [];
if (!is_wp_error($all_cities) && is_array($all_cities) && !empty($all_cities)) {
    shuffle($all_cities);
    $cities_to_show = array_slice($all_cities, 0, 3);
}
?>

<main class="main-content" role="main">
    <div class="rentpress-page-hero">
        <header class="rentpress-page-hero-full">
            <h1 class="rentpress-inherited-font-family rentpress-page-title">
                <?php echo 'Apartments In <br />' . esc_html($city_name); ?>
            </h1>
            <img src="<?php echo esc_url($featured_image); ?>"
                class="lazyload rentpress-city-banner rentpress-template-banner"
                alt="<?php echo esc_attr($city_name); ?>">
        </header>
    </div>

    <section class="clearfix rentpress-term-short-description">
        <div class="container">
            <h2 class="rentpress-inherited-font-family rentpress-taxonomy-description-header"
                style="color:<?php echo esc_attr($accentColor); ?>;">
                <?php echo esc_html($city_name); ?>
            </h2>
            <?php echo wp_kses_post($short_description); ?>
        </div>
    </section>

    <section class="clearfix rentpress-term-properties">
        <?php echo do_shortcode('[rentpress_property_search terms="' . esc_attr($city_name) . '" HIDEFILTERS=true]');
        ?>
    </section>

    <?php if ($extended_content) : ?>
    <section class="clearfix rentpress-extended-term-description">
        <div class="container">
            <?php echo wp_kses_post($extended_content); ?>
        </div>
    </section>
    <?php endif; ?>

    <?php if ($shortcode) : ?>
    <section class="clearfix rentpress-term-shortcode">
        <div class="container">
            <?php echo do_shortcode($shortcode); ?>
        </div>
    </section>
    <?php endif; ?>

    <?php if (!empty($cities_to_show)) : ?>
    <section class="rentpress-other-cities">
        <h3 style="text-align:center" class="rentpress-inherited-font-family text-center accent-color">
            Explore Other Options
        </h3>
        <div class="city-term-grid">
            <?php
            // Pre-fetch meta for all other cities to avoid repeated get_term_meta calls
            $other_city_meta = [];
              foreach ($cities_to_show as $other_city) {
                  $other_image = get_term_meta($other_city->term_id, 'rentpress_custom_field_city_image', true);
                  $other_city_meta[$other_city->term_id] = [
                      'image' => $other_image ?: $default_city_image
                  ];
              }
            ?>

            <?php foreach ($cities_to_show as $other_city) :
                $other_image = $other_city_meta[$other_city->term_id]['image'];
            ?>
            <div class="is-rp-city flex-grid-thirds">
                <a href="<?php echo esc_url(get_term_link($other_city->term_id)); ?>">
                    <figure class="rp-city-figure">
                        <img loading="lazy" class="lazyload rp-city-image" src="<?php echo esc_url($other_image); ?>"
                            alt="<?php echo esc_attr($other_city->name); ?>">
                    </figure>
                    <section class="rp-city-details">
                        <div class="rp-city-top">
                            <h2 class="rentpress-inherited-font-family rp-city-name"
                                style="font-weight: bold; color:<?php echo esc_attr($accentColor); ?>;">
                                <?php echo esc_html($other_city->name); ?>
                            </h2>
                        </div>
                    </section>
                </a>
            </div>
            <?php endforeach; ?>
        </div>
    </section>
    <?php endif; ?>

</main>

<?php get_footer(); ?>