<?php

declare(strict_types=1);

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

/**
 * Service to handle the custom Feed and Form on the single product page.
 */
class DSProofwp_Product_Page_Review
{
    public static function init(): void
    {
        // Only run on the frontend
        if (is_admin()) {
            return;
        }

        add_filter('woocommerce_product_tabs', [self::class, 'handle_reviews_tab'], 99);
    }

    /**
     * Override the WooCommerce reviews tab if it exists.
     */
    public static function handle_reviews_tab(array $tabs): array
    {
        if (isset($tabs['reviews'])) {
            $tabs['reviews']['callback'] = [self::class, 'render_review_feed_form'];
        }

        return $tabs;
    }

    /**
     * Render the target div for React to inject the Feed and Form.
     */
    public static function render_review_feed_form(): void
    {
        $product_id = get_the_ID();
        echo DSProofwp_Plugin::maybe_render_frontend_config();
        echo '<div class="dsproofwp-review-feed-form-container" id="reviews" data-product-id="' . esc_attr((string) $product_id) . '"></div>';
    }
}
