<?php
namespace HydraBooking\Hooks;

// Use
use HydraBooking\DB\Meeting;
use HydraBooking\DB\Attendees;
use HydraBooking\DB\Host;
use HydraBooking\DB\BookingMeta;
use HydraBooking\Admin\Controller\DateTimeController;
use HydraBooking\Admin\Controller\Helper;


class MailHooks {
	// Approved
	// Pending
	// Re-schedule
	// Canceled
 
	public function __construct() {
		add_action( 'hydra_booking/after_booking_confirmed', array( $this, 'pushBookingToConfirmed' ), 20, 1 ); 
		add_action( 'hydra_booking/after_booking_pending', array( $this, 'pushBookingToPending' ), 20, 1 );
		add_action( 'hydra_booking/after_booking_canceled', array( $this, 'pushBookingToCanceled' ), 20, 1 );
		add_action( 'hydra_booking/after_booking_schedule', array( $this, 'pushBookingToscheduled' ), 20, 2 );
		add_action( 'hydra_booking/send_booking_reminder', array( $this, 'send_booking_reminder' ), 20, 1 );

		// Send Mail Booking with All attendees
		add_action( 'hydra_booking/send_booking_with_all_attendees_confirmed', array( $this, 'send_booking_with_all_attendees_confirmed' ), 10, 1 );

		// Send Mail Booking with All attendees Pending
		add_action( 'hydra_booking/send_booking_with_all_attendees_pending', array( $this, 'send_booking_with_all_attendees_pending' ), 10, 1 );

		// Send Mail Booking with All attendees Canceled
		add_action( 'hydra_booking/send_booking_with_all_attendees_canceled', array( $this, 'send_booking_with_all_attendees_canceled' ), 10, 1 );

		// Send Mail Booking with All attendees Schedule
		add_action( 'hydra_booking/send_booking_with_all_attendees_schedule', array( $this, 'send_booking_with_all_attendees_schedule' ), 10, 1 );
	}

	private function get_activity_datetime() {
		$helper = new Helper();
		return wp_date( $helper->get_date_time_format_from_settings( 'M d, Y', 'h:i A' ) );
	}

	// Get Meeting Data
	public function getMeetingData( $meeting_id ) {
		$meeting      = new Meeting();
		$meeting_data = $meeting->get( $meeting_id );
		return get_post_meta( $meeting_data->post_id, '__tfhb_meeting_opt', true );
	}

	// Get Host Data
	public function getHostData( $host_id ) {
		$host      = new Host();
		$host_data = $host->getHostById(  $host_id );
		return $host_data;
	}
	// get admin email from general settings
	public function getAdminEmail() {
		$_tfhb_general_settings = !empty(get_option( '_tfhb_general_settings' )) && get_option( '_tfhb_general_settings' ) != false ? get_option( '_tfhb_general_settings' ) : array();
		$admin_email = isset($_tfhb_general_settings['admin_email']) && !empty($_tfhb_general_settings['admin_email']) && $_tfhb_general_settings['admin_email'] != '{{wp.admin_email}}' ? sanitize_email($_tfhb_general_settings['admin_email']) : get_bloginfo( 'admin_email' );
		return $admin_email;
	}

	// If booking Status is Complted
	public function pushBookingToConfirmed( $attendees ) {
		$bookingMeta                 = new BookingMeta();
		$Meeting_meta                = $this->getMeetingData( $attendees->meeting_id );
		$_tfhb_notification_settings = ! empty( $Meeting_meta['notification'] ) ? $Meeting_meta['notification'] : '';
		$hostData                    = $this->getHostData( $attendees->host_id );  

		
		if ( ! empty( $_tfhb_notification_settings ) ) {

			// Host Confirmation Email, If Settings Enable for Host Confirmation
			if ( ! empty( $_tfhb_notification_settings['host']['booking_confirmation']['status'] ) ) {
				     
			
				
				// From Email
				$replyTo = ! empty( $_tfhb_notification_settings['host']['booking_confirmation']['form'] ) ? $_tfhb_notification_settings['host']['booking_confirmation']['form'] : $this->getAdminEmail();

				// Email Subject
				$subject = ! empty( $_tfhb_notification_settings['host']['booking_confirmation']['subject'] ) ? $_tfhb_notification_settings['host']['booking_confirmation']['subject'] : 'Booking Confirmation';

				// Replace Shortcode to Values
				$subject = $this->replace_mail_tags( $subject, $attendees->id );
				
				
				// Setting Body
				$mailbody = ! empty( $_tfhb_notification_settings['host']['booking_confirmation']['body'] ) ? $_tfhb_notification_settings['host']['booking_confirmation']['body'] : ''; 
 	 
				// Replace Shortcode to Values
				$finalbody = $this->replace_mail_tags( $mailbody, $attendees->id );
		 
			
				// Result after Shortcode replce
				$body = wp_kses_post( $this->email_body_open() . $finalbody . $this->email_body_close() );


				// Host Email
				$mailto = ! empty( $hostData->email ) ? $hostData->email : '';

				$headers = array(
					'Reply-To: ' . $replyTo,
				);

				
				Mailer::send( $mailto, $subject, $body, $headers );

				// Add activity after email sent
				$bookingMeta->add([
					'booking_id' => $attendees->booking_id,
					'meta_key' => 'booking_activity',
					'value' => array( 
							'datetime' => $this->get_activity_datetime(), 
							'title' => 'Confirmation Email Sent', // translate it from Vue
							'description' => 'Confirmation Email Sent to Host',  // translate it from Vue
						)
					]
				);
			}

			// Attendee Confirmation Email, If Settings Enable for Attendee Confirmation
			if ( ! empty( $_tfhb_notification_settings['attendee']['booking_confirmation']['status'] ) ) {
				
				
				// From Email
				$replyTo = ! empty( $_tfhb_notification_settings['attendee']['booking_confirmation']['form'] ) ? $_tfhb_notification_settings['attendee']['booking_confirmation']['form'] : $this->getAdminEmail();

				// Email Subject
				$subject = ! empty( $_tfhb_notification_settings['attendee']['booking_confirmation']['subject'] ) ? $_tfhb_notification_settings['attendee']['booking_confirmation']['subject'] : 'Booking Confirmation';

				// Replace Shortcode to Values
				$subject = $this->replace_mail_tags( $subject, $attendees->id );
				
				
				// Setting Body
				$mailbody = ! empty( $_tfhb_notification_settings['attendee']['booking_confirmation']['body'] ) ? $_tfhb_notification_settings['attendee']['booking_confirmation']['body'] : ''; 

				
				// Replace Shortcode to Values
				$finalbody = $this->replace_mail_tags( $mailbody, $attendees->id );
			
				// Result after Shortcode replce
				$body = wp_kses_post( $this->email_body_open() . $finalbody . $this->email_body_close() );
 
				// Host Email
				$mailto = ! empty( $attendees->email ) ? $attendees->email : '';

				$headers = array(
					'Reply-To: ' . $replyTo,
				);

				
				Mailer::send( $mailto, $subject, $body, $headers );


				// Add activity after email sent
				$bookingMeta->add([
					'booking_id' => $attendees->booking_id,
					'meta_key' => 'booking_activity',
					'value' => array(
							 
							'datetime' => $this->get_activity_datetime(), 
							'title' =>  'Confirmation Email Sent', // translate it from Vue
							'description' => 'Confirmation Email Sent to Attendee', // translate it from Vue
						)
					]
				);
			}
		}
	}


