<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}
?>
<form id="meliconnect-ml-events-settings-form">

    <input type="hidden" name="checkbox_fields" id="checkbox_fields"
        value="meliconnect_sync_enable_auto_sync,meliconnect_sync_on_orders,meliconnect_sync_on_product_changes,meliconnect_sync_create_product, meliconnect_sync_on_stock_locations, meliconnect_sync_on_price_changes">

    <section class="meliconnect-section">
        <div class="meliconnect-container">
            <div class="meliconnect-columns">
                <div class="meliconnect-column">

                    <?php if ( ! empty( $sellers_with_free_plan ) ) : ?>
                        <div class="meliconnect-notification meliconnect-is-warning">
                            <?php
                            printf(
                                'The following free plan users will not be able to perform automatic synchronizations: <strong>%s</strong>.',
                                implode( ', ', $sellers_with_free_plan )
                            );
                            ?>
                        </div>
                    <?php endif; ?>

                    <h2 class="meliconnect-title meliconnect-is-5">
                        <?php esc_html_e( 'MercadoLibre Callbacks', 'meliconnect' ); ?>
                    </h2>

                    <div class="meliconnect-content">

                        <?php
                        self::print_setting_checkbox(
                            'meliconnect_sync_enable_auto_sync',
                            esc_html__( 'Enable webhook processing', 'meliconnect' ),
                            $sync_data['meliconnect_sync_enable_auto_sync'],
                            'true',
                            esc_html__(
                                'Enable processing of MercadoLibre notifications (webhooks). When MercadoLibre sends callbacks, such as product creation, updates, deletions, or order events.',
                                'meliconnect'
                            )
                        );
                        ?>

                        <hr>

                        <h3 class="meliconnect-title meliconnect-is-6">
                            <?php esc_html_e( 'Process webhooks for:', 'meliconnect' ); ?>
                        </h3>

                        <?php
                        self::print_setting_checkbox(
                            'meliconnect_sync_on_orders',
                            esc_html__( 'Process order events', 'meliconnect' ),
                            $sync_data['meliconnect_sync_on_orders'],
                            'true',
                            esc_html__( 'Update WooCommerce stock, price or status when an order is created or updated in MercadoLibre.', 'meliconnect' )
                        );

                        self::print_setting_checkbox(
                            'meliconnect_sync_on_product_changes',
                            esc_html__( 'Process item update events', 'meliconnect' ),
                            $sync_data['meliconnect_sync_on_product_changes'],
                            'true',
                            esc_html__( 'Update WooCommerce products when listings are modified in MercadoLibre. Based on import settings.', 'meliconnect' )
                        );

                        self::print_setting_checkbox(
                            'meliconnect_sync_create_product',
                            esc_html__( 'Auto-create products from events', 'meliconnect' ),
                            $sync_data['meliconnect_sync_create_product'],
                            'true',
                            esc_html__( 'When enabled, a WooCommerce product will be created if a MercadoLibre listing does not exist yet during a product callback.', 'meliconnect' )
                        );

                        self::print_setting_checkbox(
                            'meliconnect_sync_on_stock_locations',
                            __( 'Process stock location events', 'meliconnect' ),
                            $sync_data['meliconnect_sync_on_stock_locations'] ?? 'false',
                            'true',
                            __( 'Updates stock based on MercadoLibre stock location changes', 'meliconnect' )
                        );

                        self::print_setting_checkbox(
                            'meliconnect_sync_on_price_changes',
                            __( 'Process price events', 'meliconnect' ),
                            $sync_data['meliconnect_sync_on_price_changes'] ?? 'false',
                            'true',
                            __( 'Updates price based on MercadoLibre price changes callbacks', 'meliconnect' )
                        );
                        ?>

                        
                        

                        <div class="meliconnect-notification meliconnect-is-info">
                            <?php
                            printf(
                                esc_html__(
                                    'WooCommerce updates depend on the enabled synchronization options (price, stock, status). Configure them in %s.',
                                    'meliconnect'
                                ),
                                '<a href="' . esc_url(admin_url('admin.php?page=meliconnect-settings#synchronization')) . '" target="_blank">Sync Settings</a>'
                            );
                            ?>
                        </div>

                        <div class="meliconnect-level">
                            <div class="meliconnect-level-right">
                                <button type="submit" id="save-ml-events-button"
                                    class="meliconnect-button meliconnect-is-primary">
                                    <?php esc_html_e( 'Save Webhook Settings', 'meliconnect' ); ?>
                                </button>
                            </div>
                        </div>

                    </div>
                </div>
            </div>
        </div>
    </section>
</form>