<?php
/**
 * Template: product/review-form.php
 *
 * Renders the review submission form with an interactive star picker.
 *
 * Available variables:
 *   $product_id   int     tc_products.id
 *   $customer_id  int|null
 *   $can_review   bool    false = already reviewed or blocked
 *   $is_logged_in bool
 *
 * Override: copy to your-theme/titancart/product/review-form.php
 *
 * @package TitanCart
 */
defined( 'ABSPATH' ) || exit;

$product_id   = (int) ( $product_id   ?? 0 );
$customer_id  = $customer_id  ?? null;
$can_review   = (bool) ( $can_review  ?? true );
$is_logged_in = (bool) ( $is_logged_in ?? is_user_logged_in() );

if ( ! $product_id ) {
    return;
}
?>

<div class="tc-review-form-wrap" id="tc-review-form-wrap">

    <h3 class="tc-review-form__heading">
        <?php esc_html_e( 'Write a Review', 'titancart-lite' ); ?>
    </h3>

    <?php if ( ! $is_logged_in ) : ?>
        <p class="tc-notice tc-notice--info">
            <?php
            printf(
                wp_kses(
                    /* translators: %s: login page URL. */
                    __( 'Please <a href="%s">log in</a> to submit a review.', 'titancart-lite' ),
                    [ 'a' => [ 'href' => [] ] ]
                ),
                esc_url( titancart_login_url() )
            );
            ?>
        </p>

    <?php elseif ( ! $can_review ) : ?>
        <p class="tc-notice tc-notice--info">
            <?php esc_html_e( 'You have already submitted a review for this product.', 'titancart-lite' ); ?>
        </p>

    <?php else : ?>

        <!-- Review submission notice (hidden until AJAX response) -->
        <div class="tc-notice js-tc-review-notice" role="alert" aria-live="polite" hidden></div>

        <form class="tc-review-form js-tc-review-form"
              data-product-id="<?php echo esc_attr( $product_id ); ?>"
              novalidate>

            <!-- Star picker -->
            <fieldset class="tc-review-form__fieldset">
                <legend class="tc-review-form__legend tc-label tc-label--required">
                    <?php esc_html_e( 'Your Rating', 'titancart-lite' ); ?>
                </legend>

                <div class="tc-star-picker js-tc-star-picker"
                     role="radiogroup"
                     aria-label="<?php esc_attr_e( 'Select a rating', 'titancart-lite' ); ?>">
                    <?php for ( $i = 1; $i <= 5; $i++ ) : ?>
                        <button type="button"
                                class="tc-star-picker__star"
                                data-value="<?php echo esc_attr( $i ); ?>"
                                aria-label="<?php
                                    /* translators: %d: star number */
                                    printf( esc_attr__( '%d star', 'titancart-lite' ), (int) $i );
                                ?>"
                                role="radio"
                                aria-checked="false">
                            <svg width="28" height="28" viewBox="0 0 16 16" fill="#E1E5EA"
                                 xmlns="http://www.w3.org/2000/svg"
                                 class="tc-star-picker__icon">
                                <path d="M8 1l1.8 3.6L14 5.3 11 8.1l.7 3.9L8 10.1l-3.7 1.9.7-3.9L2 5.3l4.2-.7z"/>
                            </svg>
                        </button>
                    <?php endfor; ?>
                </div>

                <input type="hidden"
                       name="rating"
                       id="tc-review-rating"
                       class="js-tc-rating-value"
                       value="0" />

                <p class="tc-star-picker__label js-tc-star-label tc-muted" aria-live="polite">
                    <?php esc_html_e( 'Click a star to rate', 'titancart-lite' ); ?>
                </p>
            </fieldset>

            <!-- Title -->
            <div class="tc-form-row">
                <label for="tc-review-title" class="tc-label">
                    <?php esc_html_e( 'Review Title', 'titancart-lite' ); ?>
                    <span class="tc-muted"><?php esc_html_e( '(optional)', 'titancart-lite' ); ?></span>
                </label>
                <input type="text"
                       id="tc-review-title"
                       name="title"
                       class="tc-input"
                       maxlength="255"
                       placeholder="<?php esc_attr_e( 'Summarise your experience…', 'titancart-lite' ); ?>" />
            </div>

            <!-- Body -->
            <div class="tc-form-row">
                <label for="tc-review-body" class="tc-label tc-label--required">
                    <?php esc_html_e( 'Your Review', 'titancart-lite' ); ?>
                </label>
                <textarea id="tc-review-body"
                          name="body"
                          class="tc-textarea"
                          rows="5"
                          required
                          placeholder="<?php esc_attr_e( 'Share your thoughts about this product…', 'titancart-lite' ); ?>"></textarea>
            </div>

            <?php
            /**
             * Fires inside the review form, after the body textarea and
             * before the submit button. Extensions can render additional
             * optional fields here (e.g. marketplace vendor rating).
             *
             * Listeners that add input elements should give them a `name`
             * attribute matching the REST body key they want to pass
             * through. reviews.js explicitly picks fields by name —
             * any new field needs companion JS to be wired into the
             * submission payload. The marketplace's VendorReviewFormFieldInjector
             * (S201) renders a second star picker and is paired with
             * reviews.js's vendor_rating pickup at the same release.
             *
             * @since S201
             *
             * @param int $product_id The product being reviewed.
             */
            do_action( 'titancart_review_form_extra_fields', (int) $product_id );
            ?>

            <!-- Submit -->
            <div class="tc-form-row tc-form-row--actions">
                <button type="submit"
                        class="tc-btn tc-btn--primary js-tc-review-submit">
                    <?php esc_html_e( 'Submit Review', 'titancart-lite' ); ?>
                </button>
            </div>

        </form><!-- .tc-review-form -->

    <?php endif; ?>

</div><!-- #tc-review-form-wrap -->
