<?php

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

// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
$settings = isset($settings) ? $settings : get_option('ugcify_review_form_settings', array('formVisibility' => 'logged-in'));
$formVisibility = isset($settings['formVisibility']) ? $settings['formVisibility'] : 'logged-in';
$mediaSizeLimitMb = isset($settings['mediaMaxSizeMb']) ? absint($settings['mediaMaxSizeMb']) : 0;
if ($mediaSizeLimitMb < 1) {
	$legacyImageLimit = isset($settings['maxImageSizeMb']) ? absint($settings['maxImageSizeMb']) : 0;
	$legacyVideoLimit = isset($settings['maxVideoSizeMb']) ? absint($settings['maxVideoSizeMb']) : 0;
	$mediaSizeLimitMb = $legacyImageLimit > 0 ? $legacyImageLimit : $legacyVideoLimit;
}
if ($mediaSizeLimitMb < 1) {
	$mediaSizeLimitMb = 10;
}
$enableAttachmentLimit = ! empty($settings['enableAttachmentLimit']);
$maxAttachmentsPerReview = isset($settings['maxAttachmentsPerReview']) ? absint($settings['maxAttachmentsPerReview']) : 0;
if ($maxAttachmentsPerReview < 1) {
	$maxAttachmentsPerReview = 5;
}
$isLoggedIn = is_user_logged_in();
$hideUserInfo = false;
if (
	($formVisibility === 'logged-in' && $isLoggedIn) ||
	($formVisibility === 'verified-buyers' && $isLoggedIn) ||
	($formVisibility === 'anyone' && $isLoggedIn)
) {
	$hideUserInfo = true;
}
?>
<!-- Single Page Form: one h2 for form title, labels associated via for/id; no duplicate meta. -->
<form id="ugcify-review-form" class="ugcify-review-form" aria-label="<?php esc_attr_e( 'Review submission form', 'ugcify' ); ?>">
   <h2 class="ugcify-form-title"><?php esc_html_e( 'Write a review', 'ugcify' ); ?></h2>

   <!-- Rating -->
   <div class="ugcify-form-field" role="group" aria-labelledby="ugcify-single-rating-label">
	   <span id="ugcify-single-rating-label" class="ugcify-sr-only"><?php esc_html_e( 'Rating', 'ugcify' ); ?></span>
	   <div class="ugcify-star-rating ugcify-star-rating-large" data-rating="0">
		   <?php for ( $i = 1; $i <= 5; $i++ ) : ?>
			   <button type="button" class="ugcify-star" data-value="<?php echo esc_attr( $i ); ?>">
				   <svg width="100%" height="100%" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
					   <path d="M12 2L15.09 8.26L22 9.27L17 14.14L18.18 21.02L12 17.77L5.82 21.02L7 14.14L2 9.27L8.91 8.26L12 2Z" 
						   stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
				   </svg>
			   </button>
		   <?php endfor; ?>
	   </div>
	   <input type="hidden" name="rating" id="single-rating" required />
   </div>

   <!-- File Upload -->
   <div class="ugcify-form-field">
	   <label class="ugcify-form-label">
		   <?php esc_html_e( 'Upload your image or video get 10% off', 'ugcify' ); ?>
	   </label>
	   <div class="ugcify-upload-area">
		   <input type="file" id="single-file-upload" multiple accept="image/*,video/*" class="ugcify-file-input" />
		   <label for="single-file-upload" class="ugcify-upload-label">
			   <svg width="48" height="48" viewBox="0 0 48 48" fill="none" xmlns="http://www.w3.org/2000/svg">
				   <path d="M24 16V32M16 24H32" stroke="#5B57F6" stroke-width="2" stroke-linecap="round"/>
				   <rect x="8" y="8" width="32" height="32" rx="4" stroke="#5B57F6" stroke-width="2" stroke-dasharray="4 4"/>
			   </svg>
			   <span class="ugcify-upload-text"><?php esc_html_e( 'Click to upload', 'ugcify' ); ?></span>
			   <span class="ugcify-upload-hint">
				   <?php
				   /* translators: %s: Maximum file size in megabytes. */
				   echo esc_html( sprintf( __( 'File must be less than %sMB', 'ugcify' ), $mediaSizeLimitMb ) );
				   ?><br/>
				   <?php if ( $enableAttachmentLimit ) : ?>
					   <?php
					   /* translators: %s: Maximum number of attachments allowed. */
					   echo esc_html( sprintf( __( 'Maximum %s attachment(s) per review', 'ugcify' ), $maxAttachmentsPerReview ) );
					   ?><br/>
				   <?php endif; ?>
				   <?php esc_html_e( 'Supported files: JPEG, PNG, GIF, WebP, MP4, MOV', 'ugcify' ); ?>
			   </span>
		   </label>
	   </div>
	   <div class="ugcify-upload-alert" id="ugcify-single-upload-alert" style="display: none;" role="alert"></div>
	   <div class="ugcify-uploaded-files" id="single-uploaded-files"></div>
   </div>

   <?php if (!$hideUserInfo) : ?>
   <!-- Email -->
   <div class="ugcify-form-field">
	   <label for="single-email" class="ugcify-form-label">
		   <?php esc_html_e( 'Your Email', 'ugcify' ); ?>*
	   </label>
	   <input type="email" id="single-email" name="email" class="ugcify-form-input" placeholder="Email" required />
   </div>

   <!-- Display Name -->
   <div class="ugcify-form-field">
	   <label for="single-display-name" class="ugcify-form-label">
		   <?php esc_html_e( 'Your Display Name', 'ugcify' ); ?>*
	   </label>
	   <input type="text" id="single-display-name" name="displayName" class="ugcify-form-input" placeholder="Your name" required />
	   <label class="ugcify-form-checkbox">
		   <input type="checkbox" id="single-anonymous" name="anonymous" />
		   <span><?php esc_html_e( 'Post review as anonymous', 'ugcify' ); ?></span>
	   </label>
   </div>
   <?php endif; ?>

   <!-- Review Message -->
   <div class="ugcify-form-field">
	   <label for="single-message" class="ugcify-form-label">
		   <?php esc_html_e( 'Your Message', 'ugcify' ); ?>*
	   </label>
	   <textarea id="single-message" name="message" class="ugcify-form-textarea" placeholder="<?php esc_attr_e( 'Write here', 'ugcify' ); ?>" rows="5" required></textarea>
	   <div class="ugcify-form-hint">
		   <?php esc_html_e( 'Name will not appear on the review', 'ugcify' ); ?>
	   </div>
   </div>

   <?php if ( ! empty( $settings['recaptchaEnabled'] ) && ! empty( $settings['recaptchaSiteKey'] ) ) : ?>
   <!-- reCAPTCHA -->
   <div class="ugcify-form-field ugcify-recaptcha-container">
	   <div class="g-recaptcha" data-sitekey="<?php echo esc_attr( $settings['recaptchaSiteKey'] ); ?>"></div>
   </div>
   <?php endif; ?>

   <!-- Submit Button -->
   <button type="submit" class="ugcify-submit-button">
	   <?php esc_html_e( 'Submit Review', 'ugcify' ); ?>
   </button>
</form>