	// If booking Status is Pending
	public function pushBookingToPending( $attendees ) {

		$bookingMeta                 = new BookingMeta();
		$Meeting_meta                = $this->getMeetingData( $attendees->meeting_id );
		$_tfhb_notification_settings = ! empty( $Meeting_meta['notification'] ) ? $Meeting_meta['notification'] : '';
		$hostData                    = $this->getHostData( $attendees->host_id );  

		if ( ! empty( $_tfhb_notification_settings ) ) {

			// Host Pending Email, If Settings Enable for Host Pending
			if ( ! empty( $_tfhb_notification_settings['host']['booking_pending']['status'] ) ) {
				
				// From Email
				$replyTo = ! empty( $_tfhb_notification_settings['host']['booking_pending']['form'] ) ? $_tfhb_notification_settings['host']['booking_pending']['form'] : $this->getAdminEmail();

				// Email Subject
				$subject = ! empty( $_tfhb_notification_settings['host']['booking_pending']['subject'] ) ? $_tfhb_notification_settings['host']['booking_pending']['subject'] : 'Booking Pending';

				// Replace Shortcode to Values
				$subject = $this->replace_mail_tags( $subject, $attendees->id );
				
				// Setting Body
				$mailbody = ! empty( $_tfhb_notification_settings['host']['booking_pending']['body'] ) ? $_tfhb_notification_settings['host']['booking_pending']['body'] : '';

				// Replace Shortcode to Values
				$finalbody = $this->replace_mail_tags( $mailbody, $attendees->id );

				// Result after Shortcode replce
				$body = wp_kses_post( $this->email_body_open() . $finalbody . $this->email_body_close() );

				// Host Email
				$mailto = ! empty( $hostData->email ) ? $hostData->email : '';

				$headers = array(
					'Reply-To: ' . $replyTo,
				);

				Mailer::send( $mailto, $subject, $body, $headers );

				// Add activity after email sent
				$bookingMeta->add([
					'booking_id' => $attendees->booking_id,
					'meta_key' => 'booking_activity',
					'value' => array(
							 
							'datetime' => $this->get_activity_datetime(),  
							'title' => 'Pending Email Sent', // translate it from Vue
							'description' => 'Pending Email Sent to Host', // translate it from Vue
						)
					]
				);
			}

			// Attendee Pending Email, If Settings Enable for Attendee Pending
			if ( ! empty( $_tfhb_notification_settings['attendee']['booking_pending']['status'] ) ) {
				// From Email
				$replyTo = ! empty( $_tfhb_notification_settings['attendee']['booking_pending']['form'] ) ? $_tfhb_notification_settings['attendee']['booking_pending']['form'] : $this->getAdminEmail();

				// Email Subject
				$subject = ! empty( $_tfhb_notification_settings['attendee']['booking_pending']['subject'] ) ? $_tfhb_notification_settings['attendee']['booking_pending']['subject'] : 'Booking Pending';

				// Replace Shortcode to Values
				$subject = $this->replace_mail_tags( $subject, $attendees->id );


				// Setting Body
				$mailbody = ! empty( $_tfhb_notification_settings['attendee']['booking_pending']['body'] ) ? $_tfhb_notification_settings['attendee']['booking_pending']['body'] : '';

				// Replace Shortcode to Values
				$finalbody = $this->replace_mail_tags( $mailbody, $attendees->id );

				// Result after Shortcode replce
				$body = wp_kses_post( $this->email_body_open() . $finalbody . $this->email_body_close() );

				// Attendee Email
				$mailto = ! empty( $attendees->email ) ? $attendees->email : '';

				$headers = array(
					'Reply-To: ' . $replyTo,
				);

				Mailer::send( $mailto, $subject, $body, $headers );

				// Add activity after email sent
				$bookingMeta->add([
					'booking_id' => $attendees->booking_id,
					'meta_key' => 'booking_activity',
					'value' => array(
							 
							'datetime' => $this->get_activity_datetime(),   
							'title' => 'Pending Email Sent', // translate it from Vue
							'description' => 'Pending Email Sent to Attendee', // translate it from Vue
						)
					]
				);
			}
		}
	}

