<?php
/**
 * Output JSON-LD Schema for Single Floor Plans
 */
if ( is_singular( 'rentpress_floorplan' ) && ! empty( $floorplan ) ) :

    $rent_min = !empty($floorplan->floorplan_rent_min) ? (float) $floorplan->floorplan_rent_min : (!empty($floorplan->floorplan_rent_base) ? (float) $floorplan->floorplan_rent_base : null);
    $rent_max = !empty($floorplan->floorplan_rent_max) ? (float) $floorplan->floorplan_rent_max : $rent_min;

    $sqft_min = !empty($floorplan->floorplan_sqft_min) ? (float) $floorplan->floorplan_sqft_min : null;
    $sqft_max = !empty($floorplan->floorplan_sqft_max) ? (float) $floorplan->floorplan_sqft_max : $sqft_min;

    $units_available = isset($floorplan->floorplan_units_available) ? (int) $floorplan->floorplan_units_available : 0;

    if ( $rent_min !== null ) :

        $schema = array(
            '@context' => 'https://schema.org',
            '@type'    => 'Apartment',
            'name'     => get_the_title(),
            'url'      => get_permalink(),
        );

        // Add Floor Size
        if ( $sqft_min ) {
            $schema['floorSize'] = array(
                '@type'    => 'QuantitativeValue',
                'unitCode' => 'FTK', 
                'minValue' => $sqft_min,
                'maxValue' => $sqft_max,
            );
            if ( $sqft_min === $sqft_max ) {
                $schema['floorSize']['value'] = $sqft_min;
                unset($schema['floorSize']['minValue'], $schema['floorSize']['maxValue']);
            }
        }

        // --- THE MAGIC: UNIT-LEVEL OFFERS ---
        if ( !empty($floorplan->units) && is_array($floorplan->units) ) {
            
            // If we have specific units, loop through them and create individual Offers
            $schema['offers'] = array();
            
            foreach ( $floorplan->units as $unit ) {
                // Get the best available price for this specific unit
                $unit_price = !empty($unit->unit_rent_best) ? (float) $unit->unit_rent_best : (!empty($unit->unit_rent_base) ? (float) $unit->unit_rent_base : $rent_min);
                $is_available = !empty($unit->unit_available) ? true : false;

                $schema['offers'][] = array(
                    '@type'         => 'Offer',
                    'name'          => 'Unit ' . $unit->unit_name,
                    'priceCurrency' => 'USD',
                    'price'         => $unit_price,
                    'url'           => !empty($unit->unit_availability_url) ? $unit->unit_availability_url : get_permalink(),
                    'availability'  => $is_available ? 'https://schema.org/InStock' : 'https://schema.org/OutOfStock',
                );
            }

        } else {
            
            // FALLBACK: If there are no specific units, use the Aggregate Min/Max range
            if ( $rent_min !== $rent_max ) {
                $schema['offers'] = array(
                    '@type'         => 'AggregateOffer',
                    'priceCurrency' => 'USD',
                    'lowPrice'      => $rent_min,
                    'highPrice'     => $rent_max,
                    'availability'  => ($units_available > 0) ? 'https://schema.org/InStock' : 'https://schema.org/OutOfStock',
                );
            } else {
                $schema['offers'] = array(
                    '@type'         => 'Offer',
                    'priceCurrency' => 'USD',
                    'price'         => $rent_min,
                    'availability'  => ($units_available > 0) ? 'https://schema.org/InStock' : 'https://schema.org/OutOfStock',
                );
            }
        }

        echo "\n";
        echo '<script type="application/ld+json">' . wp_json_encode($schema, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT) . '</script>' . "\n";

    endif;
endif;
/**
 * Output Supplemental JSON-LD Schema (Pricing) for Single Properties
 */
if ( is_singular( 'rentpress_property' ) && ! empty( $property_data ) ) :

    $rent_min = !empty($property_data->property_rent_min) ? (float) $property_data->property_rent_min : (!empty($property_data->property_rent_base) ? (float) $property_data->property_rent_base : null);
    $rent_max = !empty($property_data->property_rent_max) ? (float) $property_data->property_rent_max : $rent_min;

    if ( $rent_min !== null ) :

        // Create a focused schema block that Google will merge with the main one
        $schema = array(
            '@context' => 'https://schema.org',
            '@type'    => 'ApartmentComplex',
            'url'      => get_permalink(), // This is the anchor Google uses to merge them
        );

        if ( $rent_min !== $rent_max ) {
            $schema['offers'] = array(
                '@type'         => 'AggregateOffer',
                'priceCurrency' => 'USD',
                'lowPrice'      => $rent_min,
                'highPrice'     => $rent_max,
            );
        } else {
            $schema['offers'] = array(
                '@type'         => 'Offer',
                'priceCurrency' => 'USD',
                'price'         => $rent_min,
            );
        }

        echo "\n";
        echo '<script type="application/ld+json">' . wp_json_encode($schema, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT) . '</script>' . "\n";

    endif;
endif;