<?php if (!defined('ABSPATH')) exit; ?>
<?php
/**
 * Grid Gallery - Single Property Template
 *
 * Full photo gallery grid at top (masonry-like, 3 columns).
 * Below: clean property info. Photography-first design.
 *
 * @package Integrate_Palace_Properties
 */

$prop   = new IPP_Property_Helper(get_the_ID());
$images = $prop->gallery_images('large');
?>

<?php echo $prop->schema_json_ld(); ?>

<div class="ipp-single ipp-single--grid-gallery">

    <!-- Masonry Photo Gallery Grid -->
    <?php if (!empty($images)) : ?>
    <div class="ipp-single__gallery ipp-gallery" aria-label="Property photo gallery"
         style="column-count:3;column-gap:0.5rem;margin-bottom:2.5rem;">
        <?php foreach ($images as $i => $img) : ?>
            <figure style="margin:0 0 0.5rem;break-inside:avoid;border-radius:var(--ipp-radius,4px);overflow:hidden;">
                <img src="<?php echo esc_url($img['url']); ?>"
                     alt="<?php echo esc_attr($img['alt'] ?: $prop->name() . ' - Photo ' . ($i + 1)); ?>"
                     loading="<?php echo $i < 6 ? 'eager' : 'lazy'; ?>"
                     style="width:100%;display:block;object-fit:cover;">
            </figure>
        <?php endforeach; ?>
    </div>
    <?php endif; ?>

    <!-- Property Info -->
    <div class="ipp-container" style="max-width:960px;margin:0 auto;padding:0 1rem;">

        <!-- Header -->
        <header class="ipp-single__section" style="margin-bottom:2rem;">
            <div style="display:flex;align-items:flex-start;justify-content:space-between;flex-wrap:wrap;gap:1rem;">
                <div>
                    <?php if ($prop->show('status') && $prop->status()) : ?>
                        <span class="ipp-badge <?php echo strtolower($prop->status()) === 'active' ? 'ipp-badge--active' : 'ipp-badge--inactive'; ?>" style="margin-bottom:0.5rem;display:inline-block;">
                            <?php echo esc_html($prop->status()); ?>
                        </span>
                    <?php endif; ?>

                    <h1 style="font-size:2rem;margin:0 0 0.375rem;"><?php echo esc_html(get_the_title()); ?></h1>

                    <?php if ($prop->show('address') && $prop->full_address()) : ?>
                        <p style="color:var(--ipp-color-gray-500,#6b7280);margin:0;">
                            <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M21 10c0 7-9 13-9 13s-9-6-9-13a9 9 0 0 1 18 0z"/><circle cx="12" cy="10" r="3"/></svg>
                            <?php echo esc_html($prop->full_address()); ?>
                        </p>
                    <?php endif; ?>
                </div>

                <?php if ($prop->show('rent') && $prop->formatted_rent()) : ?>
                    <p style="font-size:1.75rem;font-weight:700;color:var(--ipp-color-primary,#2563eb);margin:0;white-space:nowrap;">
                        <?php echo esc_html($prop->formatted_rent()); ?>
                        <?php if ($prop->rental_period()) : ?>
                            <span style="font-size:0.875rem;font-weight:400;color:var(--ipp-color-gray-500,#6b7280);">/ <?php echo esc_html($prop->rental_period()); ?></span>
                        <?php endif; ?>
                    </p>
                <?php endif; ?>
            </div>

            <div class="ipp-card__meta" style="margin-top:1.25rem;">
                <?php if ($prop->show('bedrooms') && $prop->bedrooms()) : ?>
                    <span class="ipp-card__meta-item" aria-label="<?php echo esc_attr($prop->bedrooms()); ?> bedrooms">
                        <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" aria-hidden="true"><path d="M3 7v11a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2V7"/><path d="M21 11H3V9a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2v2z"/><path d="M7 7V5a2 2 0 0 1 2-2h6a2 2 0 0 1 2 2v2"/></svg>
                        <?php echo esc_html($prop->bedrooms()); ?> Bed
                    </span>
                <?php endif; ?>
                <?php if ($prop->show('bathrooms') && $prop->bathrooms()) : ?>
                    <span class="ipp-card__meta-item" aria-label="<?php echo esc_attr($prop->bathrooms()); ?> bathrooms">
                        <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" aria-hidden="true"><path d="M4 12h16a1 1 0 0 1 1 1v3a4 4 0 0 1-4 4H7a4 4 0 0 1-4-4v-3a1 1 0 0 1 1-1z"/><path d="M6 12V5a2 2 0 0 1 2-2h1"/></svg>
                        <?php echo esc_html($prop->bathrooms()); ?> Bath
                    </span>
                <?php endif; ?>
                <?php if ($prop->show('ensuites') && $prop->ensuites()) : ?>
                    <span class="ipp-card__meta-item" aria-label="<?php echo esc_attr($prop->ensuites()); ?> ensuites">
                        <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" aria-hidden="true"><path d="M4 12h16a1 1 0 0 1 1 1v3a4 4 0 0 1-4 4H7a4 4 0 0 1-4-4v-3a1 1 0 0 1 1-1z"/><path d="M6 12V5a2 2 0 0 1 2-2h1"/></svg>
                        <?php echo esc_html($prop->ensuites()); ?> Ensuite
                    </span>
                <?php endif; ?>
                <?php if ($prop->show('cars') && $prop->cars()) : ?>
                    <span class="ipp-card__meta-item" aria-label="<?php echo esc_attr($prop->cars()); ?> car spaces">
                        <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" aria-hidden="true"><path d="M5 17h14M5 17a2 2 0 0 1-2-2V9a2 2 0 0 1 2-2h1l2-3h8l2 3h1a2 2 0 0 1 2 2v6a2 2 0 0 1-2 2"/><circle cx="7.5" cy="15.5" r="1.5"/><circle cx="16.5" cy="15.5" r="1.5"/></svg>
                        <?php echo esc_html($prop->cars()); ?> Car
                    </span>
                <?php endif; ?>
            </div>
        </header>

        <!-- Details -->
        <section class="ipp-single__section" aria-labelledby="ipp-gg-details" style="margin-bottom:2rem;">
            <h2 id="ipp-gg-details" class="ipp-single__section-title">Property Details</h2>

            <?php
            $details = [
                'property_code'  => ['Property Code',  $prop->property_code()],
                'property_class' => ['Property Type',   $prop->property_class()],
                'date_available'  => ['Available From',  $prop->formatted_date_available()],
                'year_built'     => ['Year Built',      $prop->year_built()],
                'stories'        => ['Stories',         $prop->stories()],
                'furnishings'    => ['Furnishings',     $prop->furnishings()],
                'floor_area'     => ['Floor Area',      $prop->floor_area()],
                'land_area'      => ['Land Area',       $prop->land_area_sqm() ? $prop->land_area_sqm() . ' sqm' : ''],
                'parking'        => ['Parking',         $prop->parking()],
                'pets_allowed'   => ['Pets Allowed',    $prop->pets_allowed()],
                'smokers_allowed'=> ['Smokers Allowed', $prop->smokers_allowed()],
            ];
            ?>
            <div style="display:grid;grid-template-columns:repeat(2,1fr);gap:0;">
                <?php foreach ($details as $key => $detail) : ?>
                    <?php if ($prop->show($key) && !empty($detail[1])) : ?>
                        <div class="ipp-single__detail-row">
                            <span class="ipp-single__detail-label"><?php echo esc_html($detail[0]); ?></span>
                            <span class="ipp-single__detail-value"><?php echo esc_html($detail[1]); ?></span>
                        </div>
                    <?php endif; ?>
                <?php endforeach; ?>
            </div>
        </section>

        <!-- Features -->
        <?php if ($prop->show('feature_details') && $prop->feature_details()) : ?>
        <section class="ipp-single__section" aria-labelledby="ipp-gg-features" style="margin-bottom:2rem;">
            <h2 id="ipp-gg-features" class="ipp-single__section-title">Features</h2>
            <ul style="list-style:none;padding:0;display:grid;grid-template-columns:repeat(3,1fr);gap:0.25rem 1.5rem;">
                <?php foreach ((array) $prop->feature_details() as $feature) : ?>
                    <li style="padding:0.375rem 0;">
                        <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="var(--ipp-color-primary,#2563eb)" stroke-width="2" aria-hidden="true"><polyline points="20 6 9 17 4 12"/></svg>
                        <?php echo esc_html($feature); ?>
                    </li>
                <?php endforeach; ?>
            </ul>
        </section>
        <?php endif; ?>

        <!-- Description -->
        <?php if ($prop->show('advert_text') && $prop->advert_text()) : ?>
        <section class="ipp-single__section" aria-labelledby="ipp-gg-desc" style="margin-bottom:2rem;">
            <h2 id="ipp-gg-desc" class="ipp-single__section-title">Description</h2>
            <div class="ipp-single__description">
                <?php echo wp_kses_post($prop->advert_text()); ?>
            </div>
        </section>
        <?php endif; ?>

        <!-- Virtual Tour -->
        <?php if ($prop->show('virtual_tour_url') && $prop->virtual_tour_url()) : ?>
        <section class="ipp-single__section" style="margin-bottom:2rem;">
            <a href="<?php echo esc_url($prop->virtual_tour_url()); ?>" target="_blank" rel="noopener noreferrer" class="ipp-btn ipp-btn--primary">
                View Virtual Tour
            </a>
        </section>
        <?php endif; ?>

        <!-- Map -->
        <?php if ($prop->show('geographic_location') && $prop->has_map_coords()) : ?>
        <section class="ipp-single__section" aria-labelledby="ipp-gg-map" style="margin-bottom:2rem;">
            <h2 id="ipp-gg-map" class="ipp-single__section-title">Location</h2>
            <div class="ipp-map">
                <iframe src="<?php echo esc_url($prop->map_embed_url()); ?>"
                        width="100%" height="400"
                        style="border:0;border-radius:var(--ipp-radius,8px);"
                        allowfullscreen loading="lazy"
                        referrerpolicy="no-referrer-when-downgrade"
                        title="Property location map"></iframe>
            </div>
        </section>
        <?php endif; ?>

        <!-- Agent -->
        <section class="ipp-single__section" aria-labelledby="ipp-gg-agent" style="margin-bottom:2rem;">
            <h2 id="ipp-gg-agent" class="ipp-single__section-title">Contact Agent</h2>
            <div class="ipp-agent" style="display:flex;align-items:center;gap:1.25rem;flex-wrap:wrap;">
                <div style="width:64px;height:64px;border-radius:50%;background:var(--ipp-color-gray-100,#f3f4f6);display:flex;align-items:center;justify-content:center;flex-shrink:0;" aria-hidden="true">
                    <svg width="32" height="32" viewBox="0 0 24 24" fill="none" stroke="var(--ipp-color-gray-400,#9ca3af)" stroke-width="1.5"><path d="M20 21v-2a4 4 0 0 0-4-4H8a4 4 0 0 0-4 4v2"/><circle cx="12" cy="7" r="4"/></svg>
                </div>
                <div style="flex:1;min-width:150px;">
                    <?php if ($prop->agent_name()) : ?>
                        <p class="ipp-agent__name"><?php echo esc_html($prop->agent_name()); ?></p>
                    <?php endif; ?>
                    <?php if ($prop->agent_title()) : ?>
                        <p class="ipp-agent__role"><?php echo esc_html($prop->agent_title()); ?></p>
                    <?php endif; ?>
                </div>
                <div style="display:flex;gap:0.75rem;flex-wrap:wrap;">
                    <?php if ($prop->agent_phone_mobile()) : ?>
                        <a href="tel:<?php echo esc_attr($prop->agent_phone_mobile()); ?>" class="ipp-btn ipp-btn--outline">
                            <?php echo esc_html($prop->agent_phone_mobile()); ?>
                        </a>
                    <?php endif; ?>
                    <?php if ($prop->agent_email()) : ?>
                        <a href="mailto:<?php echo esc_attr($prop->agent_email()); ?>?subject=<?php echo esc_attr('Enquiry about ' . $prop->name()); ?>" class="ipp-btn ipp-btn--primary">
                            Enquire Now
                        </a>
                    <?php endif; ?>
                </div>
            </div>
        </section>

    </div>
</div><!-- .ipp-single -->
