<?php
/**
 * Order Care Page.
 *
 * @package Noesia_Commerce_Suite
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- View templates receive local variables from their renderer.
$settings        = isset( $settings ) && is_array( $settings ) ? $settings : Noesia_CS_Settings::get_all();
$hub             = isset( $hub ) && is_array( $hub ) ? $hub : array();
$items           = isset( $hub['items'] ) && is_array( $hub['items'] ) ? $hub['items'] : array();
$smart_links     = isset( $hub['smart_links'] ) && is_array( $hub['smart_links'] ) ? $hub['smart_links'] : array();
$dpp_items       = apply_filters( 'noesia_cs_order_care_dpp_items', array(), $order, $settings );
$dpp_items       = is_array( $dpp_items ) ? $dpp_items : array();
$dpp_note        = apply_filters( 'noesia_cs_order_care_dpp_note', __( 'This information is provided by the merchant as a product transparency readiness layer. It is not an official legal certification.', 'noesia-commerce-suite' ), $settings, $dpp_items );
$dpp_note        = is_string( $dpp_note ) ? trim( $dpp_note ) : '';
$privacy_note    = apply_filters( 'noesia_cs_order_care_privacy_note', __( 'Secure page. Customer email, phone, address and payment details are never shown here.', 'noesia-commerce-suite' ), $settings, $order );
$privacy_note    = is_string( $privacy_note ) ? trim( $privacy_note ) : '';
$public_style    = in_array( $settings['order_care_public_style'] ?? 'clean', array( 'clean', 'compact', 'premium', 'enterprise' ), true ) ? $settings['order_care_public_style'] : 'clean';
$button_style    = in_array( $settings['order_care_button_style'] ?? 'solid', array( 'solid', 'soft', 'outline' ), true ) ? $settings['order_care_button_style'] : 'solid';
$global_button_style = in_array( $settings['button_style'] ?? 'rounded', array( 'rounded', 'pill', 'minimal' ), true ) ? $settings['button_style'] : 'rounded';
$content_width   = in_array( $settings['order_care_content_width'] ?? 'medium', array( 'small', 'medium', 'full' ), true ) ? $settings['order_care_content_width'] : 'medium';
$content_width_map = array(
	'small'  => 'min(980px, calc(100vw - 32px))',
	'medium' => 'min(1240px, calc(100vw - 48px))',
	'full'   => 'min(1520px, calc(100vw - 64px))',
);
$content_max_width = $content_width_map[ $content_width ] ?? $content_width_map['medium'];
$style             = isset( $style ) ? (string) $style : '';
$style             = rtrim( $style, ';' ) . ';--noesia-cs-order-care-hub-max-width:' . $content_max_width . ';';
$order_number    = $order && is_a( $order, 'WC_Order' ) ? $order->get_order_number() : '';
$product_image   = ! empty( $hub['product_image'] ) ? $hub['product_image'] : '';
$product_name    = ! empty( $hub['product_name'] ) ? $hub['product_name'] : __( 'Purchased item', 'noesia-commerce-suite' );
$item_count      = isset( $hub['item_count'] ) ? absint( $hub['item_count'] ) : count( $items );
$review_action   = isset( $hub['review'] ) && is_array( $hub['review'] ) ? $hub['review'] : array();
$review_actions  = isset( $hub['review_actions'] ) && is_array( $hub['review_actions'] ) ? $hub['review_actions'] : array();
$support_action  = isset( $hub['support'] ) && is_array( $hub['support'] ) ? $hub['support'] : array();
$show_review     = ! empty( $hub['show_review'] ) && ! empty( $review_actions );
$show_support    = ! empty( $hub['show_support'] ) && ! empty( $support_action['url'] );
$single_reorder = ( 1 === $item_count && ! empty( $items[0]['reorder_url'] ) && 'yes' === ( $settings['show_reorder_button'] ?? 'yes' ) );
$multi_reorder  = ( $item_count > 1 && 'yes' === ( $settings['show_reorder_button'] ?? 'yes' ) );
$hub_title       = ! empty( $settings['care_page_title_text'] ) ? $settings['care_page_title_text'] : __( 'Order Care Hub', 'noesia-commerce-suite' );
$hub_intro       = ! empty( $settings['care_page_intro_text'] ) ? $settings['care_page_intro_text'] : __( 'Your secure post-purchase space for reviews, reorders and support.', 'noesia-commerce-suite' );
?>
<!doctype html>
<html <?php language_attributes(); ?>>
<head>
	<meta charset="<?php bloginfo( 'charset' ); ?>">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<?php if ( 'yes' === ( $settings['privacy_noindex'] ?? 'yes' ) ) : ?><meta name="robots" content="noindex,nofollow"><?php endif; ?>
	<?php wp_head(); ?>
</head>
<body <?php body_class( 'noesia-cs-care-body noesia-cs-order-care-hub-body noesia-cs-order-care-hub--' . $public_style . ' noesia-cs-order-care-hub-width--' . $content_width . ' noesia-cs-order-care-hub-buttons--' . $button_style . ' noesia-cs-button-style--' . $global_button_style ); ?> style="<?php echo esc_attr( $style ); ?>">
	<div class="noesia-cs-order-care-hub-wrap">
		<main class="noesia-cs-order-care-hub-shell" aria-label="<?php esc_attr_e( 'Order Care Hub', 'noesia-commerce-suite' ); ?>">
			<section class="noesia-cs-order-care-hub-hero">
				<div class="noesia-cs-order-care-hub-hero-copy">
					<div class="noesia-cs-order-care-hub-brand">
						<?php if ( ! empty( $logo ) ) : ?>
							<img src="<?php echo esc_url( $logo ); ?>" alt="">
						<?php else : ?>
							<span class="noesia-cs-order-care-hub-mark">N</span>
						<?php endif; ?>
						<div>
							<span class="noesia-cs-order-care-hub-eyebrow"><?php esc_html_e( 'Secure after-sales hub', 'noesia-commerce-suite' ); ?></span>
							<h1><?php echo esc_html( $hub_title ); ?></h1>
							<p><?php echo esc_html( $hub_intro ); ?></p>
						</div>
					</div>
					<div class="noesia-cs-order-care-hub-badges">
						<?php if ( 'yes' === ( $settings['show_order_number'] ?? 'yes' ) && $order_number ) : ?>
							<strong><?php printf( /* translators: %s: dynamic value. */ esc_html__( 'Order #%s', 'noesia-commerce-suite' ), esc_html( $order_number ) ); ?></strong>
						<?php endif; ?>
						<?php if ( 'yes' === ( $settings['show_verified_badge'] ?? 'yes' ) ) : ?>
							<span><?php echo 'yes' === ( $settings['show_verified_purchase_text'] ?? 'yes' ) ? esc_html( $settings['verified_purchase_text'] ?? __( 'Verified Purchase', 'noesia-commerce-suite' ) ) : esc_html__( 'Verified Purchase', 'noesia-commerce-suite' ); ?></span>
						<?php endif; ?>
					</div>
				</div>

				<section class="noesia-cs-order-care-hub-product" aria-label="<?php esc_attr_e( 'Purchased item summary', 'noesia-commerce-suite' ); ?>">
					<div class="noesia-cs-order-care-hub-product-image">
						<?php if ( $product_image ) : ?>
							<img src="<?php echo esc_url( $product_image ); ?>" alt="">
						<?php else : ?>
							<span>▣</span>
						<?php endif; ?>
					</div>
					<div class="noesia-cs-order-care-hub-product-meta">
						<span><?php esc_html_e( 'Purchased item', 'noesia-commerce-suite' ); ?></span>
						<strong><?php echo 'yes' === ( $settings['show_product_name'] ?? 'yes' ) ? esc_html( $product_name ) : esc_html__( 'Product details are protected', 'noesia-commerce-suite' ); ?></strong>
						<?php if ( $item_count > 1 ) : ?>
							<small><?php printf( /* translators: %d: numeric value. */ esc_html__( '%d items in this order', 'noesia-commerce-suite' ), absint( $item_count ) ); ?></small>
						<?php elseif ( ! empty( $items[0]['quantity'] ) ) : ?>
							<small><?php printf( /* translators: %d: numeric value. */ esc_html__( 'Quantity: %d', 'noesia-commerce-suite' ), absint( $items[0]['quantity'] ) ); ?></small>
						<?php endif; ?>
					</div>
				</section>
			</section>

			<section class="noesia-cs-order-care-hub-panel noesia-cs-order-care-hub-primary-panel">
				<div class="noesia-cs-order-care-hub-section-head">
					<div>
						<strong><?php esc_html_e( 'What would you like to do next?', 'noesia-commerce-suite' ); ?></strong>
						<span><?php esc_html_e( 'Choose one clear action for this purchase.', 'noesia-commerce-suite' ); ?></span>
					</div>
				</div>
				<div class="noesia-cs-order-care-hub-actions" aria-label="<?php esc_attr_e( 'Order care actions', 'noesia-commerce-suite' ); ?>">
					<?php if ( $show_review ) : ?>
						<div class="noesia-cs-order-care-hub-action noesia-cs-order-care-hub-action--review noesia-cs-order-care-hub-action--static">
							<span class="noesia-cs-order-care-hub-action-icon">★</span>
							<span class="noesia-cs-order-care-hub-action-copy">
								<strong><?php echo esc_html( $settings['review_button_label'] ?? __( 'Leave a Review', 'noesia-commerce-suite' ) ); ?></strong>
								<small><?php echo ! empty( $settings['review_intro_text'] ) ? esc_html( $settings['review_intro_text'] ) : esc_html__( 'Choose where you want to rate this purchase.', 'noesia-commerce-suite' ); ?></small>
							</span>
							<div class="noesia-cs-order-care-hub-review-links" aria-label="<?php esc_attr_e( 'Review destinations', 'noesia-commerce-suite' ); ?>">
								<?php foreach ( $review_actions as $review_link ) : ?>
									<a href="<?php echo esc_url( $review_link['url'] ); ?>" target="_blank" rel="noopener noreferrer nofollow"><?php echo esc_html( $review_link['label'] ); ?></a>
								<?php endforeach; ?>
							</div>
						</div>
					<?php endif; ?>

					<?php if ( $single_reorder ) : ?>
						<a class="noesia-cs-order-care-hub-action noesia-cs-order-care-hub-action--reorder" href="<?php echo esc_url( $items[0]['reorder_url'] ); ?>" rel="nofollow">
							<span class="noesia-cs-order-care-hub-action-icon">↻</span>
							<span class="noesia-cs-order-care-hub-action-copy">
								<strong><?php esc_html_e( 'Order Again', 'noesia-commerce-suite' ); ?></strong>
								<small><?php esc_html_e( 'Buy this item again without searching for it.', 'noesia-commerce-suite' ); ?></small>
							</span>
						</a>
					<?php elseif ( $multi_reorder ) : ?>
						<div class="noesia-cs-order-care-hub-action noesia-cs-order-care-hub-action--reorder noesia-cs-order-care-hub-action--static">
							<span class="noesia-cs-order-care-hub-action-icon">↻</span>
							<span class="noesia-cs-order-care-hub-action-copy">
								<strong><?php esc_html_e( 'Order items again', 'noesia-commerce-suite' ); ?></strong>
								<small><?php esc_html_e( 'Choose exactly which purchased item you want to reorder.', 'noesia-commerce-suite' ); ?></small>
							</span>
						</div>
					<?php endif; ?>

					<?php if ( $show_support ) : ?>
						<a class="noesia-cs-order-care-hub-action noesia-cs-order-care-hub-action--support" href="<?php echo esc_url( $support_action['url'] ); ?>" <?php if ( false === strpos( $support_action['url'], 'mailto:' ) ) : ?>target="_blank" rel="noopener noreferrer nofollow"<?php endif; ?>>
							<span class="noesia-cs-order-care-hub-action-icon">?</span>
							<span class="noesia-cs-order-care-hub-action-copy">
								<strong><?php echo esc_html( $settings['support_button_label'] ?? __( 'Need Help?', 'noesia-commerce-suite' ) ); ?></strong>
								<small><?php echo ! empty( $settings['support_intro_text'] ) ? esc_html( $settings['support_intro_text'] ) : esc_html__( 'Contact the store team about this order.', 'noesia-commerce-suite' ); ?></small>
							</span>
						</a>
					<?php endif; ?>
				</div>
			</section>

			<?php if ( $multi_reorder ) : ?>
				<section class="noesia-cs-order-care-hub-panel noesia-cs-order-care-hub-items" aria-label="<?php esc_attr_e( 'Reorder purchased items', 'noesia-commerce-suite' ); ?>">
					<div class="noesia-cs-order-care-hub-section-head">
						<div>
							<strong><?php esc_html_e( 'Reorder purchased items', 'noesia-commerce-suite' ); ?></strong>
							<span><?php esc_html_e( 'Each item has its own reorder action.', 'noesia-commerce-suite' ); ?></span>
						</div>
					</div>
					<div class="noesia-cs-order-care-hub-item-list">
						<?php foreach ( $items as $item ) : ?>
							<div class="noesia-cs-order-care-hub-item-row">
								<div class="noesia-cs-order-care-hub-item-thumb">
									<?php if ( ! empty( $item['image'] ) ) : ?><img src="<?php echo esc_url( $item['image'] ); ?>" alt=""><?php else : ?><span>▣</span><?php endif; ?>
								</div>
								<div>
									<strong><?php echo esc_html( $item['name'] ); ?></strong>
									<small><?php printf( /* translators: %d: numeric value. */ esc_html__( 'Quantity: %d', 'noesia-commerce-suite' ), absint( $item['quantity'] ) ); ?></small>
								</div>
								<?php if ( ! empty( $item['reorder_url'] ) ) : ?>
									<a href="<?php echo esc_url( $item['reorder_url'] ); ?>" rel="nofollow"><?php esc_html_e( 'Reorder', 'noesia-commerce-suite' ); ?></a>
								<?php endif; ?>
							</div>
						<?php endforeach; ?>
					</div>
				</section>
			<?php endif; ?>

			<?php if ( ! empty( $smart_links ) ) : ?>
				<section class="noesia-cs-order-care-hub-panel noesia-cs-order-care-hub-smart-links" aria-label="<?php esc_attr_e( 'Helpful links', 'noesia-commerce-suite' ); ?>">
					<div class="noesia-cs-order-care-hub-section-head">
						<div>
							<strong><?php esc_html_e( 'Helpful resources', 'noesia-commerce-suite' ); ?></strong>
							<span><?php esc_html_e( 'Store links configured by the merchant for this after-sales experience.', 'noesia-commerce-suite' ); ?></span>
						</div>
					</div>
					<div class="noesia-cs-order-care-hub-smart-grid">
						<?php foreach ( $smart_links as $link ) : ?>
							<?php $is_promo = 'promo' === ( $link['key'] ?? '' ); ?>
							<div class="noesia-cs-order-care-hub-smart-card noesia-cs-order-care-hub-smart-card--<?php echo esc_attr( $link['key'] ?? 'link' ); ?>">
								<span class="noesia-cs-order-care-hub-smart-icon"><?php echo esc_html( $link['icon'] ?? '•' ); ?></span>
								<div class="noesia-cs-order-care-hub-smart-copy">
									<strong><?php echo esc_html( $link['title'] ?? '' ); ?></strong>
									<?php if ( ! empty( $link['text'] ) ) : ?><p><?php echo esc_html( $link['text'] ); ?></p><?php endif; ?>
									<?php if ( ! empty( $link['code'] ) ) : ?>
										<code class="noesia-cs-order-care-hub-promo-code" role="button" tabindex="0" data-noesia-cs-copy-code="<?php echo esc_attr( $link['code'] ); ?>" data-noesia-cs-copy-original="<?php echo esc_attr( $link['code'] ); ?>" data-noesia-cs-copy-done="<?php esc_attr_e( 'Copied!', 'noesia-commerce-suite' ); ?>" aria-label="<?php esc_attr_e( 'Copy promo code', 'noesia-commerce-suite' ); ?>"><?php echo esc_html( $link['code'] ); ?></code>
									<?php endif; ?>
								</div>
								<?php if ( ! empty( $link['url'] ) ) : ?>
									<a href="<?php echo esc_url( $link['url'] ); ?>" target="_blank" rel="noopener noreferrer nofollow"><?php echo esc_html( $link['button'] ?? __( 'Open', 'noesia-commerce-suite' ) ); ?></a>
								<?php elseif ( $is_promo && ! empty( $link['code'] ) ) : ?>
									<span class="noesia-cs-order-care-hub-code-note"><?php esc_html_e( 'Use this code on your next order.', 'noesia-commerce-suite' ); ?></span>
								<?php endif; ?>
							</div>
						<?php endforeach; ?>
					</div>
				</section>
			<?php endif; ?>


			<?php if ( ! empty( $dpp_items ) ) : ?>
				<section class="noesia-cs-order-care-hub-panel noesia-cs-order-care-hub-dpp" aria-label="<?php esc_attr_e( 'Digital Product Passport information', 'noesia-commerce-suite' ); ?>">
					<div class="noesia-cs-order-care-hub-section-head">
						<div>
							<strong><?php esc_html_e( 'Digital Product Passport Information', 'noesia-commerce-suite' ); ?></strong>
							<span><?php esc_html_e( 'Merchant-managed product transparency information for this purchase.', 'noesia-commerce-suite' ); ?></span>
						</div>
					</div>

					<div class="noesia-cs-order-care-hub-dpp-items">
						<?php foreach ( $dpp_items as $dpp_item ) : ?>
							<?php $dpp_boxes = isset( $dpp_item['boxes'] ) && is_array( $dpp_item['boxes'] ) ? $dpp_item['boxes'] : array(); ?>
							<?php if ( empty( $dpp_boxes ) ) : ?>
								<?php continue; ?>
							<?php endif; ?>
							<article class="noesia-cs-order-care-hub-dpp-item">
								<header class="noesia-cs-order-care-hub-dpp-item-head">
									<span><?php esc_html_e( 'Product passport', 'noesia-commerce-suite' ); ?></span>
									<strong><?php echo esc_html( $dpp_item['name'] ?? __( 'Purchased item', 'noesia-commerce-suite' ) ); ?></strong>
								</header>
								<div class="noesia-cs-order-care-hub-dpp-grid noesia-cs-order-care-hub-smart-grid">
								<?php foreach ( $dpp_boxes as $box ) : ?>
									<article class="noesia-cs-order-care-hub-smart-card noesia-cs-order-care-hub-dpp-card noesia-cs-order-care-hub-dpp-card--<?php echo esc_attr( sanitize_html_class( $box['key'] ?? 'info' ) ); ?>">
										<span class="noesia-cs-order-care-hub-smart-icon noesia-cs-order-care-hub-dpp-icon"><?php echo esc_html( $box['icon'] ?? '•' ); ?></span>
										<span class="noesia-cs-order-care-hub-smart-copy noesia-cs-order-care-hub-dpp-copy">
											<strong><?php echo esc_html( $box['title'] ?? '' ); ?></strong>
											<?php if ( ! empty( $box['text'] ) ) : ?><p><?php echo nl2br( esc_html( (string) $box['text'] ) ); ?></p><?php endif; ?>
											<?php if ( ! empty( $box['details'] ) && is_array( $box['details'] ) ) : ?>
												<ul class="noesia-cs-order-care-hub-dpp-details">
													<?php foreach ( $box['details'] as $detail ) : ?>
														<?php if ( empty( $detail['value'] ) ) : ?><?php continue; ?><?php endif; ?>
														<li><span><?php echo esc_html( $detail['label'] ?? '' ); ?></span><em><?php echo nl2br( esc_html( (string) $detail['value'] ) ); ?></em></li>
													<?php endforeach; ?>
												</ul>
											<?php endif; ?>
											<?php if ( ! empty( $box['links'] ) && is_array( $box['links'] ) ) : ?>
												<span class="noesia-cs-order-care-hub-dpp-links">
													<?php foreach ( $box['links'] as $link ) : ?>
														<?php if ( empty( $link['url'] ) ) : ?><?php continue; ?><?php endif; ?>
														<a href="<?php echo esc_url( $link['url'] ); ?>" target="_blank" rel="noopener noreferrer nofollow"><?php echo esc_html( $link['label'] ?? __( 'Open', 'noesia-commerce-suite' ) ); ?></a>
													<?php endforeach; ?>
												</span>
											<?php endif; ?>
										</span>
									</article>
								<?php endforeach; ?>
							</div>
							</article>
						<?php endforeach; ?>
					</div>
					<?php if ( '' !== $dpp_note ) : ?><p class="noesia-cs-order-care-hub-dpp-note"><?php echo esc_html( $dpp_note ); ?></p><?php endif; ?>
				</section>
			<?php endif; ?>

			<?php if ( '' !== $privacy_note ) : ?>
				<footer class="noesia-cs-order-care-hub-footer">
					<?php echo esc_html( $privacy_note ); ?>
				</footer>
			<?php endif; ?>
		</main>
	</div>
	<?php wp_footer(); ?>
</body>
</html>