	// If booking Status is Cancel
	public function pushBookingToCanceled( $attendees ) {
		$bookingMeta                 = new BookingMeta();
		$Meeting_meta                = $this->getMeetingData( $attendees->meeting_id );
		$_tfhb_notification_settings = ! empty( $Meeting_meta['notification'] ) ? $Meeting_meta['notification'] : '';
		$hostData                    = $this->getHostData( $attendees->host_id );

		
		if ( ! empty( $_tfhb_notification_settings ) ) {

			// Host Canceled Email, If Settings Enable for Host Canceled
			if ( ! empty( $_tfhb_notification_settings['host']['booking_cancel']['status'] ) ) {

				// From Email
				$replyTo = ! empty( $_tfhb_notification_settings['host']['booking_cancel']['form'] ) ? $_tfhb_notification_settings['host']['booking_cancel']['form'] : $this->getAdminEmail();

				// Email Subject
				$subject = ! empty( $_tfhb_notification_settings['host']['booking_cancel']['subject'] ) ? $_tfhb_notification_settings['host']['booking_cancel']['subject'] : 'Booking Canceled';

				// Replace Shortcode to Values
				$subject = $this->replace_mail_tags( $subject, $attendees->id );

				// Setting Body
				$mailbody = ! empty( $_tfhb_notification_settings['host']['booking_cancel']['body'] ) ? $_tfhb_notification_settings['host']['booking_cancel']['body'] : '';

				// Replace Shortcode to Values
				$finalbody = $this->replace_mail_tags( $mailbody, $attendees->id );

				// Result after Shortcode replce
				$body = wp_kses_post( $this->email_body_open() . $finalbody . $this->email_body_close() );

				// Host Email
				$mailto = ! empty( $hostData->email ) ? $hostData->email : '';

				$headers = array(
					'Reply-To: ' . $replyTo,
				);
				Mailer::send( $mailto, $subject, $body, $headers );

				// Add activity after email sent
				$bookingMeta->add([
					'booking_id' => $attendees->booking_id,
					'meta_key' => 'booking_activity',
					'value' => array(
							 
							'datetime' => $this->get_activity_datetime(),    
							'title' => 'Canceled Email Sent', // translate it from Vue
							'description' => 'Canceled Email Sent to Host', // translate it from Vue
						)
					]
				);
			}

			// Attendee Canceled Email, If Settings Enable for Attendee Canceled
			if ( ! empty( $_tfhb_notification_settings['attendee']['booking_cancel']['status'] ) ) {
				// From Email
				$replyTo = ! empty( $_tfhb_notification_settings['attendee']['booking_cancel']['form'] ) ? $_tfhb_notification_settings['attendee']['booking_cancel']['form'] : $this->getAdminEmail();

				// Email Subject
				$subject = ! empty( $_tfhb_notification_settings['attendee']['booking_cancel']['subject'] ) ? $_tfhb_notification_settings['attendee']['booking_cancel']['subject'] : 'Booking Canceled';

				// Replace Shortcode to Values
				$subject = $this->replace_mail_tags( $subject, $attendees->id );

				// Setting Body
				$mailbody = ! empty( $_tfhb_notification_settings['attendee']['booking_cancel']['body'] ) ? $_tfhb_notification_settings['attendee']['booking_cancel']['body'] : '';

				// Replace Shortcode to Values
				$finalbody = $this->replace_mail_tags( $mailbody, $attendees->id );

				// Result after Shortcode replce
				$body = wp_kses_post( $this->email_body_open() . $finalbody . $this->email_body_close() );

				// Attendee Email
				$mailto = ! empty( $attendees->email ) ? $attendees->email : '';

				$headers = array(
					'Reply-To: ' . $replyTo,
				);

				Mailer::send( $mailto, $subject, $body, $headers );

				// Add activity after email sent
				$bookingMeta->add([
					'booking_id' => $attendees->booking_id,
					'meta_key' => 'booking_activity',
					'value' => array(
							 
							'datetime' => $this->get_activity_datetime(),
							'title' => 'Canceled Email Sent', // translate it from Vue
							'description' => 'Canceled Email Sent to Attendee', // translate it from Vue
						)
					]
				);
			}
		}
	}

	// If booking Status is ReSchedule
	public function pushBookingToscheduled( $old_booking_id,  $attendees ) { 
		 
		$bookingMeta                 = new BookingMeta();
		$Meeting_meta                = $this->getMeetingData( $attendees->meeting_id );
		$_tfhb_notification_settings = ! empty( $Meeting_meta['notification'] ) ? $Meeting_meta['notification'] : '';
		$hostData                    = $this->getHostData( $attendees->host_id );

		if ( ! empty( $_tfhb_notification_settings ) ) {

			// Host ReSchedule Email, If Settings Enable for Host ReSchedule
			if ( ! empty( $_tfhb_notification_settings['host']['booking_reschedule']['status'] ) ) {
				// From Email
				$replyTo = ! empty( $_tfhb_notification_settings['host']['booking_reschedule']['form'] ) ? $_tfhb_notification_settings['host']['booking_reschedule']['form'] : $this->getAdminEmail();
 
				// Email Subject
				$subject = ! empty( $_tfhb_notification_settings['host']['booking_reschedule']['subject'] ) ? $_tfhb_notification_settings['host']['booking_reschedule']['subject'] : 'Booking ReSchedule';
				
				// Replace Shortcode to Values
				$subject = $this->replace_mail_tags( $subject, $attendees->id );


				// Setting Body
				$mailbody = ! empty( $_tfhb_notification_settings['host']['booking_reschedule']['body'] ) ? $_tfhb_notification_settings['host']['booking_reschedule']['body'] : '';

				// Replace Shortcode to Values
				$finalbody = $this->replace_mail_tags( $mailbody, $attendees->id );

				// Result after Shortcode replce
				$body = wp_kses_post( $this->email_body_open() . $finalbody . $this->email_body_close() );

				// Host Email
				$mailto = ! empty( $hostData->host_email ) ? $hostData->host_email : '';

				$headers = array(
					'Reply-To: ' . $replyTo,
				);

				Mailer::send( $mailto, $subject, $body, $headers );

				// Add activity after email sent
				$bookingMeta->add([
					'booking_id' => $attendees->booking_id,
					'meta_key' => 'booking_activity',
					'value' => array( 
							'datetime' => $this->get_activity_datetime(),
							'title' => 'ReSchedule Email Sent', // translate it from Vue
							'description' => 'ReSchedule Email Sent to Host', // translate it from Vue
						)
					]
				);
			}

			// Attendee ReSchedule Email, If Settings Enable for Attendee ReSchedule
			if ( ! empty( $_tfhb_notification_settings['attendee']['booking_reschedule']['status'] ) ) {
				// From Email
				$replyTo = ! empty( $_tfhb_notification_settings['attendee']['booking_reschedule']['form'] ) ? $_tfhb_notification_settings['attendee']['booking_reschedule']['form'] : $this->getAdminEmail();

				// Email Subject
				$subject = ! empty( $_tfhb_notification_settings['attendee']['booking_reschedule']['subject'] ) ? $_tfhb_notification_settings['attendee']['booking_reschedule']['subject'] : 'Booking ReSchedule';

				// Replace Shortcode to Values
				$subject = $this->replace_mail_tags( $subject, $attendees->id );

				// Setting Body
				$mailbody = ! empty( $_tfhb_notification_settings['attendee']['booking_reschedule']['body'] ) ? $_tfhb_notification_settings['attendee']['booking_reschedule']['body'] : '';

				// Replace Shortcode to Values
				$finalbody = $this->replace_mail_tags( $mailbody, $attendees->id );

				// Result after Shortcode replce
				$body = wp_kses_post( $this->email_body_open() . $finalbody . $this->email_body_close() );

				// Attendee Email
				$mailto = ! empty( $attendees->email ) ? $attendees->email : '';

				$headers = array(
					'Reply-To: ' . $replyTo,
				);

				Mailer::send( $mailto, $subject, $body, $headers );

				// Add activity after email sent
				$bookingMeta->add([
					'booking_id' => $attendees->booking_id,
					'meta_key' => 'booking_activity',
					'value' => array( 
							'datetime' => $this->get_activity_datetime(),
							'title' => 'ReSchedule Email Sent', // translate it from Vue
							'description' => 'ReSchedule Email Sent to Attendee', // translate it from Vue
						)
					]
				);
			}
		}
	}


