<?php

namespace WPVOID\StockBeacon\Admin;

if ( !defined( 'ABSPATH' ) ) {
    exit;
}
class ProductTab {
    public function register() : void {
        add_filter( 'woocommerce_product_data_tabs', array($this, 'add_tab') );
        add_action( 'woocommerce_product_data_panels', array($this, 'render_panel') );
        add_action( 'admin_enqueue_scripts', array($this, 'enqueue_assets') );
    }

    public function add_tab( array $tabs ) : array {
        $tabs['stockbeacon_waitlist'] = array(
            'label'    => __( 'Waitlist', 'stockbeacon-product-waitlists-for-woocommerce' ),
            'target'   => 'stockbeacon_waitlist_data',
            'class'    => array('show_if_simple', 'show_if_variable'),
            'priority' => 80,
        );
        return $tabs;
    }

    public function render_panel() : void {
        echo '<div id="stockbeacon_waitlist_data" class="panel woocommerce_options_panel">';
        echo '<div id="stockbeacon-product-waitlist-root" class="stockbeacon-admin-app"></div>';
        echo '</div>';
    }

    public function enqueue_assets( string $hook ) : void {
        if ( !in_array( $hook, array('post.php', 'post-new.php'), true ) ) {
            return;
        }
        $screen = get_current_screen();
        if ( !$screen || 'product' !== $screen->post_type ) {
            return;
        }
        // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- WooCommerce product editor.
        global $post;
        $product_id = ( $post instanceof \WP_Post ? (int) $post->ID : 0 );
        $has_premium_package = stockbeacon_has_premium_package();
        $config = array(
            'context'           => 'product',
            'productId'         => $product_id,
            'upgradeUrl'        => stockbeacon_fs()->get_upgrade_url(),
            'isPremium'         => false,
            'isLicensedPremium' => stockbeacon_fs()->can_use_premium_code(),
            'hasPremiumPackage' => $has_premium_package,
            'isFreePlan'        => stockbeacon_fs()->is_not_paying(),
            'isTrial'           => stockbeacon_fs()->is_trial(),
            'restUrl'           => rest_url( 'stockbeacon/v1' ),
            'nonce'             => wp_create_nonce( 'wp_rest' ),
            'portalUrl'         => 'https://customers.freemius.com/store/14307',
            'dashboardUrl'      => admin_url( 'admin.php?page=stockbeacon' ),
            'i18n'              => array(
                'recentSubscribers' => __( 'Recent subscribers', 'stockbeacon-product-waitlists-for-woocommerce' ),
                'openDashboard'     => __( 'Open dashboard', 'stockbeacon-product-waitlists-for-woocommerce' ),
                'noProductYet'      => __( 'Save the product to load waitlist subscribers.', 'stockbeacon-product-waitlists-for-woocommerce' ),
                'loading'           => __( 'Loading…', 'stockbeacon-product-waitlists-for-woocommerce' ),
                'loadError'         => __( 'Could not load subscribers.', 'stockbeacon-product-waitlists-for-woocommerce' ),
                'noneYet'           => __( 'No subscribers yet for this product.', 'stockbeacon-product-waitlists-for-woocommerce' ),
                'upgradeCta'        => __( 'Upgrade to Pro', 'stockbeacon-product-waitlists-for-woocommerce' ),
            ),
        );
        AdminBundle::enqueue_react( $config, 'stockbeaconProduct', false );
    }

}
