<?php
/**
 * MeetingHub msteams single Template
 *
 * Description: This template displays Microsoft Teams meeting join links for a specific post.
 *
 * @package MeetingHub
 */

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

// Get the current gmdate and time.
$is_schedule       = false;
$next_meeting_time = '';

$start_time       = gmdate( 'Y-m-d\TH:i:s.v\Z', strtotime( $options['startDateTime'] ) + $gmt_offset_val );
$meeting_duration = get_post_meta( get_the_ID(), 'mhub_msteams_meeting_duration', true );
$join_link        = get_post_meta( get_the_ID(), 'mhub_msteams_join_url', true );
$meeting_agenda   = isset( $options['agenda'] ) ? $options['agenda'] : '';
$meeting_id       = get_post_meta( get_the_ID(), 'mhub_msteams_meeting_id', true );

$msteams_api      = \SOVLIX\MHUB\API\MsTeams_Api::get_instance();
$msteams_response = $msteams_api->get_meeting( $meeting_id );

// Fallback to saved details if API fails (e.g. for Personal accounts using Calendar API)
if ( isset( $msteams_response['error'] ) || empty( $msteams_response ) ) {
	$saved_details = get_post_meta( get_the_ID(), 'mhub_msteams_details', true );
	if ( ! empty( $saved_details ) && ! isset( $saved_details['error'] ) ) {
		$msteams_response = $saved_details;
	}
}

$response_message = '';

if ( isset( $msteams_response['error'] ) ) {
	$response_message = isset( $msteams_response['error']['message'] ) ? $msteams_response['error']['message'] : __( 'Error retrieving meeting details.', 'meetinghub' );
}

// Single meeting logic.
if ( ( strtotime( gmdate( 'Y-m-d H:i:s' ) ) + $gmt_offset_val ) - strtotime( $start_time ) <= 0 ) {
	$next_meeting_time = gmdate( 'Y-m-d\TH:i:s.v\Z', strtotime( $start_time ) - $gmt_offset_val );
	$is_schedule       = true;
} else {
	$next_meeting_time = gmdate( 'Y-m-d\TH:i:s.v\Z', strtotime( $start_time ) );
}


if ( $mhub_is_pro_active ) {
	$attendee_login = 'login_successful' === $login_status ? true : false;
}

$enable_user_zone = isset( $options['display_time_user_zone'] ) ? $options['display_time_user_zone'] : '';

?>
<div class="meetinghub-wrapper meetinghub-wrapper-flex" data-mhub-starttime="<?php echo esc_attr( $next_meeting_time ); ?>" data-mhub-timezone="<?php echo esc_attr( $options['meeting_timezone'] ); ?>" data-mhub-title="<?php echo esc_attr( $meeting_title ); ?>" data-mhub-permalilk="<?php echo esc_url( get_permalink( $po_meeting_id ) ); ?>" data-mhub-enable-user-zone="<?php echo esc_attr( $enable_user_zone ); ?>">
	<div class="<?php echo ! $hide_sidebar ? 'mhub-meeting-col' : 'mhub-col-12'; ?> ">
	<?php if ( ! isset( $msteams_response['error'] ) ) { ?>
		<?php if ( 'end' !== $meeting_status ) { ?>
			<?php if ( ( $should_register && $attendee_login ) || current_user_can( 'manage_options' ) || ! $should_register ) { ?>

				<?php
				if ( ! empty( $thumbnail_html ) ) {
					?>
						<div class="meeting-thumbnail">
							<?php echo wp_kses_post( $thumbnail_html ); ?>
						</div>
					<?php
				}
				?>

				<?php
				if ( ! empty( $meeting_description ) ) {
					?>
						<div class="meeting-details">
							<?php echo wp_kses_post( $meeting_description ); ?>
						</div>
					<?php
				}
				?>

				<?php
				// Check if this is a webinar with registration enabled (MsTeams typically assumes Meeting, but structure preserved).
				$meeting_type                 = isset( $options['meeting_type'] ) ? $options['meeting_type'] : 'meeting';
				$is_webinar_with_registration = ( 'webinar' === $meeting_type && ! empty( $options['enable_should_register'] ) );

				// Show registration notice for webinars with registration enabled.
				if ( $is_webinar_with_registration ) {
					?>
					<div class="mhub-webinar-registration-notice">
						<p><?php esc_html_e( 'This webinar requires registration.', 'meetinghub' ); ?></p>
					</div>
					<?php
				}
				?>

			<div class="mhub-webex-join-links">
				<?php 
				$web_link = isset( $msteams_response['webLink'] ) ? $msteams_response['webLink'] : '';
				
				if ( ! empty( $join_link ) ) { 
				?>
				<a class="meetinghub-webex-button mhub-webex-join" target="_blank" href="<?php echo esc_url( $join_link ); ?>" title="<?php echo $is_webinar_with_registration ? esc_attr__( 'Register for Webinar', 'meetinghub' ) : esc_attr__( 'JOIN NOW', 'meetinghub' ); ?>">
					<?php
					if ( $is_webinar_with_registration ) {
						esc_html_e( 'REGISTER NOW', 'meetinghub' );
					} else {
						esc_html_e( 'JOIN NOW', 'meetinghub' );
					}
					?>
				</a>
				<?php } elseif ( ! empty( $web_link ) ) { ?>
					<a class="meetinghub-webex-button mhub-webex-join" target="_blank" href="<?php echo esc_url( $web_link ); ?>" title="<?php esc_attr_e( 'View Calendar Event', 'meetinghub' ); ?>">
						<?php esc_html_e( 'VIEW IN CALENDAR', 'meetinghub' ); ?>
					</a>
					<?php if ( current_user_can( 'manage_options' ) ) { ?>
						<div class="meeting-not-started" style="margin-top: 10px;">
							<?php esc_html_e( 'Admin Note: No video link generated. This happens with Personal (Free) accounts. To generate video links, please use a Microsoft 365 Business or Education account.', 'meetinghub' ); ?>
						</div>
					<?php } ?>
				<?php } else { ?>
					<div class="meeting-not-started">
						<?php 
						if ( current_user_can( 'manage_options' ) ) {
							esc_html_e( 'No join link available. To generate video links, please use a Microsoft 365 Business or Education account.', 'meetinghub' ); 
						} else {
							esc_html_e( 'Meeting link not available yet.', 'meetinghub' );
						}
						?>
					</div>
				<?php } ?>
			</div>

				<?php
			} else {
				// Only show custom registration template for Meeting type.
				$meeting_type = isset( $options['meeting_type'] ) ? $options['meeting_type'] : 'meeting';

				if ( 'meeting' === $meeting_type ) {
					$register_file = MHUB_PRO_INCLUDES . '/Templates/mhub-register.php';
					if ( file_exists( $register_file ) ) {
						require_once $register_file;
					}
				}
			}
			?>
	<?php } else { ?>
				<div class="meeting-not-started">
					<?php esc_html_e( 'The meeting has ended by the host.', 'meetinghub' ); ?>
				</div>
			<?php } ?>
			<?php } else { ?>
				<div class="meeting-not-started">
					<?php echo esc_html( $response_message ); ?>
				</div>

		<?php } ?>
	</div>
	

	<?php
	if ( ! $hide_sidebar ) {
		require_once MHUB_INCLUDES . '/Templates/sidebars/mhub-msteams-sidebar.php';
	}
	?>
	 
</div>
