<?php
/**
 * HVAC Custom Event Submission Form - Minimal Prototype
 * Template override for TEC Community Events edit-event.php
 * 
 * This is a MINIMAL PROTOTYPE to test template override system
 * and add one additional field (excerpt) to validate the approach.
 *
 * Override path: [your-theme]/tribe-events/community/edit-event.php
 * Original: /wp-content/plugins/the-events-calendar-community-events/src/views/community/edit-event.php
 *
 * @version 1.0.0 - HVAC Prototype
 * @since   4.10.17 - TEC Original
 * @since   5.0.0 - TEC Refactored to use generate_form_layout
 * 
 * HVAC CUSTOMIZATIONS:
 * - Added excerpt field after description  
 * - Added version tracking and compatibility comments
 * - Added custom field processing hooks
 * - Maintains all original TEC functionality
 *
 * TEC Version Compatibility: 5.0+ 
 * Last Updated: August 12, 2025
 * Changes: Added excerpt field as prototype
 *
 * @var int|string $tribe_event_id
 */

if ( ! defined( 'ABSPATH' ) ) {
	die( '-1' );
}

if ( ! isset( $tribe_event_id ) ) {
	$tribe_event_id = null;
}

$datepicker_format = Tribe__Date_Utils::get_datepicker_format_index();

/** @var Tribe__Events__Community__Main $main */
$main = tribe( 'community.main' );

// Get event data for editing
$event = null;
$event_excerpt = '';
if ( $tribe_event_id ) {
	$event = get_post( $tribe_event_id );
	if ( $event ) {
		$event_excerpt = $event->post_excerpt;
	}
}

?>

<?php tribe( Tribe__Events__Community__Templates::class )->tribe_get_template_part( 'community/modules/header-links' ); ?>

<?php do_action( 'tribe_events_community_form_before_template', $tribe_event_id ); ?>

<form method="post" enctype="multipart/form-data" data-datepicker_format="<?php echo esc_attr( $datepicker_format ); ?>">
	<input type="hidden" name="post_ID" id="post_ID" value="<?php echo absint( $tribe_event_id ); ?>"/>
	<?php wp_nonce_field( 'ecp_event_submission' ); ?>

	<?php 
	/**
	 * HVAC CUSTOMIZATION: Custom form layout with excerpt field
	 * 
	 * We use a modified approach that adds the excerpt field after description
	 * while maintaining all original TEC functionality through the filter system.
	 */
	
	// Get the standard form layout
	$modules = $main->event_form_layout();
	
	// Insert excerpt field after description
	$excerpt_module = [
		'hvac-excerpt' => [
			'template' => 'community/modules/hvac-excerpt',
			'data' => [
				'event_excerpt' => $event_excerpt,
				'event_id' => $tribe_event_id
			]
		]
	];
	
	// Use TEC's array insert method to add excerpt after description
	if ( method_exists( tribe( 'main' ), 'array_insert_after_key' ) ) {
		$modules = tribe( 'main' )->array_insert_after_key( 'description', $modules, $excerpt_module );
	} else {
		// Fallback: add at the end if method doesn't exist
		$modules = array_merge( $modules, $excerpt_module );
	}
	
	// Apply the standard TEC filter to allow other plugins/code to modify
	$modules = apply_filters( 'tec_events_community_form_layout', $modules );
	
	// Ensure submit button is at the end
	$modules['submit-button'] = [
		'template' => 'community/modules/submit',
	];
	
	// Generate the form using TEC's standard method
	foreach ( $modules as $module_key => $module ) {
		/**
		 * Action hook before loading a module template part.
		 */
		do_action( "tec_events_community_form_before_module_{$module_key}", $tribe_event_id, $module_key, $module );

		// Handle our custom excerpt module
		if ( $module_key === 'hvac-excerpt' ) {
			// Custom excerpt field HTML (inline for prototype)
			?>
			<div class="events-community-post-excerpt hvac-custom-field">
				<?php
				/**
				 * Allow developers to hook and add content to the beginning of this section
				 */
				do_action( 'hvac_events_community_section_before_excerpt', $tribe_event_id );
				?>
				
				<div class="tribe-section-header">
					<h3><?php _e( 'Event Excerpt', 'tribe-events-community' ); ?></h3>
					<p class="tribe-field-description"><?php _e( 'Brief summary of the event for search results and previews (optional).', 'tribe-events-community' ); ?></p>
				</div>
				
				<div class="tribe-section-content">
					<div class="tribe-section-content-field">
						<textarea 
							name="post_excerpt" 
							id="hvac_post_excerpt" 
							rows="3" 
							class="tribe-common-form-control-text__input"
							placeholder="<?php esc_attr_e( 'Enter a brief summary of your event...', 'tribe-events-community' ); ?>"
						><?php echo esc_textarea( $event_excerpt ); ?></textarea>
					</div>
				</div>

				<?php
				/**
				 * Allow developers to hook and add content to the end of this section
				 */
				do_action( 'hvac_events_community_section_after_excerpt', $tribe_event_id );
				?>
			</div>
			
			<!-- HVAC Prototype Success Indicator -->
			<div class="hvac-prototype-indicator" style="background: #e8f5e8; border: 1px solid #4CAF50; padding: 10px; margin: 10px 0; border-radius: 4px;">
				<strong style="color: #2e7d2e;">✓ HVAC Template Override Active</strong> - Excerpt field successfully added via template override system.
			</div>
			<?php
			
		} else {
			// Use standard TEC template loading for all other modules
			tribe( Tribe__Events__Community__Templates::class )->tribe_get_template_part(
				$module['template'],
				null,
				$module['data'] ?? []
			);
		}
		
		/**
		 * Action hook after loading a module template part.
		 */
		do_action( "tec_events_community_form_after_module_{$module_key}", $tribe_event_id, $module_key, $module );
	}
	?>

</form>

<?php 
/**
 * HVAC CUSTOMIZATION: Add custom form processing hooks
 */
add_action( 'tribe_events_community_before_event_save', 'hvac_prototype_process_excerpt_field' );

/**
 * Process the custom excerpt field
 */
function hvac_prototype_process_excerpt_field( $event_id ) {
	// Verify nonce (already done by TEC, but good practice)
	if ( ! wp_verify_nonce( $_POST['_wpnonce'], 'ecp_event_submission' ) ) {
		return;
	}
	
	// Process excerpt field
	if ( isset( $_POST['post_excerpt'] ) ) {
		$excerpt = sanitize_textarea_field( $_POST['post_excerpt'] );
		wp_update_post( array(
			'ID' => $event_id,
			'post_excerpt' => $excerpt
		) );
		
		// Log success for debugging
		if ( function_exists( 'error_log' ) ) {
			error_log( "HVAC Prototype: Excerpt field processed for event ID {$event_id}: " . substr( $excerpt, 0, 50 ) );
		}
	}
}
?>

<?php do_action( 'tribe_events_community_form_after_template', $tribe_event_id ); ?>

<!-- HVAC Template Override Metadata -->
<!-- 
HVAC TEMPLATE OVERRIDE ACTIVE
File: /wp-content/themes/astra-child-hvac/tribe-events/community/edit-event.php
Version: 1.0.0
Customizations: Added excerpt field as prototype
Compatibility: TEC Community Events 5.0+
Created: August 12, 2025
-->