<?php
if (! defined('ABSPATH')) {
    exit;
}

use Meliconnect\Meliconnect\Core\Controllers\SettingController;
use Meliconnect\Meliconnect\Core\Helpers\Helper;

$sync_data = Helper::getMeliconnectOptions('sync');
update_option( 'meliconnect_hide_sync_notice', true );
?>

<h2><?php esc_html_e('Synchronization Settings', 'meliconnect'); ?></h2>
<p class="description">
    <?php esc_html_e('Configure how WooCommerce and MercadoLibre stay synchronized. You can use real-time triggers, scheduled synchronization, or both.', 'meliconnect'); ?>
</p>


<form id="meliconnect-sync-settings-form">

    <!-- MASTER SWITCH -->
    <?php
    SettingController::print_setting_checkbox(
        'meliconnect_sync_enabled',
        __('Enable synchronization', 'meliconnect'),
        $sync_data['meliconnect_sync_enabled'] ?? 'false',
        'true',
        __('Enable automatic synchronization between WooCommerce and MercadoLibre.', 'meliconnect')
    );
    ?>

    <hr>
    <div id="meliconnect-realtime-section">

        <div id="meliconnect-realtime-disabled-message" class="meliconnect-notification meliconnect-is-info" style="display:none;">
            <p>
                <?php esc_html_e(
                    'Real-time triggers are disabled because synchronization is set to MercadoLibre → WooCommerce. Updates are received from MercadoLibre via API/webhooks, not from WooCommerce actions.',
                    'meliconnect'
                ); ?>
            </p>
        </div>

    </div>
    <hr>

    <div id="meliconnect-cron-section">
        <!-- SCHEDULED SYNC -->
        <h3><?php esc_html_e('Scheduled synchronization', 'meliconnect'); ?></h3>

        <p class="description">
            <?php esc_html_e('Runs synchronization periodically to ensure data consistency and recover missed updates using WP-Cron.', 'meliconnect'); ?>
        </p>

        <?php
        SettingController::print_setting_checkbox(
            'meliconnect_sync_enable_cron',
            __('Enable scheduled synchronization', 'meliconnect'),
            $sync_data['meliconnect_sync_enable_cron'] ?? 'false',
            'true'
        );
        ?>




        <div class="meliconnect-columns meliconnect-is-mobile meliconnect-is-multiline">
            <div class="meliconnect-column meliconnect-is-12">
                <div class="meliconnect-columns meliconnect-meliconnect-is-mobile">
                    <div class="meliconnect-column meliconnect-is-3">
                        <div class="meliconnect-field">
                            <label class="meliconnect-label" for="meliconnect_sync_batch_size"><?php esc_html_e('Items per batch', 'meliconnect'); ?></label>
                            <div class="meliconnect-control">
                                <input class="meliconnect-input" type="number" min="1" name="meliconnect_sync_batch_size" id="meliconnect_sync_batch_size" value="<?php echo isset($sync_data['meliconnect_sync_batch_size']) ? esc_attr($sync_data['meliconnect_sync_batch_size']) : ''; ?>" min="1" max="1000">
                            </div>
                        </div>
                    </div>
                    <div class="meliconnect-column meliconnect-is-4">
                        <div class="meliconnect-field">
                            <label class="meliconnect-label" for="meliconnect_sync_frequency"><?php esc_html_e('Frequency runs batch (minutes)', 'meliconnect'); ?></label>
                            <div class="meliconnect-control">
                                <input class="meliconnect-input" type="number" min="1" name="meliconnect_sync_frequency" id="meliconnect_sync_frequency" value="<?php echo isset($sync_data['meliconnect_sync_frequency']) ? esc_attr($sync_data['meliconnect_sync_frequency']) : ''; ?>">
                            </div>
                        </div>
                    </div>

                </div>
            </div>
            <div class="meliconnect-column meliconnect-is-12">
                <div class="meliconnect-columns meliconnect-is-mobile meliconnect-is-align-items-flex-end">
                    <div class="meliconnect-column meliconnect-is-4">
                        <div class="meliconnect-field">
                            <label class="meliconnect-label" for="meliconnect_sync_reconcile_hours">
                                <?php esc_html_e('Reconcile interval (hours)', 'meliconnect'); ?>
                            </label>

                            <div class="meliconnect-control">
                                <input
                                    class="meliconnect-input"
                                    type="number"
                                    min="1"
                                    max="168"
                                    name="meliconnect_sync_reconcile_hours"
                                    id="meliconnect_sync_reconcile_hours"
                                    value="<?php echo isset($sync_data['meliconnect_sync_reconcile_hours'])
                                                ? esc_attr($sync_data['meliconnect_sync_reconcile_hours'])
                                                : '6'; ?>">
                            </div>
                        </div>
                    </div>
                    <div class="meliconnect-column meliconnect-is-6">
                        <p class="meliconnect-description">
                            <?php esc_html_e('If a product has not been synchronized within this number of hours, it will be automatically added back to the sync queue.', 'meliconnect'); ?>
                        </p>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <hr>
    <!-- WHAT TO SYNC -->
    <h3><?php esc_html_e('What to synchronize', 'meliconnect'); ?></h3>

    <?php
    SettingController::print_setting_checkbox(
        'meliconnect_sync_stock',
        __('Stock', 'meliconnect'),
        $sync_data['meliconnect_sync_stock'] ?? 'false',
        'true'
    );

    SettingController::print_setting_checkbox(
        'meliconnect_sync_price',
        __('Price', 'meliconnect'),
        $sync_data['meliconnect_sync_price'] ?? 'false',
        'true'
    );

    SettingController::print_setting_checkbox(
        'meliconnect_sync_status',
        __('Status (active / paused)', 'meliconnect'),
        $sync_data['meliconnect_sync_status'] ?? 'false',
        'true'
    );
    ?>
    <hr>
    <div id="meliconnect-sync-direction-wrapper">
        <h3><?php esc_html_e('Synchronization direction', 'meliconnect'); ?></h3>

        <div class="meliconnect-columns">
            <div class="meliconnect-column meliconnect-is-12">
                <div class="meliconnect-select meliconnect-is-fullwidth">
                    <select name="meliconnect_sync_direction" id="meliconnect_sync_direction">
                        <option value="woo_to_meli" <?php selected($sync_data['meliconnect_sync_direction'] ?? '', 'woo_to_meli'); ?>>
                            <?php esc_html_e('WooCommerce → MercadoLibre', 'meliconnect'); ?>
                        </option>
                        <option value="meli_to_woo" <?php selected($sync_data['meliconnect_sync_direction'] ?? '', 'meli_to_woo'); ?>>
                            <?php esc_html_e('MercadoLibre → WooCommerce', 'meliconnect'); ?>
                        </option>
                        <option value="bidirectional" <?php selected($sync_data['meliconnect_sync_direction'] ?? '', 'bidirectional'); ?>>
                            <?php esc_html_e('Both directions (bidirectional)', 'meliconnect'); ?>
                        </option>
                    </select>
                </div>

                <p class="description">
                    <?php esc_html_e('Defines the main data flow direction between WooCommerce and MercadoLibre.', 'meliconnect'); ?>
                </p>
            </div>
        </div>
    </div>

    <hr>

    <div id="meliconnect-conflict-section">
        <!-- CONFLICT RESOLUTION -->
        <h3><?php esc_html_e('Conflict resolution', 'meliconnect'); ?></h3>

        <div class="meliconnect-columns">
            <div class="meliconnect-column">
                <label><?php esc_html_e('Priority', 'meliconnect'); ?></label>
            </div>
            <div class="meliconnect-column meliconnect-is-8">
                <div class="meliconnect-select meliconnect-is-fullwidth">
                    <select name="meliconnect_sync_conflict_resolution">
                        <option value="woo" <?php selected($sync_data['meliconnect_sync_conflict_resolution'] ?? '', 'woo'); ?>>
                            <?php esc_html_e('WooCommerce overrides MercadoLibre', 'meliconnect'); ?>
                        </option>
                        <option value="meli" <?php selected($sync_data['meliconnect_sync_conflict_resolution'] ?? '', 'meli'); ?>>
                            <?php esc_html_e('MercadoLibre overrides WooCommerce', 'meliconnect'); ?>
                        </option>
                        <option value="auto" <?php selected($sync_data['meliconnect_sync_conflict_resolution'] ?? '', 'last_updated'); ?>>
                            <?php esc_html_e('Last updated wins (by date)', 'meliconnect'); ?>
                        </option>
                    </select>
                </div>
                <p class="description">
                    <?php esc_html_e('Defines which platform takes priority when both have different values.', 'meliconnect'); ?>
                </p>
            </div>
        </div>
    </div>
    <hr>

    <!-- SUBMIT -->
    <button type="submit" id="save-sync-button" class="meliconnect-button meliconnect-is-primary">
        <?php esc_html_e('Save Synchronization Settings', 'meliconnect'); ?>
    </button>

</form>