<?php
/**
 * Single Property Template
 *
 * @package HomeProz
 */

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

get_header();

while (have_posts()) :
    the_post();
    $property_id = get_the_ID();

    // Get property data
    $price = get_field('property_price', $property_id);
    $street_address = get_field('street_address', $property_id);
    $city = get_field('city', $property_id);
    $state = get_field('state', $property_id);
    $zip_code = get_field('zip_code', $property_id);
    $mls_number = get_field('mls_number', $property_id);
    $bedrooms = get_field('bedrooms', $property_id);
    $bathrooms = get_field('bathrooms', $property_id);
    $square_feet = get_field('square_feet', $property_id);
    $lot_size = get_field('lot_size', $property_id);
    $year_built = get_field('year_built', $property_id);
    $garage = get_field('garage', $property_id);
    $short_description = get_field('short_description', $property_id);
    $property_features = get_field('property_features', $property_id);
    $gallery = get_field('property_gallery', $property_id);
    $listing_agent = get_field('listing_agent', $property_id);

    // Get status from taxonomy
    $status_terms = get_the_terms($property_id, 'property_status');
    $status = $status_terms && !is_wp_error($status_terms) ? $status_terms[0]->name : '';
    $status_class = homeproz_get_status_class($status);

    // Get type from taxonomy
    $type_terms = get_the_terms($property_id, 'property_type');
    $type = $type_terms && !is_wp_error($type_terms) ? $type_terms[0]->name : '';

    // Format full address
    $full_address = $street_address;
    if ($city) $full_address .= ', ' . $city;
    if ($state) $full_address .= ', ' . $state;
    if ($zip_code) $full_address .= ' ' . $zip_code;
?>

<main id="primary" class="site-main single-property-main">
    <!-- Breadcrumbs -->
    <nav class="breadcrumbs" aria-label="Breadcrumb">
        <div class="container">
            <ol class="breadcrumb-list">
                <li><a href="<?php echo esc_url(home_url('/')); ?>">Home</a></li>
                <li><a href="<?php echo esc_url(get_post_type_archive_link('property')); ?>">Properties</a></li>
                <li aria-current="page"><?php echo esc_html($street_address ?: get_the_title()); ?></li>
            </ol>
        </div>
    </nav>

    <div class="container">
        <div class="single-property-layout">
            <!-- Main Content -->
            <div class="single-property-content">
                <!-- Gallery -->
                <?php get_template_part('template-parts/property/property-gallery', null, array('gallery' => $gallery, 'property_id' => $property_id)); ?>

                <!-- Property Header -->
                <header class="property-header">
                    <div class="property-header-top">
                        <?php if ($status) : ?>
                            <span class="badge <?php echo esc_attr($status_class); ?>"><?php echo esc_html($status); ?></span>
                        <?php endif; ?>
                        <?php if ($type) : ?>
                            <span class="property-type"><?php echo esc_html($type); ?></span>
                        <?php endif; ?>
                    </div>

                    <h1 class="property-title"><?php echo esc_html(homeproz_format_price($price)); ?></h1>

                    <p class="property-address">
                        <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" 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($full_address); ?>
                    </p>

                    <?php if ($mls_number) : ?>
                        <p class="property-mls">MLS# <?php echo esc_html($mls_number); ?></p>
                    <?php endif; ?>
                </header>

                <!-- Property Specs -->
                <?php if ($bedrooms || $bathrooms || $square_feet || $lot_size || $year_built || $garage) : ?>
                    <section class="property-specs-section">
                        <h2 class="section-title">Property Details</h2>
                        <ul class="property-specs-grid">
                            <?php if ($bedrooms) : ?>
                                <li class="spec-item">
                                    <span class="spec-label">Bedrooms</span>
                                    <span class="spec-value"><?php echo esc_html($bedrooms); ?></span>
                                </li>
                            <?php endif; ?>
                            <?php if ($bathrooms) : ?>
                                <li class="spec-item">
                                    <span class="spec-label">Bathrooms</span>
                                    <span class="spec-value"><?php echo esc_html($bathrooms); ?></span>
                                </li>
                            <?php endif; ?>
                            <?php if ($square_feet) : ?>
                                <li class="spec-item">
                                    <span class="spec-label">Square Feet</span>
                                    <span class="spec-value"><?php echo esc_html(number_format($square_feet)); ?></span>
                                </li>
                            <?php endif; ?>
                            <?php if ($lot_size) : ?>
                                <li class="spec-item">
                                    <span class="spec-label">Lot Size</span>
                                    <span class="spec-value"><?php echo esc_html($lot_size); ?></span>
                                </li>
                            <?php endif; ?>
                            <?php if ($year_built) : ?>
                                <li class="spec-item">
                                    <span class="spec-label">Year Built</span>
                                    <span class="spec-value"><?php echo esc_html($year_built); ?></span>
                                </li>
                            <?php endif; ?>
                            <?php if ($garage) : ?>
                                <li class="spec-item">
                                    <span class="spec-label">Garage</span>
                                    <span class="spec-value"><?php echo esc_html($garage); ?> Car</span>
                                </li>
                            <?php endif; ?>
                        </ul>
                    </section>
                <?php endif; ?>

                <!-- Description -->
                <section class="property-description">
                    <h2 class="section-title">Description</h2>
                    <?php if ($short_description) : ?>
                        <p class="property-short-desc"><?php echo esc_html($short_description); ?></p>
                    <?php endif; ?>
                    <div class="property-full-desc">
                        <?php the_content(); ?>
                    </div>
                </section>

                <!-- Features -->
                <?php if ($property_features && is_array($property_features)) : ?>
                    <section class="property-features">
                        <h2 class="section-title">Features & Amenities</h2>
                        <ul class="features-list">
                            <?php
                            $feature_labels = array(
                                'central_air' => 'Central Air',
                                'central_heat' => 'Central Heat',
                                'fireplace' => 'Fireplace',
                                'hardwood_floors' => 'Hardwood Floors',
                                'updated_kitchen' => 'Updated Kitchen',
                                'updated_bathrooms' => 'Updated Bathrooms',
                                'basement' => 'Basement',
                                'finished_basement' => 'Finished Basement',
                                'deck_patio' => 'Deck/Patio',
                                'pool' => 'Pool',
                                'fenced_yard' => 'Fenced Yard',
                                'sprinkler_system' => 'Sprinkler System',
                                'smart_home' => 'Smart Home Features',
                                'solar_panels' => 'Solar Panels',
                                'new_roof' => 'New Roof',
                                'new_windows' => 'New Windows',
                                'waterfront' => 'Waterfront',
                                'lake_access' => 'Lake Access',
                            );
                            foreach ($property_features as $feature) :
                                $label = isset($feature_labels[$feature]) ? $feature_labels[$feature] : ucwords(str_replace('_', ' ', $feature));
                            ?>
                                <li class="feature-item">
                                    <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="3" aria-hidden="true">
                                        <polyline points="20 6 9 17 4 12"/>
                                    </svg>
                                    <?php echo esc_html($label); ?>
                                </li>
                            <?php endforeach; ?>
                        </ul>
                    </section>
                <?php endif; ?>
            </div>

            <!-- Sidebar -->
            <aside class="single-property-sidebar">
                <?php get_template_part('template-parts/property/property-agent', null, array('agent' => $listing_agent, 'property_id' => $property_id)); ?>
            </aside>
        </div>
    </div>
</main>

<?php
endwhile;
get_footer();
