<?php
/**
 * Group Field Template
 * 
 * This template displays the group field values.
 * 
 * HOW TO OVERRIDE THIS TEMPLATE:
 * 1. Copy this file from: wp-content/plugins/havenlytics/templates/single-property/fields/group-field.php
 * 2. Paste it to your theme at: wp-content/themes/your-theme/havenlytics/single-property/fields/group-field.php
 * 3. Modify the copied file to customize the group field display
 * 
 * @package     Havenlytics
 * @subpackage  Templates/single-property/fields/
 * @since       2.0.0
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

// Prefix all variables with hvnly_ to avoid global namespace conflicts
$hvnly_field        = $args['field'] ?? array();
$hvnly_value        = $args['field_value'] ?? $args['value'] ?? '';
$hvnly_label        = $args['field_label'] ?? $hvnly_field['label'] ?? '';
$hvnly_property_id  = $args['property_id'] ?? get_the_ID();
$hvnly_field_name   = $args['field_name'] ?? $hvnly_field['name'] ?? '';
$hvnly_field_type   = $args['field_type'] ?? $hvnly_field['type'] ?? '';

$hvnly_group_id      = $hvnly_field['group_id'] ?? '';
$hvnly_group_type    = $hvnly_field['group_type'] ?? '';
$hvnly_group_name    = $hvnly_field['group_name'] ?? '';
$hvnly_group_position = $hvnly_field['group_position'] ?? 0;
$hvnly_group_total   = $hvnly_field['group_total'] ?? 0;

// For groups, we only render at position 0 to avoid duplicates
if ( $hvnly_group_position > 0 ) {
    return;
}

// If this is a group, find all fields in the same group
if ( ! empty( $hvnly_group_id ) ) {
    $hvnly_all_sections = get_option( 'hvnly_property_builder.sections', array() );
    $hvnly_group_fields = array();
    $hvnly_group_title = '';
    
    foreach ( $hvnly_all_sections as $hvnly_section ) {
        if ( ! empty( $hvnly_section['fields'] ) && is_array( $hvnly_section['fields'] ) ) {
            foreach ( $hvnly_section['fields'] as $hvnly_section_field ) {
                if ( isset( $hvnly_section_field['group_id'] ) && $hvnly_section_field['group_id'] === $hvnly_group_id ) {
                    $hvnly_group_fields[] = $hvnly_section_field;
                    if ( empty( $hvnly_group_title ) && ! empty( $hvnly_section_field['group_name'] ) ) {
                        $hvnly_group_title = $hvnly_section_field['group_name'];
                    }
                }
            }
        }
    }
    
    if ( ! empty( $hvnly_group_fields ) ) {
        // Render based on group type
        switch ( $hvnly_group_type ) {
            case 'video':
                // Use video-card template
                hvnly_get_template( 'single-property/video-card.php', array(
                    'property_id' => $hvnly_property_id,
                    'section' => array(
                        'title' => $hvnly_group_title,
                        'fields' => $hvnly_group_fields,
                    ),
                ) );
                break;
                
            case 'map':
                // Use location-card template
                hvnly_get_template( 'single-property/location-card.php', array(
                    'property_id' => $hvnly_property_id,
                    'section' => array(
                        'title' => $hvnly_group_title,
                        'fields' => $hvnly_group_fields,
                    ),
                ) );
                break;
                
            case 'gallery':
                // Use gallery-card template
                hvnly_get_template( 'single-property/gallery-card.php', array(
                    'property_id' => $hvnly_property_id,
                    'section' => array(
                        'title' => $hvnly_group_title,
                        'fields' => $hvnly_group_fields,
                    ),
                ) );
                break;
                
            default:
                // Render as a standard section
                ?>
                <div class="hvnly-field hvnly-field--group hvnly-field--<?php echo esc_attr( $hvnly_group_type ); ?>">
                    <?php if ( ! empty( $hvnly_group_title ) ) : ?>
                        <h4 class="hvnly-field__group-title"><?php echo esc_html( $hvnly_group_title ); ?></h4>
                    <?php endif; ?>
                    
                    <div class="hvnly-field__group-fields">
                        <?php
                        foreach ( $hvnly_group_fields as $hvnly_group_field ) {
                            $hvnly_field_value = get_post_meta( $hvnly_property_id, $hvnly_group_field['name'], true );
                            if ( ! empty( $hvnly_field_value ) ) {
                                // Render each field using its template
                                hvnly_get_template( 'single-property/fields/' . $hvnly_group_field['type'] . '-field.php', array(
                                    'field' => $hvnly_group_field,
                                    'value' => $hvnly_field_value,
                                    'property_id' => $hvnly_property_id,
                                ), false, true );
                            }
                        }
                        ?>
                    </div>
                </div>
                <?php
                break;
        }
        return;
    }
}

// If not a group or no group fields found, render as regular field
hvnly_get_template( 'single-property/fields/fallback-field.php', $args );