<?php if (!defined('ABSPATH')) exit; ?>
<?php
/**
 * Developer - Single Property Template
 *
 * Semantic HTML5, all data present, proper headings, no decorative styling.
 * Clean structure for theme developers to override completely.
 * All data attributes for JS hooks.
 *
 * @package Integrate_Palace_Properties
 */

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

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

<article class="ipp-single ipp-single--developer"
         data-property-code="<?php echo esc_attr($prop->property_code()); ?>"
         data-property-status="<?php echo esc_attr($prop->status()); ?>"
         data-bedrooms="<?php echo esc_attr($prop->bedrooms()); ?>"
         data-bathrooms="<?php echo esc_attr($prop->bathrooms()); ?>"
         data-ensuites="<?php echo esc_attr($prop->ensuites()); ?>"
         data-cars="<?php echo esc_attr($prop->cars()); ?>"
         data-rent="<?php echo esc_attr($prop->rent()); ?>"
         data-rental-period="<?php echo esc_attr($prop->rental_period()); ?>"
         data-suburb="<?php echo esc_attr($prop->suburb()); ?>"
         data-region="<?php echo esc_attr($prop->region()); ?>"
         data-postcode="<?php echo esc_attr($prop->postcode()); ?>"
         data-property-class="<?php echo esc_attr($prop->property_class()); ?>"
         data-date-available="<?php echo esc_attr($prop->date_available()); ?>"
         data-pets-allowed="<?php echo esc_attr($prop->pets_allowed()); ?>"
         data-smokers-allowed="<?php echo esc_attr($prop->smokers_allowed()); ?>"
         aria-label="<?php echo esc_attr($prop->name()); ?>">

    <!-- Gallery -->
    <?php if (!empty($images)) : ?>
    <section data-role="gallery" aria-label="Property gallery">
        <?php foreach ($images as $i => $img) : ?>
            <figure data-image-index="<?php echo esc_attr($i); ?>" data-image-id="<?php echo esc_attr($img['id']); ?>">
                <img src="<?php echo esc_url($img['url']); ?>"
                     alt="<?php echo esc_attr($img['alt'] ?: $prop->name() . ' - Photo ' . ($i + 1)); ?>"
                     data-thumb="<?php echo esc_url($img['thumb']); ?>"
                     data-full="<?php echo esc_url($img['full']); ?>"
                     loading="<?php echo $i < 3 ? 'eager' : 'lazy'; ?>">
            </figure>
        <?php endforeach; ?>
    </section>
    <?php endif; ?>

    <!-- Header -->
    <header data-role="property-header">
        <?php if ($prop->show('status') && $prop->status()) : ?>
            <span data-role="property-status"><?php echo esc_html($prop->status()); ?></span>
        <?php endif; ?>

        <h1 data-role="property-title"><?php echo esc_html(get_the_title()); ?></h1>

        <?php if ($prop->show('address') && $prop->full_address()) : ?>
            <address data-role="property-address">
                <span data-field="full-address"><?php echo esc_html($prop->full_address()); ?></span>
                <?php if ($prop->suburb()) : ?>
                    <span data-field="suburb"><?php echo esc_html($prop->suburb()); ?></span>
                <?php endif; ?>
                <?php if ($prop->region()) : ?>
                    <span data-field="region"><?php echo esc_html($prop->region()); ?></span>
                <?php endif; ?>
                <?php if ($prop->postcode()) : ?>
                    <span data-field="postcode"><?php echo esc_html($prop->postcode()); ?></span>
                <?php endif; ?>
            </address>
        <?php endif; ?>

        <?php if ($prop->show('rent') && $prop->formatted_rent()) : ?>
            <div data-role="property-price" data-raw-rent="<?php echo esc_attr($prop->rent()); ?>">
                <span data-field="formatted-rent"><?php echo esc_html($prop->formatted_rent()); ?></span>
                <?php if ($prop->rental_period()) : ?>
                    <span data-field="rental-period"><?php echo esc_html($prop->rental_period()); ?></span>
                <?php endif; ?>
            </div>
        <?php endif; ?>
    </header>

    <!-- Specifications -->
    <section data-role="property-specs" aria-labelledby="dev-specs-heading">
        <h2 id="dev-specs-heading">Specifications</h2>
        <dl>
            <?php if ($prop->show('bedrooms') && $prop->bedrooms()) : ?>
                <div data-spec="bedrooms">
                    <dt>Bedrooms</dt>
                    <dd><?php echo esc_html($prop->bedrooms()); ?></dd>
                </div>
            <?php endif; ?>
            <?php if ($prop->show('bathrooms') && $prop->bathrooms()) : ?>
                <div data-spec="bathrooms">
                    <dt>Bathrooms</dt>
                    <dd><?php echo esc_html($prop->bathrooms()); ?></dd>
                </div>
            <?php endif; ?>
            <?php if ($prop->show('ensuites') && $prop->ensuites()) : ?>
                <div data-spec="ensuites">
                    <dt>Ensuites</dt>
                    <dd><?php echo esc_html($prop->ensuites()); ?></dd>
                </div>
            <?php endif; ?>
            <?php if ($prop->show('cars') && $prop->cars()) : ?>
                <div data-spec="cars">
                    <dt>Car Spaces</dt>
                    <dd><?php echo esc_html($prop->cars()); ?></dd>
                </div>
            <?php endif; ?>
            <?php if ($prop->show('parking') && $prop->parking()) : ?>
                <div data-spec="parking">
                    <dt>Parking</dt>
                    <dd><?php echo esc_html($prop->parking()); ?></dd>
                </div>
            <?php endif; ?>
        </dl>
    </section>

    <!-- Details -->
    <section data-role="property-details" aria-labelledby="dev-details-heading">
        <h2 id="dev-details-heading">Details</h2>
        <dl>
            <?php if ($prop->show('property_code') && $prop->property_code()) : ?>
                <div data-detail="property-code">
                    <dt>Property Code</dt>
                    <dd><?php echo esc_html($prop->property_code()); ?></dd>
                </div>
            <?php endif; ?>
            <?php if ($prop->show('property_class') && $prop->property_class()) : ?>
                <div data-detail="property-class">
                    <dt>Property Type</dt>
                    <dd><?php echo esc_html($prop->property_class()); ?></dd>
                </div>
            <?php endif; ?>
            <?php if ($prop->show('date_available') && $prop->formatted_date_available()) : ?>
                <div data-detail="date-available">
                    <dt>Available From</dt>
                    <dd>
                        <time datetime="<?php echo esc_attr($prop->date_available()); ?>">
                            <?php echo esc_html($prop->formatted_date_available()); ?>
                        </time>
                    </dd>
                </div>
            <?php endif; ?>
            <?php if ($prop->show('year_built') && $prop->year_built()) : ?>
                <div data-detail="year-built">
                    <dt>Year Built</dt>
                    <dd><?php echo esc_html($prop->year_built()); ?></dd>
                </div>
            <?php endif; ?>
            <?php if ($prop->show('stories') && $prop->stories()) : ?>
                <div data-detail="stories">
                    <dt>Stories</dt>
                    <dd><?php echo esc_html($prop->stories()); ?></dd>
                </div>
            <?php endif; ?>
            <?php if ($prop->show('furnishings') && $prop->furnishings()) : ?>
                <div data-detail="furnishings">
                    <dt>Furnishings</dt>
                    <dd><?php echo esc_html($prop->furnishings()); ?></dd>
                </div>
            <?php endif; ?>
            <?php if ($prop->show('floor_area') && $prop->floor_area()) : ?>
                <div data-detail="floor-area">
                    <dt>Floor Area</dt>
                    <dd><?php echo esc_html($prop->floor_area()); ?></dd>
                </div>
            <?php endif; ?>
            <?php if ($prop->show('land_area') && $prop->land_area_sqm()) : ?>
                <div data-detail="land-area">
                    <dt>Land Area</dt>
                    <dd><?php echo esc_html($prop->land_area_sqm()); ?> sqm</dd>
                </div>
            <?php endif; ?>
            <?php if ($prop->show('pets_allowed') && $prop->pets_allowed()) : ?>
                <div data-detail="pets-allowed">
                    <dt>Pets Allowed</dt>
                    <dd><?php echo esc_html($prop->pets_allowed()); ?></dd>
                </div>
            <?php endif; ?>
            <?php if ($prop->show('smokers_allowed') && $prop->smokers_allowed()) : ?>
                <div data-detail="smokers-allowed">
                    <dt>Smokers Allowed</dt>
                    <dd><?php echo esc_html($prop->smokers_allowed()); ?></dd>
                </div>
            <?php endif; ?>
        </dl>
    </section>

    <!-- Features -->
    <?php if ($prop->show('feature_details') && $prop->feature_details()) : ?>
    <section data-role="property-features" aria-labelledby="dev-features-heading">
        <h2 id="dev-features-heading">Features</h2>
        <ul>
            <?php foreach ((array) $prop->feature_details() as $feature) : ?>
                <li><?php echo esc_html($feature); ?></li>
            <?php endforeach; ?>
        </ul>
    </section>
    <?php endif; ?>

    <!-- Description -->
    <?php if ($prop->show('advert_text') && $prop->advert_text()) : ?>
    <section data-role="property-description" aria-labelledby="dev-desc-heading">
        <h2 id="dev-desc-heading">Description</h2>
        <div data-field="advert-text">
            <?php echo wp_kses_post($prop->advert_text()); ?>
        </div>
    </section>
    <?php endif; ?>

    <!-- Custom Fields -->
    <?php if ($prop->custom_fields()) : ?>
    <section data-role="property-custom-fields" aria-labelledby="dev-custom-heading">
        <h2 id="dev-custom-heading">Additional Information</h2>
        <dl>
            <?php foreach ((array) $prop->custom_fields() as $key => $value) : ?>
                <div data-custom-field="<?php echo esc_attr($key); ?>">
                    <dt><?php echo esc_html($key); ?></dt>
                    <dd><?php echo esc_html($value); ?></dd>
                </div>
            <?php endforeach; ?>
        </dl>
    </section>
    <?php endif; ?>

    <!-- Links -->
    <section data-role="property-links">
        <?php if ($prop->show('virtual_tour_url') && $prop->virtual_tour_url()) : ?>
            <a href="<?php echo esc_url($prop->virtual_tour_url()); ?>" target="_blank" rel="noopener noreferrer" data-role="virtual-tour-link">
                Virtual Tour
            </a>
        <?php endif; ?>
        <?php if ($prop->show('web_link_url') && $prop->web_link_url()) : ?>
            <a href="<?php echo esc_url($prop->web_link_url()); ?>" target="_blank" rel="noopener noreferrer" data-role="web-link">
                View on Website
            </a>
        <?php endif; ?>
    </section>

    <!-- Map -->
    <?php if ($prop->show('geographic_location') && $prop->has_map_coords()) : ?>
    <section data-role="property-map" aria-labelledby="dev-map-heading">
        <h2 id="dev-map-heading">Location</h2>
        <?php $geo = $prop->geographic_location(); ?>
        <div data-role="map-container"
             data-map-embed-url="<?php echo esc_url($prop->map_embed_url()); ?>"
             data-lat="<?php echo isset($geo['lat']) ? esc_attr($geo['lat']) : ''; ?>"
             data-lng="<?php echo isset($geo['lng']) ? esc_attr($geo['lng']) : ''; ?>">
            <iframe src="<?php echo esc_url($prop->map_embed_url()); ?>"
                    width="100%" height="400"
                    style="border:0;"
                    allowfullscreen loading="lazy"
                    referrerpolicy="no-referrer-when-downgrade"
                    title="Property location map"></iframe>
        </div>
    </section>
    <?php endif; ?>

    <!-- Agent -->
    <aside data-role="property-agent" aria-label="Agent information">
        <h2>Agent</h2>
        <div data-agent-name="<?php echo esc_attr($prop->agent_name()); ?>"
             data-agent-email="<?php echo esc_attr($prop->agent_email()); ?>"
             data-agent-phone-mobile="<?php echo esc_attr($prop->agent_phone_mobile()); ?>"
             data-agent-phone-work="<?php echo esc_attr($prop->agent_phone_work()); ?>">

            <?php if ($prop->agent_name()) : ?>
                <p data-field="agent-name"><?php echo esc_html($prop->agent_name()); ?></p>
            <?php endif; ?>

            <?php if ($prop->agent_title()) : ?>
                <p data-field="agent-title"><?php echo esc_html($prop->agent_title()); ?></p>
            <?php endif; ?>

            <ul data-role="agent-contacts">
                <?php if ($prop->agent_phone_mobile()) : ?>
                    <li data-contact="mobile">
                        <a href="tel:<?php echo esc_attr($prop->agent_phone_mobile()); ?>">
                            <?php echo esc_html($prop->agent_phone_mobile()); ?>
                        </a>
                    </li>
                <?php endif; ?>
                <?php if ($prop->agent_phone_work()) : ?>
                    <li data-contact="work">
                        <a href="tel:<?php echo esc_attr($prop->agent_phone_work()); ?>">
                            <?php echo esc_html($prop->agent_phone_work()); ?>
                        </a>
                    </li>
                <?php endif; ?>
                <?php if ($prop->agent_email()) : ?>
                    <li data-contact="email">
                        <a href="mailto:<?php echo esc_attr($prop->agent_email()); ?>">
                            <?php echo esc_html($prop->agent_email()); ?>
                        </a>
                    </li>
                <?php endif; ?>
            </ul>

            <?php if ($prop->agent_email()) : ?>
                <a href="mailto:<?php echo esc_attr($prop->agent_email()); ?>?subject=<?php echo esc_attr('Enquiry about ' . $prop->name()); ?>"
                   data-role="enquire-link">
                    Enquire Now
                </a>
            <?php endif; ?>
        </div>
    </aside>

</article>