		// If booking Status is ReSchedule
	public function send_booking_reminder( $booking ) {
		$bookingMeta                 = new BookingMeta();
		$Meeting_meta                = $this->getMeetingData( $booking->meeting_id );
		$_tfhb_notification_settings = ! empty( $Meeting_meta['notification'] ) ? $Meeting_meta['notification'] : '';
		$hostData                    = $this->getHostData( $booking->host_id );


		if ( ! empty( $_tfhb_notification_settings ) ) {
			$attendees = $booking->attendees;
	 
			// Attendee ReSchedule Email, If Settings Enable for Attendee ReSchedule
			if ( ! empty( $_tfhb_notification_settings['attendee']['booking_reminder']['status'] ) ) {
				foreach($attendees as $key => $attendee_data){
					// From Email
					$replyTo = ! empty( $_tfhb_notification_settings['attendee']['booking_reminder']['form'] ) ? $_tfhb_notification_settings['attendee']['booking_reminder']['form'] : $this->getAdminEmail();

					// Email Subject
					$subject = ! empty( $_tfhb_notification_settings['attendee']['booking_reminder']['subject'] ) ? $_tfhb_notification_settings['attendee']['booking_reminder']['subject'] : 'Booking ReSchedule';

					// Replace Shortcode to Values
					$subject = $this->replace_mail_tags( $subject, $attendee_data->id );

					// Setting Body
					$mailbody = ! empty( $_tfhb_notification_settings['attendee']['booking_reminder']['body'] ) ? $_tfhb_notification_settings['attendee']['booking_reminder']['body'] : '';

					// Replace Shortcode to Values
					$finalbody = $this->replace_mail_tags( $mailbody, $attendee_data->id );

					// Result after Shortcode replce
					$body = wp_kses_post( $this->email_body_open() . $finalbody . $this->email_body_close() );

					// Attendee Email
					$mailto = ! empty( $attendee_data->email ) ? $attendee_data->email : ''; 
					$headers = array(
						'Reply-To: ' . $replyTo,
					);

					Mailer::send( $mailto, $subject, $body, $headers );

						// Add activity after email sent
					$bookingMeta->add([
						'booking_id' => $attendees->booking_id,
						'meta_key' => 'booking_activity',
						'value' => array( 
								'datetime' => $this->get_activity_datetime(), 
								'title' => 'Reminder Email Sent', // translate it from Vue
								'description' => 'Reminder Email Sent to Attendee', // translate it from Vue
							)
						]
					);
				}
			}
		}
	}

	/**
	 * Send Mail Booking with All attendees
	 */
	public function send_booking_with_all_attendees_confirmed( $booking ) { 
		$bookingMeta                 = new BookingMeta();
		$Meeting_meta                = $this->getMeetingData( $booking->meeting_id );
		$_tfhb_notification_settings = ! empty( $Meeting_meta['notification'] ) ? $Meeting_meta['notification'] : '';
		$hostData                    = $this->getHostData( $booking->host_id );
		 
		$attendees = $booking->attendees;
 
		if ( ! empty( $_tfhb_notification_settings ) ) { 
			// Attendee ReSchedule Email, If Settings Enable for Attendee ReSchedule
			if ( ! empty( $_tfhb_notification_settings['attendee']['booking_confirmation']['status'] ) ) {
				foreach($attendees as $key => $attendee_data){
					// From Email
					$replyTo = ! empty( $_tfhb_notification_settings['attendee']['booking_confirmation']['form'] ) ? $_tfhb_notification_settings['attendee']['booking_confirmation']['form'] : $this->getAdminEmail();

					// Email Subject
					$subject = ! empty( $_tfhb_notification_settings['attendee']['booking_confirmation']['subject'] ) ? $_tfhb_notification_settings['attendee']['booking_confirmation']['subject'] : 'Booking ReSchedule';

					// Replace Shortcode to Values
					$subject = $this->replace_mail_tags( $subject, $attendee_data->id );

					// Setting Body
					$mailbody = ! empty( $_tfhb_notification_settings['attendee']['booking_confirmation']['body'] ) ? $_tfhb_notification_settings['attendee']['booking_confirmation']['body'] : '';

					// Replace Shortcode to Values
					$finalbody = $this->replace_mail_tags( $mailbody, $attendee_data->id );

					// Result after Shortcode replce
					$body = wp_kses_post( $this->email_body_open() . $finalbody . $this->email_body_close() );

					// Attendee Email
					$mailto = ! empty( $attendee_data->email ) ? $attendee_data->email : ''; 
					$headers = array(
						'Reply-To: ' . $replyTo,
					);
 
					Mailer::send( $mailto, $subject, $body, $headers );

					
				}
				// Add activity after email sent
				$bookingMeta->add([
					'booking_id' => $booking->id,
					'meta_key' => 'booking_activity',
					'value' => array( 
							'datetime' => $this->get_activity_datetime(),  
							'title' => 'Booking Has Been Confirmed', // translate it from Vue
							'description' => 'Confirmation Email Sent to Attendee', // translate it from Vue
						)
					]
				);
			}
		}
	}
	/**
	 * Send Mail Booking with All attendees Pending
	 */
	public function send_booking_with_all_attendees_pending( $booking ) {
		$bookingMeta                 = new BookingMeta();
		$Meeting_meta                = $this->getMeetingData( $booking->meeting_id );
		$_tfhb_notification_settings = ! empty( $Meeting_meta['notification'] ) ? $Meeting_meta['notification'] : '';
		$hostData                    = $this->getHostData( $booking->host_id );
		 
		$attendees = $booking->attendees; 

		if ( ! empty( $_tfhb_notification_settings ) ) { 
			// Attendee ReSchedule Email, If Settings Enable for Attendee ReSchedule
			if ( ! empty( $_tfhb_notification_settings['attendee']['booking_pending']['status'] ) ) {
				foreach($attendees as $key => $attendee_data){
					// From Email
					$replyTo = ! empty( $_tfhb_notification_settings['attendee']['booking_pending']['form'] ) ? $_tfhb_notification_settings['attendee']['booking_pending']['form'] : $this->getAdminEmail();

					// Email Subject
					$subject = ! empty( $_tfhb_notification_settings['attendee']['booking_pending']['subject'] ) ? $_tfhb_notification_settings['attendee']['booking_pending']['subject'] : 'Booking ReSchedule';

					// Replace Shortcode to Values
					$subject = $this->replace_mail_tags( $subject, $attendee_data->id );

					// Setting Body
					$mailbody = ! empty( $_tfhb_notification_settings['attendee']['booking_pending']['body'] ) ? $_tfhb_notification_settings['attendee']['booking_pending']['body'] : '';

					// Replace Shortcode to Values
					$finalbody = $this->replace_mail_tags( $mailbody, $attendee_data->id );

					// Result after Shortcode replce
					$body = wp_kses_post( $this->email_body_open() . $finalbody . $this->email_body_close() );

					// Attendee Email
					$mailto = ! empty( $attendee_data->email ) ? $attendee_data->email : ''; 
					$headers = array(
						'Reply-To: ' . $replyTo,
					);
 
					Mailer::send( $mailto, $subject, $body, $headers );

				
				}
				// Add activity after email sent
				$bookingMeta->add([
					'booking_id' => $booking->id,
					'meta_key' => 'booking_activity',
					'value' => array( 
							'datetime' => $this->get_activity_datetime(),   
							'title' => 'Booking Has Been Pending', // translate it from Vue
							'description' => 'Pending Email Sent to Attendee', // translate it from Vue
						)
					]
				);
			
			}
		}
	}
	/**
	 * Send Mail Booking with All attendees Canceled
	 */
	public function send_booking_with_all_attendees_canceled( $booking ) {
		$bookingMeta                 = new BookingMeta();
		$Meeting_meta                = $this->getMeetingData( $booking->meeting_id );
		$_tfhb_notification_settings = ! empty( $Meeting_meta['notification'] ) ? $Meeting_meta['notification'] : '';
		$hostData                    = $this->getHostData( $booking->host_id );
		 
		$attendees = $booking->attendees;
 
		if ( ! empty( $_tfhb_notification_settings ) ) { 
			// Attendee ReSchedule Email, If Settings Enable for Attendee ReSchedule
			if ( ! empty( $_tfhb_notification_settings['attendee']['booking_cancel']['status'] ) ) {
				foreach($attendees as $key => $attendee_data){
					// From Email
					$replyTo = ! empty( $_tfhb_notification_settings['attendee']['booking_cancel']['form'] ) ? $_tfhb_notification_settings['attendee']['booking_cancel']['form'] : $this->getAdminEmail();

					// Email Subject
					$subject = ! empty( $_tfhb_notification_settings['attendee']['booking_cancel']['subject'] ) ? $_tfhb_notification_settings['attendee']['booking_cancel']['subject'] : 'Booking ReSchedule';

					// Replace Shortcode to Values
					$subject = $this->replace_mail_tags( $subject, $attendee_data->id );

					// Setting Body
					$mailbody = ! empty( $_tfhb_notification_settings['attendee']['booking_cancel']['body'] ) ? $_tfhb_notification_settings['attendee']['booking_cancel']['body'] : '';

					// Replace Shortcode to Values
					$finalbody = $this->replace_mail_tags( $mailbody, $attendee_data->id );
				
					// Result after Shortcode replce
					$body = wp_kses_post( $this->email_body_open() . $finalbody . $this->email_body_close() );

					// Attendee Email
					$mailto = ! empty( $attendee_data->email ) ? $attendee_data->email : ''; 
					$headers = array(
						'Reply-To: ' . $replyTo,
					);
 
					Mailer::send( $mailto, $subject, $body, $headers );

					
				}
				// Add activity after email sent
				$bookingMeta->add([
					'booking_id' => $booking->id,
					'meta_key' => 'booking_activity',
					'value' => array( 
							'datetime' => $this->get_activity_datetime(),    
							'title' => 'Booking Has Been Canceled', // translate it from Vue
							'description' => 'Canceled Email Sent to Attendee', // translate it from Vue
						)
					]
				);
				
			}
		}
	}

