<?php
/**
 * Single Event Template
 *
 * @package NemtlyBooking
 */

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

// Check if header.php exists before including it
if ( file_exists( get_template_directory() . '/header.php' ) ) {
    get_header();
} else {
    // Fallback header
    ?>
    <!DOCTYPE html>
    <html <?php language_attributes(); ?>>
    <head>
        <meta charset="<?php bloginfo( 'charset' ); ?>">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title><?php wp_title( '|', true, 'right' ); ?></title>
        <?php wp_head(); ?>
    </head>
    <body <?php body_class(); ?>>
    <?php wp_body_open(); ?>
    <div id="page" class="site">
        <header id="masthead" class="site-header">
            <div class="site-branding">
                <h1 class="site-title"><a href="<?php echo esc_url( home_url( '/' ) ); ?>"><?php bloginfo( 'name' ); ?></a></h1>
            </div>
        </header>
        <div id="content" class="site-content">
    <?php
}
?>

<div class="nb-container nb-mx-auto nb-px-4 nb-py-8">
    <?php
    while ( have_posts() ) :
		the_post();
		?>
        <?php
        // Get event data - prefix all variables
        $nemtly_booking_event_id       = get_the_ID();
        $nemtly_booking_event_date     = get_post_meta( $nemtly_booking_event_id, 'nemtly_event_date', true );
        $nemtly_booking_event_time     = get_post_meta( $nemtly_booking_event_id, 'nemtly_event_time', true );
        $nemtly_booking_event_timezone = get_post_meta( $nemtly_booking_event_id, 'nemtly_event_timezone', true );
        $nemtly_booking_event_duration = get_post_meta( $nemtly_booking_event_id, 'nemtly_event_duration', true );
        $nemtly_booking_location       = get_post_meta( $nemtly_booking_event_id, 'nemtly_location', true );
        $nemtly_booking_max_attendees  = get_post_meta( $nemtly_booking_event_id, 'nemtly_max_attendees', true );
        $nemtly_booking_booking_cutoff = get_post_meta( $nemtly_booking_event_id, 'nemtly_booking_cutoff', true );
        $nemtly_booking_allow_waitlist = get_post_meta( $nemtly_booking_event_id, 'nemtly_allow_waitlist', true );

        // Use event's timezone for display, fall back to site timezone
        try {
            $nemtly_booking_tz = new \DateTimeZone( $nemtly_booking_event_timezone ? $nemtly_booking_event_timezone : wp_timezone_string() );
        } catch ( \Exception $e ) {
            $nemtly_booking_tz = wp_timezone();
        }

        // Format date and time using wp_date() with event timezone
        $nemtly_booking_formatted_date = '';
        $nemtly_booking_formatted_time = '';
        if ( $nemtly_booking_event_date ) {
            $nemtly_booking_date_obj       = new \DateTime( $nemtly_booking_event_date . ' ' . ( $nemtly_booking_event_time ? $nemtly_booking_event_time : '00:00' ), $nemtly_booking_tz );
            $nemtly_booking_formatted_date = wp_date( get_option( 'date_format' ), $nemtly_booking_date_obj->getTimestamp(), $nemtly_booking_tz );
            if ( $nemtly_booking_event_time ) {
                $nemtly_booking_formatted_time = wp_date( get_option( 'time_format' ), $nemtly_booking_date_obj->getTimestamp(), $nemtly_booking_tz );
            }
        }

        // Calculate end time
        $nemtly_booking_end_time = '';
        if ( $nemtly_booking_event_time && $nemtly_booking_event_duration ) {
            $nemtly_booking_end_obj = clone $nemtly_booking_date_obj;
            $nemtly_booking_end_obj->modify( '+' . intval( $nemtly_booking_event_duration ) . ' minutes' );
            $nemtly_booking_end_time = wp_date( get_option( 'time_format' ), $nemtly_booking_end_obj->getTimestamp(), $nemtly_booking_tz );
        }
        ?>

        <!-- Schema.org Event Markup -->
        <script type="application/ld+json">
        {
            "@context": "https://schema.org",
            "@type": "Event",
            "name": "<?php echo esc_js( get_the_title() ); ?>",
            "description": "<?php echo esc_js( wp_strip_all_tags( get_the_excerpt() ) ); ?>",
            "startDate": "<?php echo esc_js( isset( $nemtly_booking_date_obj ) ? $nemtly_booking_date_obj->format( 'c' ) : '' ); ?>",
            "endDate": "<?php echo esc_js( isset( $nemtly_booking_end_obj ) ? $nemtly_booking_end_obj->format( 'c' ) : '' ); ?>",
            "location": {
                "@type": "Place",
                "name": "<?php echo esc_js( $nemtly_booking_location ); ?>"
            },
            "organizer": {
                "@type": "Organization",
                "name": "<?php echo esc_js( get_bloginfo( 'name' ) ); ?>"
            },
            "url": "<?php echo esc_js( get_permalink() ); ?>"
        }
        </script>

        <article id="event-<?php echo esc_attr( $nemtly_booking_event_id ); ?>" class="nb-event-single">
            <!-- Event Header -->
            <header class="nb-event-header nb-mb-8">
                <div class="nb-max-w-4xl nb-mx-auto">
                    <h1 class="nb-text-4xl nb-font-bold nb-text-gray-900 nb-mb-4">
                        <?php the_title(); ?>
                    </h1>

                    <!-- Event Meta -->
                    <div class="nb-flex nb-flex-wrap nb-gap-6 nb-text-gray-600 nb-mb-6">
                        <?php if ( $nemtly_booking_formatted_date ) : ?>
                            <div class="nb-flex nb-items-center nb-gap-2">
                                <svg class="nb-w-5 nb-h-5" fill="currentColor" viewBox="0 0 20 20">
                                    <path fill-rule="evenodd" d="M6 2a1 1 0 00-1 1v1H4a2 2 0 00-2 2v10a2 2 0 002 2h12a2 2 0 002-2V6a2 2 0 00-2-2h-1V3a1 1 0 10-2 0v1H7V3a1 1 0 00-1-1zm0 5a1 1 0 000 2h8a1 1 0 100-2H6z" clip-rule="evenodd" />
                                </svg>
                                <span><?php echo esc_html( $nemtly_booking_formatted_date ); ?></span>
                            </div>
                        <?php endif; ?>

                        <?php if ( $nemtly_booking_formatted_time ) : ?>
                            <div class="nb-flex nb-items-center nb-gap-2">
                                <svg class="nb-w-5 nb-h-5" fill="currentColor" viewBox="0 0 20 20">
                                    <path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zm1-12a1 1 0 10-2 0v4a1 1 0 00.293.707l2.828 2.829a1 1 0 101.415-1.415L11 9.586V6z" clip-rule="evenodd" />
                                </svg>
                                <span><?php echo esc_html( $nemtly_booking_formatted_time ); ?>
                                <?php if ( $nemtly_booking_end_time ) : ?>
                                    - <?php echo esc_html( $nemtly_booking_end_time ); ?>
                                <?php endif; ?>
                                </span>
                            </div>
                        <?php endif; ?>

                        <?php if ( $nemtly_booking_location ) : ?>
                            <div class="nb-flex nb-items-center nb-gap-2">
                                <svg class="nb-w-5 nb-h-5" fill="currentColor" viewBox="0 0 20 20">
                                    <path fill-rule="evenodd" d="M5.05 4.05a7 7 0 119.9 9.9L10 18.9l-4.95-4.95a7 7 0 010-9.9zM10 11a2 2 0 100-4 2 2 0 000 4z" clip-rule="evenodd" />
                                </svg>
                                <span><?php echo esc_html( $nemtly_booking_location ); ?></span>
                            </div>
                        <?php endif; ?>
                    </div>
                </div>
            </header>

            <div class="nb-max-w-4xl nb-mx-auto">
                <div class="nb-grid nb-grid-cols-1 lg:nb-grid-cols-3 nb-gap-8">
                    <!-- Event Content -->
                    <div class="lg:nb-col-span-2">
                        <!-- Featured Image -->
                        <?php if ( has_post_thumbnail() ) : ?>
                            <div class="nb-mb-6">
                                <?php the_post_thumbnail( 'large', [ 'class' => 'nb-w-full nb-h-auto nb-rounded-lg' ] ); ?>
                            </div>
                        <?php endif; ?>

                        <!-- Event Description -->
                        <div class="nb-prose nb-max-w-none nb-mb-8">
                            <?php the_content(); ?>
                        </div>

                        <!-- Event Details -->
                        <div class="nb-bg-gray-50 nb-rounded-lg nb-p-6 nb-mb-8">
                            <h3 class="nb-text-lg nb-font-semibold nb-text-gray-900 nb-mb-4">
                                <?php esc_html_e( 'Event Details', 'nemtly-booking' ); ?>
                            </h3>

                            <dl class="nb-grid nb-grid-cols-1 sm:nb-grid-cols-2 nb-gap-4">
                                <?php if ( $nemtly_booking_formatted_date ) : ?>
                                    <div>
                                        <dt class="nb-font-medium nb-text-gray-700"><?php esc_html_e( 'Date', 'nemtly-booking' ); ?></dt>
                                        <dd class="nb-text-gray-900"><?php echo esc_html( $nemtly_booking_formatted_date ); ?></dd>
                                    </div>
                                <?php endif; ?>

                                <?php if ( $nemtly_booking_formatted_time ) : ?>
                                    <div>
                                        <dt class="nb-font-medium nb-text-gray-700"><?php esc_html_e( 'Time', 'nemtly-booking' ); ?></dt>
                                        <dd class="nb-text-gray-900">
                                            <?php echo esc_html( $nemtly_booking_formatted_time ); ?>
                                            <?php if ( $nemtly_booking_end_time ) : ?>
                                                - <?php echo esc_html( $nemtly_booking_end_time ); ?>
                                            <?php endif; ?>
                                        </dd>
                                    </div>
                                <?php endif; ?>

                                <?php if ( $nemtly_booking_location ) : ?>
                                    <div>
                                        <dt class="nb-font-medium nb-text-gray-700"><?php esc_html_e( 'Location', 'nemtly-booking' ); ?></dt>
                                        <dd class="nb-text-gray-900"><?php echo esc_html( $nemtly_booking_location ); ?></dd>
                                    </div>
                                <?php endif; ?>

                                <?php if ( $nemtly_booking_event_duration ) : ?>
                                    <div>
                                        <dt class="nb-font-medium nb-text-gray-700"><?php esc_html_e( 'Duration', 'nemtly-booking' ); ?></dt>
                                        <dd class="nb-text-gray-900">
                                            <?php
                                            $nemtly_booking_hours   = floor( $nemtly_booking_event_duration / 60 );
                                            $nemtly_booking_minutes = $nemtly_booking_event_duration % 60;
                                            if ( $nemtly_booking_hours > 0 ) {
                                                echo esc_html(
                                                    sprintf(
                                                        // translators: %d is the number of hours.
                                                        _n( '%d hour', '%d hours', $nemtly_booking_hours, 'nemtly-booking' ),
                                                        $nemtly_booking_hours
                                                    )
                                                );
                                                if ( $nemtly_booking_minutes > 0 ) {
                                                    echo ' ' . esc_html(
                                                        sprintf(
                                                            // translators: %d is the number of minutes.
                                                            _n( '%d minute', '%d minutes', $nemtly_booking_minutes, 'nemtly-booking' ),
                                                            $nemtly_booking_minutes
                                                        )
                                                    );
                                                }
                                            } else {
                                                echo esc_html(
                                                    sprintf(
                                                        // translators: %d is the number of minutes.
                                                        _n( '%d minute', '%d minutes', $nemtly_booking_minutes, 'nemtly-booking' ),
                                                        $nemtly_booking_minutes
                                                    )
                                                );
                                            }
                                            ?>
                                        </dd>
                                    </div>
                                <?php endif; ?>

                                <?php if ( $nemtly_booking_max_attendees && $nemtly_booking_max_attendees > 0 ) : ?>
                                    <div>
                                        <dt class="nb-font-medium nb-text-gray-700"><?php esc_html_e( 'Capacity', 'nemtly-booking' ); ?></dt>
                                        <dd class="nb-text-gray-900">
                                            <?php
                                            echo esc_html(
                                                sprintf(
                                                    // translators: %d is the number of attendees.
                                                    _n( '%d attendee', '%d attendees', $nemtly_booking_max_attendees, 'nemtly-booking' ),
                                                    $nemtly_booking_max_attendees
                                                )
                                            );
                                            ?>
                                        </dd>
                                    </div>
                                <?php endif; ?>
                            </dl>
                        </div>
                    </div>

                    <!-- Sidebar content removed to restrict form to shortcode usage -->
                </div>
            </div>
        </article>
    <?php endwhile; ?>
