<?php

/**
 * Review landing page form renderer.
 *
 * @package ProofWP
 */

declare(strict_types=1);

defined('ABSPATH') || exit;

/**
 * Render the review form.
 */
function dsproofwp_render_review_form(WC_Order $order, array $order_items, array $request, array $page_data): void
{
	require_once __DIR__ . '/one-page-review-form-handler.php';
	$settings = dsproofwp_get_form_settings();

	$customer_name = $order->get_billing_first_name() ?: $order->get_billing_full_name();
	$customer_email = $order->get_billing_email();
	$site_name = get_bloginfo('name');
	$order_number = $order->get_order_number();
	$order_date = $order->get_date_created();
	$order_date = $order_date ? wc_format_datetime($order_date) : '';
	$invoice_number = $order->get_meta('_invoice_number', true);
	$errors = $page_data['errors'] ?? [];
	$rating_values = $page_data['posted_rating'] ?? [];
	$review_texts = $page_data['posted_review'] ?? [];
?>
	<section class="dsproofwp-review-page">
		<div class="dsproofwp-review-intro">
			<h1><?php esc_html_e('Share your Opinion', 'proofwp'); ?></h1>
			<p><?php esc_html_e('Share your experience with this product.', 'proofwp'); ?></p>
		</div>



		<?php if (!empty($errors)): ?>
			<div class="dsproofwp-error-banner">
				<?php foreach ($errors as $error): ?>
					<div><?php echo esc_html($error); ?></div>
				<?php endforeach; ?>
			</div>
		<?php endif; ?>

		<form method="POST" enctype="multipart/form-data" class="dsproofwp-review-form">
			<div class="dsproofwp-customer-meta">
				<div class="dsproofwp-field"><label><?php esc_html_e('Name', 'proofwp'); ?></label><input type="text" value="<?php echo esc_attr($customer_name); ?>" disabled></div>
				<div class="dsproofwp-field"><label><?php esc_html_e('Email', 'proofwp'); ?></label><input type="email" value="<?php echo esc_attr($customer_email); ?>" disabled></div>
			</div>

			<div class="dsproofwp-products-list">
				<?php foreach ($order_items as $item): ?>
					<?php $product = $item->get_product();
					if (!$product) {
						continue;
					} ?>
					<div class="dsproofwp-product-card">
						<div class="dsproofwp-product-top">
							<div class="dsproofwp-product-image">
								<?php $image_url = $product->get_image_id() ? wp_get_attachment_image_url($product->get_image_id(), 'thumbnail') : wc_placeholder_img_src(); ?>
								<img src="<?php echo esc_url($image_url); ?>" alt="<?php echo esc_attr($product->get_name()); ?>">
							</div>
							<div class="dsproofwp-product-title">
								<h2><?php echo esc_html($product->get_name()); ?></h2>
								<?php if (!empty($invoice_number)): ?>
									<div class="dsproofwp-product-order-meta"><strong><?php esc_html_e('Invoice Number:', 'proofwp'); ?></strong> <?php echo esc_html($invoice_number); ?></div>
								<?php endif; ?>
								<div class="dsproofwp-product-order-meta"><?php esc_html_e('Purchase:', 'proofwp'); ?> <?php echo esc_html($order_date); ?></div>
							</div>
						</div>

						<div class="dsproofwp-product-bottom">
							<?php $current_rating = isset($rating_values[$product->get_id()]) ? (int) $rating_values[$product->get_id()] : 5; ?>
							<div class="dsproofwp-rating-row">
								<label>
									<?php esc_html_e('How would your rate us?', 'proofwp'); ?> <span class="dsproofwp-require-mark">*</span>
								</label>
								<div class="dsproofwp-stars" data-product-id="<?php echo esc_attr((string) $product->get_id()); ?>">
									<?php for ($i = 1; $i <= 5; $i++): ?>
										<span class="dsproofwp-star <?php echo ($current_rating >= $i) ? 'active' : ''; ?>" data-value="<?php echo esc_attr((string) $i); ?>">★</span>
									<?php endfor; ?>
								</div>
								<input type="hidden" name="rating[<?php echo esc_attr((string) $product->get_id()); ?>]" class="dsproofwp-rating-value" value="<?php echo esc_attr((string) $current_rating); ?>">
							</div>

							<div class="dsproofwp-review-text">
								<label for="review_text[<?php echo esc_attr((string) $product->get_id()); ?>]"><?php esc_html_e('Share Your product experience', 'proofwp'); ?></label>
								<?php
								$review_text = $review_texts[$product->get_id()] ?? '';
								$char_count  = mb_strlen($review_text);
								?>
								<textarea id="review_text[<?php echo esc_attr((string) $product->get_id()); ?>]" name="review_text[<?php echo esc_attr((string) $product->get_id()); ?>]" placeholder="<?php esc_attr_e('Summarize your experience...', 'proofwp'); ?>" maxlength="1000"><?php echo esc_textarea($review_text); ?></textarea>
								<span class="dsproofwp-char-counter">Character count: <?php echo esc_html((string) $char_count); ?>/1000</span>
							</div>

							<?php if ($settings['image_upload_enabled'] || $settings['video_upload_enabled']): ?>
								<div class="dsproofwp-file-upload">
									<label for="media-<?php echo esc_attr((string) $product->get_id()); ?>">
										<?php
										$accept_types = [];
										if ($settings['image_upload_enabled']) {
											$accept_types[] = 'image/*';
										}
										if ($settings['video_upload_enabled']) {
											$accept_types[] = 'video/*';
										}
										$accept_value = implode(',', $accept_types);
										?>
										<input type="file" id="media-<?php echo esc_attr((string) $product->get_id()); ?>" name="media[<?php echo esc_attr((string) $product->get_id()); ?>][]" multiple accept="<?php echo esc_attr($accept_value); ?>" class="dsproofwp-file-field">
										<span>
											<svg width="32" height="32" viewBox="0 0 32 32" fill="none" xmlns="http://www.w3.org/2000/svg">
												<path d="M16.9309 4.66666H15.0717C14.1189 4.66666 13.6425 4.66666 13.2153 4.81599C12.9544 4.90718 12.7092 5.03838 12.4886 5.20488C12.1274 5.47754 11.8631 5.87391 11.3346 6.66666C11.064 7.07268 10.665 7.67115 10.506 7.83827C10.1119 8.25235 9.59524 8.52887 9.03212 8.62704C8.80485 8.66666 8.56086 8.66666 8.07289 8.66666C6.76612 8.66666 6.11274 8.66666 5.57849 8.81788C4.24205 9.1962 3.19752 10.2407 2.8192 11.5772C2.66797 12.1114 2.66797 12.7648 2.66797 14.0716V19.3333C2.66797 23.1045 2.66797 24.9901 3.83954 26.1617C5.01112 27.3333 6.89674 27.3333 10.668 27.3333H21.3347C25.1059 27.3333 26.9915 27.3333 28.1631 26.1617C29.3347 24.9901 29.3347 23.1045 29.3347 19.3333V14.0716C29.3347 12.7648 29.3347 12.1114 29.1835 11.5772C28.8051 10.2407 27.7605 9.1962 26.4241 8.81788C25.8899 8.66666 25.2365 8.66666 23.9297 8.66666C23.4417 8.66666 23.1977 8.66666 22.9705 8.62704C22.4073 8.52887 21.8907 8.25235 21.4967 7.83827C21.3376 7.67114 20.9387 7.07267 20.668 6.66666C20.1395 5.87391 19.8752 5.47754 19.514 5.20488C19.2933 5.03838 19.0483 4.90718 18.7873 4.81599C18.3601 4.66666 17.8837 4.66666 16.9309 4.66666Z" fill="#54576A" />
												<path d="M21.3347 17.3333C21.3347 20.2788 18.9468 22.6667 16.0013 22.6667C13.0558 22.6667 10.668 20.2788 10.668 17.3333C10.668 14.3879 13.0558 12 16.0013 12C18.9468 12 21.3347 14.3879 21.3347 17.3333Z" stroke="white" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" />
												<path d="M25.5 12.6667H25.3333M25.6667 12.6667C25.6667 12.8508 25.5175 13 25.3333 13C25.1492 13 25 12.8508 25 12.6667C25 12.4826 25.1492 12.3333 25.3333 12.3333C25.5175 12.3333 25.6667 12.4826 25.6667 12.6667Z" stroke="white" stroke-width="1.5" stroke-linecap="round" />
											</svg>
											<span class="dsproofwp-media-label"><?php esc_html_e('Upload image or video', 'proofwp'); ?></span>
											<span>(optional)</span>
										</span>
										<?php
										$media_desc = '';
										if ($settings['image_upload_enabled'] && $settings['video_upload_enabled']) {
											$media_desc = sprintf(
												/* translators: 1: Image max size, 2: Video max size */
												esc_html__('Up to %1$d Mb photos, or %2$d Mb Video', 'proofwp'),
												$settings['image_max_size_mb'],
												$settings['video_max_size_mb']
											);
										} elseif ($settings['image_upload_enabled']) {
											$media_desc = sprintf(
												/* translators: %d: Image max size */
												esc_html__('Up to %d Mb photos', 'proofwp'),
												$settings['image_max_size_mb']
											);
										} elseif ($settings['video_upload_enabled']) {
											$media_desc = sprintf(
												/* translators: %d: Video max size */
												esc_html__('Up to %d Mb Video', 'proofwp'),
												$settings['video_max_size_mb']
											);
										}
										?>
										<span><?php echo esc_html($media_desc); ?></span>
									</label>
									<div class="dsproofwp-file-preview" data-product-id="<?php echo esc_attr((string) $product->get_id()); ?>"></div>
								</div>
							<?php endif; ?>
						</div>
					</div>
				<?php endforeach; ?>
			</div>


			<?php if ($settings['consent_enabled']): ?>
				<div class="dsproofwp-consent-row">
					<label class="dsproofwp-consent-label">
						<input type="checkbox" name="proofwp_consent" value="1" required>
						<span><?php echo wp_kses_post($settings['consent_text']); ?></span>
					</label>
				</div>
			<?php endif; ?>

			<div class="dsproofwp-form-footer">
				<?php if ($settings['recaptcha_enabled'] && !empty($settings['recaptcha_site_key']) && $settings['recaptcha_secret_key_set']): ?>
					<div class="dsproofwp-recaptcha-row" style="display: flex; justify-content: center; margin-bottom: 20px;">
						<div id="dsproofwp-recaptcha-container"></div>
						<input type="hidden" name="recaptcha_token" id="dsproofwp-recaptcha-token" value="">
					</div>
				<?php endif; ?>

				<div class="dsproofwp-submit-row">
					<?php wp_nonce_field('proofwp_review_form', 'proofwp_review_nonce'); ?>
					<button type="submit" class="dsproofwp-button"><?php esc_html_e('Submit Reviews', 'proofwp'); ?></button>
				</div>
			</div>
		</form>
	</section>
<?php
}