	/**
	 * Send Mail Booking with All attendees Schedule
	 */
	public function send_booking_with_all_attendees_schedule( $booking ) {
		$bookingMeta                 = new BookingMeta();
		$Meeting_meta                = $this->getMeetingData( $booking->meeting_id );
		$_tfhb_notification_settings = ! empty( $Meeting_meta['notification'] ) ? $Meeting_meta['notification'] : '';
		$hostData                    = $this->getHostData( $booking->host_id );
		 
		$attendees = $booking->attendees;
 

		if ( ! empty( $_tfhb_notification_settings ) ) { 
			// Attendee ReSchedule Email, If Settings Enable for Attendee ReSchedule
			if ( ! empty( $_tfhb_notification_settings['attendee']['booking_reschedule']['status'] ) ) {
				foreach($attendees as $key => $attendee_data){
					// From Email
					$replyTo = ! empty( $_tfhb_notification_settings['attendee']['booking_reschedule']['form'] ) ? $_tfhb_notification_settings['attendee']['booking_reschedule']['form'] : $this->getAdminEmail();

					// Email Subject
					$subject = ! empty( $_tfhb_notification_settings['attendee']['booking_reschedule']['subject'] ) ? $_tfhb_notification_settings['attendee']['booking_reschedule']['subject'] : 'Booking ReSchedule';

					// Replace Shortcode to Values
					$subject = $this->replace_mail_tags( $subject, $attendee_data->id );

					// Setting Body
					$mailbody = ! empty( $_tfhb_notification_settings['attendee']['booking_reschedule']['body'] ) ? $_tfhb_notification_settings['attendee']['booking_reschedule']['body'] : '';

					// Replace Shortcode to Values
					$finalbody = $this->replace_mail_tags( $mailbody, $attendee_data->id );

					// Result after Shortcode replce
					$body = wp_kses_post( $this->email_body_open() . $finalbody . $this->email_body_close() );

					// Attendee Email
					$mailto = ! empty( $attendee_data->email ) ? $attendee_data->email : ''; 
					$headers = array(
						'Reply-To: ' . $replyTo,
					);
 
					Mailer::send( $mailto, $subject, $body, $headers );

					
				}

				// Add activity after email sent
				$bookingMeta->add([
					'booking_id' => $booking->id,
					'meta_key' => 'booking_activity',
					'value' => array( 
							'datetime' => $this->get_activity_datetime(),     
							'title' => 'Booking Has Been Rescheduled', // translate it from Vue
							'description' => 'ReSchedule Email Sent to Attendee', // translate it from Vue
						)
					]
				);
				
			}
		}
	}

	/**
	 * email body open markup
	 */
	public function email_body_open() {
		// email body open
		$email_body_open = '<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1"><link rel="preconnect" href="https://fonts.googleapis.com"></head><body style="margin: 0; padding: 0; background-color: #E1F2E4;">';
		return $email_body_open;
	}

	/**
	 * email body close markup
	 */
	public function email_body_close() {
		// email body close
		$email_body_close = '</body></html>';
		return $email_body_close;
	}

