<?php
/**
 * TemplateHooks class file.
 *
 * @package RadiusTheme\RadiusBooking\Emails
 */

namespace RadiusTheme\RadiusBooking\Emails;

use RadiusTheme\RadiusBooking\Helpers\SettingsHelper;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}
/**
 * Class TemplateHooks
 *
 * Handles default hooks for email templates.
 *
 * @package RadiusTheme\RadiusBooking\Emails
 */
class TemplateHooks {

	/**
	 * Initialize all default hooks
	 */
	public static function init() {
		// Header hooks
		add_action( 'rtrb_email_header', array( __CLASS__, 'email_styles' ), 10 );
		add_action( 'rtrb_email_header_image', array( __CLASS__, 'email_header_image' ), 10 );
		add_action( 'rtrb_email_header_content', array( __CLASS__, 'email_header_content' ), 10 );

		// Content hooks
		add_action( 'rtrb_email_before_content', array( __CLASS__, 'email_heading' ), 10, 2 );
		add_action( 'rtrb_email_booking_details', array( __CLASS__, 'email_booking_details' ), 10, 2 );
		add_action( 'rtrb_email_button', array( __CLASS__, 'email_button' ), 10, 3 );
		add_action( 'rtrb_email_after_content', array( __CLASS__, 'email_booking_id' ), 10, 2 );

		// Footer hooks
		add_action( 'rtrb_email_footer_content', array( __CLASS__, 'email_footer_content' ), 10 );
	}

	/**
	 * Output email styles
	 */
	public static function email_styles() {
		$header_color = get_option( 'rtrb_email_header_color', '#2563eb' );
		$body_bg      = get_option( 'rtrb_email_body_bg', '#f3f4f6' );
		$body_color   = get_option( 'rtrb_email_body_color', '#374151' );
		?>
		<style type="text/css">
			body {
				margin: 0;
				padding: 0;
				font-family: Arial, Helvetica, sans-serif;
				background-color: <?php echo esc_attr( $body_bg ); ?>;
			}

			table {
				border-collapse: collapse;
				mso-table-lspace: 0pt;
				mso-table-rspace: 0pt;
			}

			img {
				border: 0;
				height: auto;
				line-height: 100%;
				outline: none;
				text-decoration: none;
			}

			#wrapper {
				background-color: <?php echo esc_attr( $body_bg ); ?>;
				width: 100%;
			}

