<?php
/**
 * Template for displaying single HVAC Event Summary.
 *
 * This template overrides the default single event template provided by The Events Calendar
 * when viewed by users with appropriate permissions (or potentially all users, depending on requirements).
 * It leverages the Astra theme structure where possible.
 *
 * Design Reference: design_references/upskillhvac.com_hce-event-summary__event_id=1662 (1).png
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

// Ensure the data class is available
if ( ! class_exists( 'HVAC_Event_Summary_Data' ) ) {
    // Attempt to include it if not loaded - adjust path as needed
    $class_path = plugin_dir_path( __FILE__ ) . '../includes/community/class-event-summary-data.php';
    if ( file_exists( $class_path ) ) {
        require_once $class_path;
    } else {
        // Handle error: Class not found, cannot display summary
        echo "<p>Error: Event Summary data handler not found.</p>";
        return;
    }
}


get_header();

?>

<div id="primary" <?php astra_primary_class(); ?>>

	<?php astra_primary_content_top(); ?>

	<?php astra_content_loop(); // This typically includes the have_posts() and the_post() loop ?>

		<?php
		// Ensure we are inside the loop and it's the correct post type
		if ( have_posts() && get_post_type() === Tribe__Events__Main::POSTTYPE ) {
			the_post();

		          $event_id = get_the_ID();
            $summary_data_handler = new HVAC_Event_Summary_Data( $event_id );

            if ( $summary_data_handler->is_valid_event() ) {
                $event_details = $summary_data_handler->get_event_details();
                $venue_details = $summary_data_handler->get_event_venue_details();
                $organizer_details = $summary_data_handler->get_event_organizer_details();
                $transactions = $summary_data_handler->get_event_transactions();
		?>

			<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

				<header class="entry-header <?php astra_entry_header_class(); ?>">
					<!-- Task 5.3: Implement breadcrumb navigation using theme's breadcrumb component -->
					<?php
					// Check if Astra breadcrumb function exists and call it
					if ( function_exists( 'astra_get_breadcrumb' ) ) {
						astra_get_breadcrumb();
					} else {
					                   // Fallback or alternative breadcrumb can be added here if needed
					                   echo '<!-- Breadcrumb not available -->';
					               }
					?>

					<?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>

					               <!-- Add Edit Event Button (Task 5.6) - Conditionally shown for trainer -->
					               <?php
					               // Check if the current user can edit this specific event post
					               // Using 'edit_post' capability for now, might need refinement to a custom cap later
					               if ( current_user_can( 'edit_post', $event_id ) ) {
					                   // Get the URL of the 'manage-event' page
					                   $manage_event_page = get_page_by_path( 'manage-event' );
					                   if ( $manage_event_page ) {
					                       $edit_url = get_permalink( $manage_event_page->ID );
					                       $edit_url = add_query_arg( 'event_id', $event_id, $edit_url );
					                       // Apply Astra button classes
					                       printf(
					                           '<a href="%s" class="button astra-button">%s</a>',
					                           esc_url( $edit_url ),
					                           esc_html__( 'Edit Event', 'hvac-community-events' )
					                       );
					                   }
					               }
					               ?>

                    <!-- View Public Page Button -->
                    <a href="<?php echo esc_url( get_permalink($event_id) ); ?>" class="button astra-button" target="_blank" style="margin-left: 1em;">View Public Event Page</a>

                    <!-- Email Attendees Button (Phase 2) -->
                    <?php
                    // TODO: Add capability check for emailing attendees (e.g., 'email_hvac_attendees')
                    $can_email = current_user_can( 'edit_post', $event_id ); // Placeholder: Use edit cap for now
                    if ( $can_email ) {
                        // TODO: Link to actual Email Attendees page (Phase 2)
                        $email_attendees_url = '#'; // Placeholder URL
                        printf(
                            '<a href="%s" class="button astra-button" style="margin-left: 1em;">%s</a>',
                            esc_url( $email_attendees_url ),
                            esc_html__( 'Email Attendees', 'hvac-community-events' )
                        );
                    }
                    ?>


				</header> <!-- .entry-header -->

				<div class="entry-content clear" <?php astra_schema_e( 'text' ); ?>>

					<?php astra_entry_content_before(); ?>

                    <!-- Task 5.2 & 5.4: Display Event Details in theme-styled card sections / Format content -->
                    <div class="hvac-event-summary-details">
                        <h2>Event Details</h2>
                        <?php if ( $event_details ) { ?>
                            <p><strong>Date:</strong> <?php
                                if ( function_exists( 'tribe_events_event_schedule_details' ) ) {
                                    echo tribe_events_event_schedule_details( $event_id );
                                } else {
                                    // Fallback display if function doesn't exist
                                    echo esc_html( $event_details['start_date'] ?? 'N/A' ) . ' - ' . esc_html( $event_details['end_date'] ?? 'N/A' );
                                }
                            ?></p>
                            <p><strong>Cost:</strong> <?php echo esc_html( $event_details['cost'] ?? 'N/A' ); ?></p>
                            <div class="event-description">
                                <?php echo wp_kses_post( $event_details['description'] ); ?>
                            </div>
                        <?php } ?>

                        <?php if ( $venue_details ) { ?>
                            <h3>Venue</h3>
                            <p><strong>Name:</strong> <?php echo esc_html( $venue_details['name'] ); ?></p>
                            <?php if ( ! empty( $venue_details['address'] ) ) : ?>
                                <p><strong>Address:</strong> <?php echo esc_html( $venue_details['address'] ); ?></p>
                            <?php endif; ?>
                            <?php if ( ! empty( $venue_details['phone'] ) ) : ?>
                                <p><strong>Phone:</strong> <?php echo esc_html( $venue_details['phone'] ); ?></p>
                            <?php endif; ?>
                            <?php if ( ! empty( $venue_details['website'] ) ) : ?>
                                <p><strong>Website:</strong> <?php echo wp_kses_post( $venue_details['website'] ); // Allow link HTML ?></p>
                            <?php endif; ?>
                            <?php // TODO: Add Map Link / Directions Link if needed ?>
                        <?php } ?>

                        <?php if ( $organizer_details ) { ?>
                            <h3>Organizer</h3>
                            <p><strong>Name:</strong> <?php echo esc_html( $organizer_details['name'] ); ?></p>
                             <?php if ( ! empty( $organizer_details['phone'] ) ) : ?>
                                <p><strong>Phone:</strong> <?php echo esc_html( $organizer_details['phone'] ); ?></p>
                            <?php endif; ?>
                            <?php if ( ! empty( $organizer_details['email'] ) ) : ?>
                                <p><strong>Email:</strong> <a href="mailto:<?php echo esc_attr( $organizer_details['email'] ); ?>"><?php echo esc_html( $organizer_details['email'] ); ?></a></p>
                            <?php endif; ?>
                            <?php if ( ! empty( $organizer_details['website'] ) ) : ?>
                                <p><strong>Website:</strong> <?php echo wp_kses_post( $organizer_details['website'] ); // Allow link HTML ?></p>
                            <?php endif; ?>
                        <?php } ?>
                    </div>

                    <!-- Task 5.5: Implement Transactions Table using theme's table styling -->
                    <div class="hvac-event-summary-transactions">
                        <h2>Transactions / Attendees</h2>
                        <?php if ( ! empty( $transactions ) ) { ?>
                            <table class="hvac-transactions-table astra-table-cls"> <!-- Add theme table class -->
                                <thead>
                                    <tr>
                                        <th>Attendee Name</th>
                                        <th>Email</th>
                                        <th>Ticket Type</th>
                                        <th>Order ID</th>
                                        <th>Checked In</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php foreach ( $transactions as $txn ) { ?>
                                        <tr>
                                            <td><?php echo esc_html( $txn['purchaser_name'] ?? 'N/A' ); ?></td>
                                            <td><?php echo esc_html( $txn['purchaser_email'] ?? 'N/A' ); ?></td>
                                            <td><?php echo esc_html( $txn['ticket_type_name'] ?? 'N/A' ); ?></td>
                                            <td><?php echo esc_html( $txn['order_id'] ?? 'N/A' ); ?></td>
                                            <td><?php echo $txn['checked_in'] ? 'Yes' : 'No'; ?></td>
                                        </tr>
                                    <?php } ?>
                                </tbody>
                            </table>
                        <?php } else { ?>
                            <p>No transactions found for this event.</p>
                        <?php } ?>
                    </div>

					<?php wp_link_pages( /* ... */ ); ?>

					<?php astra_entry_content_after(); ?>

				</div><!-- .entry-content -->

			</article><!-- #post-<?php the_ID(); ?> -->

		<?php
            } else {
                // Handle case where event data couldn't be loaded
                echo '<p>Could not load event summary data.</p>';
            }

		} else {
            // Handle case where it's not a tribe_events post or no posts found
             astra_content_page_loop(); // Fallback to default page loop? Or show error.
             echo '<p>Event not found or invalid post type.</p>';
		}
		?>

	<?php astra_primary_content_bottom(); ?>

</div><!-- #primary -->

<?php get_sidebar(); ?>

<?php get_footer(); ?>