	/**
	 * Resolve locale for attendee email template rendering.
	 */
	private function get_mail_template_locale( $attendeeBooking ) {
		$locale = get_locale();

		if ( ! empty( $attendeeBooking->attendee_locale ) ) {
			$locale = $attendeeBooking->attendee_locale;
		} elseif ( ! empty( $attendeeBooking->locale ) ) {
			$locale = $attendeeBooking->locale;
		} elseif ( ! empty( $attendeeBooking->language ) ) {
			$locale = $attendeeBooking->language;
		}

		return apply_filters( 'hydra_booking/mail_template_locale', $locale, $attendeeBooking );
	}

	/**
	 * Format date string in active locale when possible.
	 */
	private function localize_meeting_dates( $meeting_dates ) {
		if ( empty( $meeting_dates ) ) {
			return '';
		}

		$date_format = get_option( 'date_format', 'M d, Y' );
		$parts       = array_map( 'trim', explode( ',', (string) $meeting_dates ) );
		$output      = array();

		foreach ( $parts as $part ) {
			if ( '' === $part ) {
				continue;
			}

			$timestamp = strtotime( $part );
			$output[]  = false !== $timestamp ? wp_date( $date_format, $timestamp ) : $part;
		}

		if ( empty( $output ) ) {
			return (string) $meeting_dates;
		}

		return implode( ', ', $output );
	}

	/**
	 * Format time string in active locale when possible.
	 */
	private function localize_time_value( $time_value ) {
		$time_value = trim( (string) $time_value );

		if ( '' === $time_value ) {
			return '';
		}

		$time_format = get_option( 'time_format', 'g:i a' );
		$timestamp   = strtotime( '1970-01-01 ' . $time_value );

		if ( false === $timestamp ) {
			return $time_value;
		}

		return wp_date( $time_format, $timestamp );
	}

	/**
	 * Format duration with locale-aware pluralization when duration is numeric.
	 */
	private function localize_duration_value( $duration_value ) {
		$duration_value = trim( (string) $duration_value );

		if ( '' === $duration_value ) {
			return '';
		}

		if ( ctype_digit( $duration_value ) ) {
			$minutes = (int) $duration_value;
			return sprintf(
				/* translators: %s is the meeting duration in minutes. */
				_n( '%s minute', '%s minutes', $minutes, 'hydra-booking' ),
				number_format_i18n( $minutes )
			);
		}

		return $duration_value;
	}

	/**
	 * Human readable timezone label with translation hook.
	 */
	private function localize_timezone_label( $timezone_value, $attendeeBooking ) {
		$timezone_value = (string) $timezone_value;
		$label          = str_replace( '_', ' ', $timezone_value );

		return apply_filters( 'hydra_booking/mail_template_timezone_label', $label, $timezone_value, $attendeeBooking );
	}

	/**
	 * Per-tag value translation hook for multilingual integrations.
	 */
	private function localize_mail_tag_value( $tag, $value, $attendeeBooking, $locale ) {
		return apply_filters( 'hydra_booking/mail_template_tag_value', $value, $tag, $attendeeBooking, $locale );
	}

	/**
	 * Translate default template copy that is saved as static text in email HTML.
	 */
	private function translate_default_template_copy( $template, $attendeeBooking, $locale ) {
		$replace_map = array(
			'Hey' => __( 'Hey', 'hydra-booking' ),
			'Your booking has been scheduled' => __( 'Your booking has been scheduled', 'hydra-booking' ),
			'Meeting Details' => __( 'Meeting Details', 'hydra-booking' ),
			'Date & Time:' => __( 'Date & Time:', 'hydra-booking' ),
			'A new booking with ' => __( 'A new booking with ', 'hydra-booking' ),
			'was confirmed.' => __( 'was confirmed.', 'hydra-booking' ),
			'Host:' => __( 'Host:', 'hydra-booking' ),
			'About:' => __( 'About:', 'hydra-booking' ),
			'Description:' => __( 'Description:', 'hydra-booking' ),
			'Location:' => __( 'Location:', 'hydra-booking' ),
			'Host Details' => __( 'Host Details', 'hydra-booking' ),
			'Name:' => __( 'Name:', 'hydra-booking' ),
			'Email:' => __( 'Email:', 'hydra-booking' ),
			'Phone:' => __( 'Phone:', 'hydra-booking' ),
			'Instructions' => __( 'Instructions', 'hydra-booking' ),
			'Please <strong>join the event five minutes before the event starts</strong> based on your time zone.' => sprintf(
				/* translators: 1: opening strong tag, 2: closing strong tag, 3: localized "Date & Time" label. */
				__( 'Please %1$sjoin the event five minutes before the event starts%2$s based on your time zone. The %3$s is listed above for your reference.', 'hydra-booking' ),
				'<strong>',
				'</strong>',
				__( 'Date & Time', 'hydra-booking' )
			),

			'Ensure you have a good internet connection, a quality camera, and a quiet space.' => __( 'Ensure you have a good internet connection, a quality camera, and a quiet space.', 'hydra-booking' ),
			'You can cancel or reschedule this event for any reason.' => __( 'You can cancel or reschedule this event for any reason.', 'hydra-booking' ),
			'Cancel' => __( 'Cancel', 'hydra-booking' ),
			'Reschedule' => __( 'Reschedule', 'hydra-booking' ),
			'Add To Calendar' => __( 'Add To Calendar', 'hydra-booking' ), 
			'Join Meeting' => __( 'Join Meeting', 'hydra-booking' ),
			'Password :' => __( 'Password :', 'hydra-booking' ),
			'Host time:' => __( 'Host time:', 'hydra-booking' ),
		);

		foreach ( $replace_map as $source => $translated ) {
			$translated = apply_filters( 'hydra_booking/mail_template_default_text', $translated, $source, $attendeeBooking, $locale );

			if ( ! empty( $translated ) && $translated !== $source ) {
				$template = str_replace( $source, $translated, $template );
			}
		}

		return $template; 
	}