			#template_container {
				background-color: #ffffff;
				border-radius: 8px;
				box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
			}

			#template_header {
				background: linear-gradient(to right, <?php echo esc_attr( $header_color ); ?>, #1d4ed8);
				background-color: <?php echo esc_attr( $header_color ); ?>;
				border-radius: 8px 8px 0 0;
				padding: 24px;
				text-align: center;
			}

			.header-logo-box {
				width: 64px;
				height: 64px;
				background-color: #ffffff;
				border-radius: 8px;
				text-align: center;
				vertical-align: middle;
			}

			.header-logo-text {
				color: <?php echo esc_attr( $header_color ); ?>;
				font-weight: bold;
				font-size: 20px;
				line-height: 64px;
			}

			.header-business-name {
				color: #ffffff;
				font-size: 14px;
				font-weight: 500;
				margin: 0;
				padding: 0;
			}

			#body_content {
				background-color: #ffffff;
			}

			#body_content_inner {
				color: <?php echo esc_attr( $body_color ); ?>;
				font-family: Arial, Helvetica, sans-serif;
				font-size: 14px;
				line-height: 1.6;
				padding: 24px;
			}

			.email-heading {
				font-size: 24px;
				font-weight: bold;
				color: <?php echo esc_attr( $header_color ); ?>;
				text-align: center;
				margin: 0 0 16px 0;
				padding: 0;
			}

			.main-content {
				color: <?php echo esc_attr( $body_color ); ?>;
				font-size: 14px;
				line-height: 1.6;
				margin: 0 0 24px 0;
			}

			.booking-details {
				background: linear-gradient(to bottom right, #eff6ff, #e0e7ff);
				background-color: #eff6ff;
				border: 1px solid #bfdbfe;
				border-radius: 8px;
				padding: 20px;
				margin: 24px 0;
			}

			.booking-details-heading {
				font-weight: 600;
				color: #111827;
				margin: 0;
				font-size: 16px;
				text-align: center;
				padding-bottom: 16px;
			}

			.booking-detail-row {
				border-bottom: 1px solid #bfdbfe;
				margin-bottom: 12px;
			}

			.booking-detail-row.last-row {
				border-bottom: none;
				padding-top: 8px;
				margin-bottom: 0;
			}

			.detail-label {
				color: #4b5563;
				font-weight: 500;
				font-size: 14px;
				padding-bottom: 8px;
			}

			.detail-value {
				color: #111827;
				font-size: 14px;
				padding-bottom: 8px;
			}

			.detail-value.service {
				font-weight: 600;
			}

			.detail-value.price {
				color: <?php echo esc_attr( $header_color ); ?>;
				font-weight: bold;
				font-size: 18px;
			}

			.footer-text {
				color: <?php echo esc_attr( $body_color ); ?>;
				font-size: 14px;
				text-align: center;
				margin: 0 0 24px 0;
			}

			.email-button {
				text-align: center;
				margin: 24px 0;
			}

			.email-button a {
				background-color: <?php echo esc_attr( $header_color ); ?>;
				color: #ffffff !important;
				padding: 12px 32px;
				text-decoration: none;
				border-radius: 8px;
				display: inline-block;
				font-weight: 500;
				font-size: 14px;
				box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
			}

			.booking-id-text {
				text-align: center;
				color: #6b7280;
				font-size: 12px;
				margin: 24px 0 8px 0;
			}

			#template_footer {
				background-color: #ffffff;
				border-top: 1px solid #e5e7eb;
				padding: 24px;
			}

			#credit {
				text-align: center;
				color: #6b7280;
				font-size: 12px;
			}

			#credit p {
				margin: 0 0 8px 0;
			}

			.copyright-text {
				font-size: 11px;
				color: #9ca3af;
				margin: 8px 0 0 0;
			}
		</style>
		<?php
	}

	/**
	 * Output header image/logo
	 */
	public static function email_header_image( $logo_url ) {
		if ( $logo_url ) {
			?>
			<table border="0" cellspacing="0" cellpadding="0" style="margin: 0 auto 12px;">
				<tr>
					<td>
						<img src="<?php echo esc_url( $logo_url ); ?>" alt="<?php echo esc_attr( get_bloginfo( 'name' ) ); ?>" width="64" height="64" style="display: block; border-radius: 8px;" />
					</td>
				</tr>
			</table>
			<?php
		}
	}

	/**
	 * Output header content
	 */
	public static function email_header_content() {
		$site_name    = get_bloginfo( 'name' );
		$header_color = get_option( 'rtrb_email_header_color', '#2563eb' );
		$business_settings = SettingsHelper::get_setting( 'business' );
		$logo_url = ! empty( $business_settings['logoUrl'] ) ? $business_settings['logoUrl'] : '';
		$site_name = ! empty( $business_settings['companyName'] ) ? $business_settings['companyName'] : $site_name;
		?>
		<table width="100%" border="0" cellspacing="0" cellpadding="0">
			<tr>
				<td align="center">
					<?php if ( ! $logo_url ) : ?>
					<!-- Logo Box -->
					<table border="0" cellspacing="0" cellpadding="0" style="margin: 0 auto 12px;">
						<tr>
							<td class="header-logo-box">
								<span class="header-logo-text">
									<?php echo esc_html( substr( $site_name, 0, 1 ) ); ?>
								</span>
							</td>
						</tr>
					</table>
					<?php else : ?>
						<?php self::email_header_image( $logo_url ); ?>
					<?php endif; ?>

					<!-- Business Name -->
					<h2 class="header-business-name">
						<?php echo esc_html( $site_name ); ?>
					</h2>
				</td>
			</tr>
		</table>
		<?php
	}

	/**
	 * Output email heading
	 *
	 * @param object $email  The email object.
	 * @param array  $booking The booking data.
	 */
	public static function email_heading( $email, $booking ) { //phpcs:ignore
		$settings = $email->get_settings();
		$heading  = isset( $settings['heading'] ) ? $settings['heading'] : '';

		if ( $heading ) {
			?>
			<table width="100%" border="0" cellspacing="0" cellpadding="0">
				<tr>
					<td align="center" style="padding-bottom: 16px;">
						<h1 class="email-heading"><?php echo esc_html( $heading ); ?></h1>
					</td>
				</tr>
			</table>
			<?php
		}
	}

	/**
	 * Output booking details table
	 *
	 * @param object $email   The email object.
	 * @param array  $booking The booking data.
	 */
	public static function email_booking_details( $email, $booking ) {
		?>
		<h3 class="booking-details-heading"><?php esc_html_e( 'Booking Details', 'radius-booking' ); ?></h3>

		<?php if ( isset( $booking['service_name'] ) ) : ?>
		<!-- Service Row -->
		<table width="100%" border="0" cellspacing="0" cellpadding="0" class="booking-detail-row">
			<tr>
				<td class="detail-label"><?php esc_html_e( 'Service:', 'radius-booking' ); ?></td>
				<td align="right" class="detail-value service"><?php echo esc_html( $booking['service_name'] ); ?></td>
			</tr>
		</table>
		<?php endif; ?>

		<?php if ( isset( $booking['appointment_date'] ) ) : ?>
		<!-- Date Row -->
		<table width="100%" border="0" cellspacing="0" cellpadding="0" class="booking-detail-row">
			<tr>
				<td class="detail-label"><?php esc_html_e( 'Date:', 'radius-booking' ); ?></td>
				<td align="right" class="detail-value"><?php echo esc_html( date_i18n( get_option( 'date_format' ), strtotime( $booking['appointment_date'] ) ) ); ?></td>
			</tr>
		</table>
		<?php endif; ?>

		<?php if ( isset( $booking['start_time'] ) ) : ?>
		<!-- Time Row -->
		<table width="100%" border="0" cellspacing="0" cellpadding="0" class="booking-detail-row">
			<tr>
				<td class="detail-label"><?php esc_html_e( 'Time:', 'radius-booking' ); ?></td>
				<td align="right" class="detail-value"><?php echo esc_html( date_i18n( get_option( 'time_format' ), strtotime( $booking['start_time'] ) ) ); ?></td>
			</tr>
		</table>
		<?php endif; ?>

		<?php if ( isset( $booking['customer']->duration ) ) : ?>
		<!-- Duration Row -->
		<table width="100%" border="0" cellspacing="0" cellpadding="0" class="booking-detail-row">
			<tr>
				<td class="detail-label"><?php esc_html_e( 'Duration:', 'radius-booking' ); ?></td>
				<td align="right" class="detail-value"><?php echo esc_html( $booking['customer']->duration / 60 . ' ' . __( 'minutes', 'radius-booking' ) ); ?></td>
			</tr>
		</table>
		<?php endif; ?>

		<?php if ( isset( $booking['employee_name'] ) ) : ?>
		<!-- Staff Row -->
		<table width="100%" border="0" cellspacing="0" cellpadding="0" class="booking-detail-row">
			<tr>
				<td class="detail-label"><?php esc_html_e( 'Employee:', 'radius-booking' ); ?></td>
				<td align="right" class="detail-value"><?php echo esc_html( $booking['employee_name'] ); ?></td>
			</tr>
		</table>
		<?php endif; ?>

		<?php if ( isset( $booking['location_info'] ) ) : ?>
		<!-- Location Row -->
		<table width="100%" border="0" cellspacing="0" cellpadding="0" class="booking-detail-row">
			<tr>
				<td class="detail-label"><?php esc_html_e( 'Location:', 'radius-booking' ); ?></td>
				<td align="right" class="detail-value"><?php echo esc_html( $booking['location_info']['address'] ); ?></td>
			</tr>
		</table>
		<?php endif; ?>

		<?php if ( isset( $booking['customer']->persons ) ) : ?>
		<table width="100%" border="0" cellspacing="0" cellpadding="0" class="booking-detail-row">
			<tr>
				<td class="detail-label"><?php esc_html_e( 'Persons:', 'radius-booking' ); ?></td>
				<td align="right" class="detail-value"><?php echo esc_html( $booking['customer']->persons + 1 ); ?></td>
			</tr>
		</table>
		<?php endif; ?>

		<?php if ( isset( $booking['customer']->price ) ) : ?>
		<!-- Service Price Row -->
		<table width="100%" border="0" cellspacing="0" cellpadding="0" class="booking-detail-row last-row">
			<tr>
				<td class="detail-label" ><?php esc_html_e( 'Service Price:', 'radius-booking' ); ?></td>
				<td align="right" class="detail-value"<?php echo ! empty( $booking['dynamic_pricing'] ) ? ' style="text-decoration: line-through; color: #9CA3AF;"' : ''; ?>><?php echo esc_html( get_rtrb_currency_symbol() . number_format( $booking['customer']->price, 2 ) ); ?></td>
			</tr>
		</table>
		<?php endif; ?>

		<?php if ( ! empty( $booking['dynamic_pricing'] ) && ! empty( $booking['dynamic_pricing']['applied_rules'] ) ) : ?>
		<!-- Dynamic Pricing Row -->
		<table width="100%" border="0" cellspacing="0" cellpadding="0" class="booking-detail-row">
			<tr>
				<td class="detail-label"><?php esc_html_e( 'Dynamic Price:', 'radius-booking' ); ?></td>
				<td align="right" class="detail-value" style="font-weight: bold;"><?php echo esc_html( get_rtrb_currency_symbol() . number_format( $booking['dynamic_pricing']['final_price'], 2 ) ); ?></td>
			</tr>
		</table>
		<?php foreach ( $booking['dynamic_pricing']['applied_rules'] as $rule ) : ?>
		<table width="100%" border="0" cellspacing="0" cellpadding="0" class="booking-detail-row">
			<tr>
				<td class="detail-label" style="color: #2563EB; font-size: 12px; padding-left: 10px;"><?php echo esc_html( $rule['name'] ); ?> (<?php echo 'percentage' === $rule['adjustment_type'] ? esc_html( $rule['adjustment_value'] . '%' ) : esc_html__( 'fixed', 'radius-booking' ); ?>)</td>
				<td align="right" class="detail-value" style="color: <?php echo $rule['calculated'] >= 0 ? '#DC2626' : '#059669'; ?>; font-size: 12px;"><?php echo $rule['calculated'] >= 0 ? '+' : '-'; ?><?php echo esc_html( get_rtrb_currency_symbol() . number_format( abs( $rule['calculated'] ), 2 ) ); ?></td>
			</tr>
		</table>
		<?php endforeach; ?>
		<?php endif; ?>

        <?php if ( isset( $booking['payment']->coupon_code ) && ! empty( $booking['payment']->coupon_code ) ) : ?>
            <!-- Coupon Discount Row -->
            <table width="100%" border="0" cellspacing="0" cellpadding="0" class="booking-detail-row">
                <tr>
                    <td class="detail-label"><?php esc_html_e( 'Coupon:', 'radius-booking' ); ?></td>
                    <td align="right" class="detail-value"><?php echo esc_html( $booking['payment']->coupon_code ); ?></td>
                </tr>
            </table>
            <table width="100%" border="0" cellspacing="0" cellpadding="0" class="booking-detail-row">
                <tr>
                    <td class="detail-label" style="color: #059669;"><?php esc_html_e( 'Discount:', 'radius-booking' ); ?></td>
                    <td align="right" class="detail-value" style="color: #059669;">-<?php echo esc_html( get_rtrb_currency_symbol() . number_format( (float) $booking['payment']->discount_amount, 2 ) ); ?></td>
                </tr>
            </table>
        <?php endif; ?>

		<?php if ( isset( $booking['payment']->tax_amount ) && $booking['payment']->tax_amount > 0 ) : ?>
			<?php
			$tax_settings = \RadiusTheme\RadiusBooking\Helpers\SettingsHelper::get_setting( 'tax' );
			$tax_label    = ! empty( $tax_settings['label'] ) ? $tax_settings['label'] : __( 'Tax', 'radius-booking' );
			?>
            <!-- Tax Row -->
            <table width="100%" border="0" cellspacing="0" cellpadding="0" class="booking-detail-row">
                <tr>
                    <td class="detail-label"><?php echo esc_html( $tax_label ); ?> (<?php echo esc_html( $booking['payment']->tax_rate ); ?>%)<?php echo ! empty( $tax_settings['inclusive'] ) ? ' (incl.)' : ' (excl.)'; ?>:</td>
                    <td align="right" class="detail-value">+<?php echo esc_html( get_rtrb_currency_symbol() . number_format( (float) $booking['payment']->tax_amount, 2 ) ); ?></td>
                </tr>
            </table>
		<?php endif; ?>

        <?php if ( isset( $booking['payment']->total_amount ) ) : ?>
            <!-- Total Price Row -->
            <table width="100%" border="0" cellspacing="0" cellpadding="0" class="booking-detail-row last-row">
                <tr>
                    <td class="detail-label" style="font-weight: bold; color: #111827;"><?php esc_html_e( 'Total Price:', 'radius-booking' ); ?></td>
                    <td align="right" class="detail-value price"><?php echo esc_html( get_rtrb_currency_symbol() . number_format( $booking['payment']->total_amount, 2 ) ); ?></td>
                </tr>
            </table>
        <?php endif; ?>
		<?php
	}

	/**
	 * Output action button
	 *
	 * @param object $email    The email object.
	 * @param array  $booking   The booking data.
	 * @param array  $settings  The email settings.
	 */
	public static function email_button( $email, $booking, $settings ) {
		$button_text = isset( $settings['button_text'] ) ? $settings['button_text'] : '';
		$button_url  = isset( $settings['button_url'] ) ? $settings['button_url'] : '';

		if ( $button_text && $button_url ) {
			// Replace merge tags in URL
			$booking_id = isset( $booking['id'] ) ? $booking['id'] : ( isset( $booking['booking_id'] ) ? $booking['booking_id'] : '' );
			$button_url = str_replace( '{booking_id}', $booking_id, $button_url );
			?>
			<table width="100%" border="0" cellspacing="0" cellpadding="0">
				<tr>
					<td align="center" style="padding-bottom: 24px;">
						<table border="0" cellspacing="0" cellpadding="0">
							<tr>
								<td style="background-color: #2563eb; border-radius: 8px; box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);">
									<a href="<?php echo esc_url( $button_url ); ?>" style="display: inline-block; color: #ffffff; padding: 12px 32px; font-weight: 500; text-decoration: none; font-size: 14px;">
										<?php echo esc_html( $button_text ); ?>
									</a>
								</td>
							</tr>
						</table>
					</td>
				</tr>
			</table>
			<?php
		}
	}

	/**
	 * Output booking ID
	 *
	 * @param object $email  The email object.
	 * @param array  $booking The booking data.
	 */
	public static function email_booking_id( $email, $booking ) {
		$booking_id = isset( $booking['id'] ) ? $booking['id'] : ( isset( $booking['booking_id'] ) ? $booking['booking_id'] : '' );

		if ( $booking_id ) {
			?>
			<table width="100%" border="0" cellspacing="0" cellpadding="0">
				<tr>
					<td class="booking-id-text">
						<?php
						printf(
						/* translators: %s: admin email address. */
							esc_html__( 'Booking ID: %s', 'radius-booking' ),
							'<strong>' . esc_html( $booking_id ) . '</strong>'
						);
						?>
					</td>
				</tr>
			</table>
			<?php
		}
	}

	/**
	 * Output footer content
	 */
	public static function email_footer_content() {
		$footer_text   = get_option( 'rtrb_email_footer_text', '' );
		$contact_phone = get_option( 'rtrb_email_contact_phone', '' );

		$business_settings = SettingsHelper::get_setting( 'business' );
		if ( isset( $business_settings['phone'] ) && ! empty( $business_settings['phone'] ) ) {
			$contact_phone = $business_settings['phone'];
		}
		$site_name = ! empty( $business_settings['companyName'] ) ? $business_settings['companyName'] : get_bloginfo( 'name' );
		?>
		<p style="margin: 0 0 8px 0;">
			<?php
			if ( $footer_text ) {
				echo wp_kses_post( $footer_text );
			} elseif ( $contact_phone ) {
				/* translators: %s: admin email address. */
					printf(
					/* translators: %s: admin email address. */
						esc_html__( 'Questions? Reply to this email or call us at %s', 'radius-booking' ),
						esc_html( $contact_phone )
					);
			} else {
				/* translators: %s: admin email address. */
				printf(
				/* translators: %s: admin email address. */
					esc_html__( 'Questions? Reply to this email or contact us at %s', 'radius-booking' ),
					esc_html( get_option( 'admin_email' ) )
				);
			}
			?>
		</p>
		<p class="copyright-text">
			<?php
			printf(
			/* translators: %1$s: current year. %2$s: site name. */
				esc_html__( '© %1$s %2$s. All rights reserved.', 'radius-booking' ),
				esc_html( date_i18n( 'Y' ) ),
				esc_html( $site_name )
			);
			?>
		</p>
		<?php
	}
}