</div>

<?php
// Add Open Graph meta tags
add_action(
    'wp_head',
    function () {
		global $post;
		if ( ! $post || 'nemtly_event' !== $post->post_type ) {
			return;
		}

		$nemtly_booking_event_date     = get_post_meta( $post->ID, 'nemtly_event_date', true );
		$nemtly_booking_event_time     = get_post_meta( $post->ID, 'nemtly_event_time', true );
		$nemtly_booking_event_timezone = get_post_meta( $post->ID, 'nemtly_event_timezone', true );
		$nemtly_booking_location       = get_post_meta( $post->ID, 'nemtly_location', true );

		// Build ISO 8601 datetime with timezone offset for OG tags
		$nemtly_booking_og_start = '';
		if ( $nemtly_booking_event_date && $nemtly_booking_event_time ) {
			try {
				$nemtly_booking_og_tz    = new \DateTimeZone( $nemtly_booking_event_timezone ? $nemtly_booking_event_timezone : wp_timezone_string() );
				$nemtly_booking_og_dt    = new \DateTime( $nemtly_booking_event_date . ' ' . $nemtly_booking_event_time, $nemtly_booking_og_tz );
				$nemtly_booking_og_start = $nemtly_booking_og_dt->format( 'c' );
			} catch ( \Exception $e ) {
				$nemtly_booking_og_start = $nemtly_booking_event_date . 'T' . $nemtly_booking_event_time . ':00';
			}
		}

		?>
    <!-- Open Graph Meta Tags -->
    <meta property="og:title" content="<?php echo esc_attr( get_the_title() ); ?>" />
    <meta property="og:description" content="<?php echo esc_attr( wp_strip_all_tags( get_the_excerpt() ) ); ?>" />
    <meta property="og:type" content="event" />
    <meta property="og:url" content="<?php echo esc_url( get_permalink() ); ?>" />
		<?php if ( has_post_thumbnail() ) : ?>
        <meta property="og:image" content="<?php echo esc_url( get_the_post_thumbnail_url( $post->ID, 'large' ) ); ?>" />
    <?php endif; ?>

    <!-- Event-specific Open Graph tags -->
		<?php if ( $nemtly_booking_og_start ) : ?>
        <meta property="event:start_time" content="<?php echo esc_attr( $nemtly_booking_og_start ); ?>" />
    <?php endif; ?>
		<?php if ( $nemtly_booking_location ) : ?>
        <meta property="event:location" content="<?php echo esc_attr( $nemtly_booking_location ); ?>" />
    <?php endif; ?>
		<?php
	},
    10
);

// Check if footer.php exists before including it
if ( file_exists( get_template_directory() . '/footer.php' ) ) {
    get_footer();
} else {
    // Fallback footer
    ?>
    </div>
    </div>
    <?php wp_footer(); ?>
    </body>
    </html>
    <?php
}
?>