	/**
	 * Replace all available mail tags
	 */
	public function replace_mail_tags( $template, $attendee_id ) { 
		
		$Attendee = new Attendees();
		$attendeeBooking =  $Attendee->getAttendeeWithBooking( 
			array(
				array('id', '=',$attendee_id),
			),
			1,
			'DESC'
		); 

		$locale       = $this->get_mail_template_locale( $attendeeBooking );
		$did_switch   = false;
		$old_locale   = function_exists( 'determine_locale' ) ? determine_locale() : get_locale();
		if ( function_exists( 'switch_to_locale' ) && ! empty( $locale ) && $locale !== $old_locale ) {
			$did_switch = switch_to_locale( $locale );
		}

		$meeting_title          = ! empty( $attendeeBooking->meeting_title ) ? $attendeeBooking->meeting_title : '';
		$meeting_content        = ! empty( $attendeeBooking->meeting_content ) ? $attendeeBooking->meeting_content : '';
		$meeting_dates          = ! empty( $attendeeBooking->meeting_dates ) ? $this->localize_meeting_dates( $attendeeBooking->meeting_dates ) : '';
		$localized_duration     = $this->localize_duration_value( $attendeeBooking->duration );
		$localized_start_time   = $this->localize_time_value( $attendeeBooking->start_time );
		$localized_end_time     = $this->localize_time_value( $attendeeBooking->end_time );
		$localized_timezone     = $this->localize_timezone_label( $attendeeBooking->attendee_time_zone, $attendeeBooking );
		$localized_host_name    = trim( $attendeeBooking->host_first_name . ' ' . $attendeeBooking->host_last_name );
		$localized_attendee_name = ! empty( $attendeeBooking->attendee_name ) ? $attendeeBooking->attendee_name : '';

		// Initialize Helper to use settings-based date format
		$helper = new Helper();
		$date_format = $helper->get_date_format_from_settings( 'M d, Y' );

		$google_calendar_link  = '#';
		$outlook_calendar_link = '#';
		$yahoo_calendar_link   = '#';
		$other_calendar_link   = '#';

		// Query-arg based add-to-calendar links
		if ( ! empty( $attendeeBooking->hash ) && ! empty( $attendeeBooking->meeting_id ) ) {
			$google_base_hash = [
				'attendee_id' => $attendeeBooking->id,
				'type' => 'google', // This can be used to identify the calendar type in the add-to-calendar handler
			];  
			$google_base_hash = base64_encode( wp_json_encode( $google_base_hash ) );  
			$google_calendar_link  =home_url( '?hydra-add-to-calendar=' . $google_base_hash );

			// outlook base hash
			$outlook_base_hash = [
				'attendee_id' => $attendeeBooking->id,
				'type' => 'outlook', // This can be used to identify the calendar type in the add-to-calendar handler
			];
			$outlook_base_hash = base64_encode( wp_json_encode( $outlook_base_hash ) );
			$outlook_calendar_link = home_url( '?hydra-add-to-calendar=' . $outlook_base_hash );

			// yahoo base hash
			$yahoo_base_hash = [
				'attendee_id' => $attendeeBooking->id,
				'type' => 'yahoo', // This can be used to identify the calendar type in the add-to-calendar handler
			];
			$yahoo_base_hash = base64_encode( wp_json_encode( $yahoo_base_hash ) );
			$yahoo_calendar_link = home_url( '?hydra-add-to-calendar=' . $yahoo_base_hash );
			 
			// other calendar base hash
			$other_base_hash = [
				'attendee_id' => $attendeeBooking->id,
				'type' => 'other', // This can be used to identify the calendar type in the add-to-calendar handler
			];
			$other_base_hash = base64_encode( wp_json_encode( $other_base_hash ) );
			$other_calendar_link = home_url( '?hydra-add-to-calendar=' . $other_base_hash );
		}
 
  
			// $google_calendar_link = 'https://calendar.google.com/calendar/r/eventedit?dates=20260223T122000/20260223T130000&text=Maiores aut sed rati Between admin  and Lillith Mccarty&details';
			// $outlook_calendar_link = '#';
			// $yahoo_calendar_link = '#';
			// $other_calendar_link = '#';
		 
		// Meeting Location Check
		$meeting_locations =  !is_array($attendeeBooking->meeting_locations) ?  json_decode( $attendeeBooking->meeting_locations ) : $attendeeBooking->meeting_locations;
		$locations         = array();
		if ( is_array( $meeting_locations ) ) {
			foreach ( $meeting_locations as $location ) {
				if ( isset( $location->location ) ) {
					$locations[] = $location->location;
				}
			}
		}
		
		// Format meeting date using settings-based date format
		$formatted_meeting_date = '';
		if ( ! empty( $attendeeBooking->meeting_dates ) ) {
			$date_obj = \DateTime::createFromFormat( 'Y-m-d', trim( $attendeeBooking->meeting_dates ) );
			if ( $date_obj !== false ) {
				$formatted_meeting_date = $date_obj->format( $date_format );
			} else {
				$formatted_meeting_date = $attendeeBooking->meeting_dates;
			}
		}

		$replacements = array(
			'{{meeting.title}}'    => $meeting_title,
			'{{meeting.content}}'  => $meeting_content,
			'{{meeting.date}}'     => $meeting_dates,
			'{{meeting.title}}'    => ! empty( $attendeeBooking->meeting_title ) ? $attendeeBooking->meeting_title : '',
			'{{meeting.content}}'  => ! empty( $attendeeBooking->meeting_content ) ? $attendeeBooking->meeting_content : '',
			'{{meeting.date}}'     => $formatted_meeting_date,
			'{{meeting.location}}' => implode( ', ', $locations ),
			'{{meeting.duration}}' => $localized_duration,
			'{{meeting.time}}'     => $localized_start_time . '-' . $localized_end_time,
			'{{host.name}}'        => $localized_host_name,
			'{{host.email}}'       => ! empty( $attendeeBooking->host_email ) ? $attendeeBooking->host_email : '',
			'{{host.phone}}'       => ! empty( $attendeeBooking->host_phone ) ? $attendeeBooking->host_phone : '',
			'{{attendee.name}}'    => $localized_attendee_name,
			'{{attendee.email}}'   => ! empty( $attendeeBooking->email ) ? $attendeeBooking->email : '', 
			'{{booking.add_to_calendar.google}}'   => ! empty( $google_calendar_link ) ? htmlspecialchars($google_calendar_link, ENT_QUOTES, 'UTF-8') : '#', 
			'{{booking.add_to_calendar.outlook}}'   => ! empty( $outlook_calendar_link ) ? htmlspecialchars($outlook_calendar_link, ENT_QUOTES, 'UTF-8') : '#', 
			'{{booking.add_to_calendar.yahoo}}'   => ! empty( $yahoo_calendar_link ) ? htmlspecialchars($yahoo_calendar_link, ENT_QUOTES, 'UTF-8') : '#', 
			'{{booking.add_to_calendar.other}}'   => ! empty( $other_calendar_link ) ? htmlspecialchars($other_calendar_link, ENT_QUOTES, 'UTF-8') : '#',
	
		);
		// tfhb_print_r($replacements);
		
		// Additional Data
		if( !empty($attendeeBooking->others_info) && $attendeeBooking->others_info != NULL ){
			$additional_data = json_decode($attendeeBooking->others_info);
			$others_info_html = '<ul>';
			foreach ($additional_data as $key => $value) {
				$others_info_html .= '<li>'.$key.' : '.$value.'</li>'; 
			}
			$others_info_html .= '</ul>';
			$replacements['{{attendee.additional_data}}'] = $others_info_html;
		}
		 
		// reason
		if( !empty($attendeeBooking->reason) && $attendeeBooking->reason != NULL ){
			$replacements['{{booking.cancel_reason}}'] = $attendeeBooking->reason;
			$replacements['{{booking.rescheduled_reason}}'] = $attendeeBooking->reason;
		}
		
		
		if($attendeeBooking->attendee_can_cancel == 1){ 
		
			$cancel_link = home_url( '?hydra-booking=booking&hash=' . $attendeeBooking->hash . '&meetingId=' . $attendeeBooking->meeting_id  . '&type=cancel' );
			$replacements['{{booking.cancel_link}}'] = $cancel_link;
		}
		if( $attendeeBooking->attendee_can_reschedule == 1){ 
			$rescheduled_link = home_url( '?hydra-booking=booking&hash=' . $attendeeBooking->hash . '&meetingId=' . $attendeeBooking->meeting_id . '&type=reschedule' );
			$replacements['{{booking.rescheduled_link}}'] = $rescheduled_link;
		}
		
		// Full start end time with timezone for attendee 
		$replacements['{{booking.full_start_end_attendee_timezone}}'] = $localized_start_time.' - '.$localized_end_time.' ('.$localized_timezone.')';
		$replacements['{{booking.start_date_time_for_attendee}}'] = $localized_start_time. ' ('.$localized_timezone.')';
		
	
		// Full start end time with timezone for host
		$dateTime = new DateTimeController( 'UTC' );
		$metting_dates = explode(',', $attendeeBooking->meeting_dates);
		if($attendeeBooking->availability_time_zone != ''){
			$full_start_end_host_timezone = $dateTime->convert_full_start_end_host_timezone_with_date( $attendeeBooking->start_time, $attendeeBooking->end_time, $attendeeBooking->attendee_time_zone, $attendeeBooking->availability_time_zone,  $metting_dates[0], 'full' );  
			$full_start_end_host_timezone = str_replace( 'Host time:', __( 'Host time:', 'hydra-booking' ), $full_start_end_host_timezone );
			$replacements['{{booking.full_start_end_host_timezone}}'] = $full_start_end_host_timezone;

			$start_date_time_for_host = $dateTime->convert_full_start_end_host_timezone_with_date( $attendeeBooking->start_time, $attendeeBooking->end_time, $attendeeBooking->attendee_time_zone, $attendeeBooking->availability_time_zone,  $metting_dates[0], 'start' );
			$start_date_time_for_host = str_replace( 'Host time:', __( 'Host time:', 'hydra-booking' ), $start_date_time_for_host );
			$replacements['{{booking.start_date_time_for_host}}'] =  $start_date_time_for_host;
		}else{
			$replacements['{{booking.full_start_end_host_timezone}}'] = $localized_start_time.' - '.$localized_end_time.' ('.$localized_timezone.')';

			$replacements['{{booking.start_date_time_for_host}}'] = $localized_start_time. ' ('.$localized_timezone.')';
		}

		foreach ( $replacements as $tag => $value ) {
			$replacements[ $tag ] = $this->localize_mail_tag_value( $tag, $value, $attendeeBooking, $locale );
		}
 
		if( !empty($attendeeBooking->meeting_locations) && $attendeeBooking->meeting_locations != NULL  ){
			$booking_locations = json_decode($attendeeBooking->meeting_locations); 
			
			$booking_locations_html = '';
			foreach ($booking_locations as $key => $value) { 
				if($key == 'zoom'){
					$link = $value->address->link;
					$password = $value->address->password;  
					$booking_locations_html .= '<b>'.$value->location.' :</b> <a href="'.esc_url($link).'" target="_blank">Join Meeting</a> <br> <b>Password :</b> '.esc_html($password).'<br>';
				}else{
					$booking_locations_html .= '<b>'.$value->location.' :</b> '.$value->address.'<br>'; 
				}
			}

			$replacements['{{booking.location_details_html}}'] = $booking_locations_html;
		}  
		$tags   = array_keys( $replacements );
		$values = array_values( $replacements ); 
		$template = str_replace( $tags, $values, $template );
		$template = $this->translate_default_template_copy( $template, $attendeeBooking, $locale );

		if ($attendeeBooking->attendee_can_cancel != 1 && $attendeeBooking->attendee_can_reschedule != 1) {
			libxml_use_internal_errors(true); // Suppress warnings for invalid HTML
			$dom = new \DOMDocument();
			$dom->loadHTML(mb_convert_encoding($template, 'HTML-ENTITIES', 'UTF-8'));

			$xpath = new \DOMXPath($dom);
			foreach ($xpath->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' tfhb-cancel-reschedule-btn ')]") as $node) {
				$node->parentNode->removeChild($node);
			}

			$template = $dom->saveHTML($dom->getElementsByTagName('body')->item(0));
			$template = preg_replace('/^<body>|<\/body>$/', '', $template); // Strip body tags
		} elseif ($attendeeBooking->attendee_can_cancel == 1 && $attendeeBooking->attendee_can_reschedule != 1) {
			// Remove only cancel button
			libxml_use_internal_errors(true);
			$dom = new \DOMDocument();
			$dom->loadHTML(mb_convert_encoding($template, 'HTML-ENTITIES', 'UTF-8'));

			$xpath = new \DOMXPath($dom);
			foreach ($xpath->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' tfhb-reschedule-btn ')]") as $node) {
				$node->parentNode->removeChild($node);
			}

			$template = $dom->saveHTML($dom->getElementsByTagName('body')->item(0));
			$template = preg_replace('/^<body>|<\/body>$/', '', $template);
		} elseif ($attendeeBooking->attendee_can_cancel != 1 && $attendeeBooking->attendee_can_reschedule == 1) {
			// Remove only cancel button
			libxml_use_internal_errors(true);
			$dom = new \DOMDocument();
			$dom->loadHTML(mb_convert_encoding($template, 'HTML-ENTITIES', 'UTF-8'));

			$xpath = new \DOMXPath($dom);
			foreach ($xpath->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' tfhb-cancel-btn ')]") as $node) {
				$node->parentNode->removeChild($node);
			}

			$template = $dom->saveHTML($dom->getElementsByTagName('body')->item(0));
			$template = preg_replace('/^<body>|<\/body>$/', '', $template);
		}

		if ( $did_switch && function_exists( 'restore_previous_locale' ) ) {
			restore_previous_locale();
		}

		return $template;

	}
}
