<?php
/**
 * WooCommerce Settings Page
 *
 * @package ReplyIt_Chat_Widget
 */

// Exit if accessed directly
if (!defined('ABSPATH')) {
    exit;
}

// Check if WooCommerce is active
if (!class_exists('WooCommerce')) {
    ?>
    <div class="wrap">
        <h1><?php esc_html_e('WooCommerce Settings', 'replyit-chat-widget'); ?></h1>
        <div class="notice notice-warning">
            <p>
                <?php esc_html_e('WooCommerce is not installed or activated.', 'replyit-chat-widget'); ?>
                <a href="<?php echo esc_url( admin_url('plugin-install.php?s=woocommerce&tab=search&type=term') ); ?>">
                    <?php esc_html_e('Install WooCommerce', 'replyit-chat-widget'); ?>
                </a>
            </p>
        </div>
    </div>
    <?php
    return;
}

// Get current settings
$woo_enabled = get_option('replyit_woocommerce_enabled', false);
$cart_abandon_time = get_option('replyit_cart_abandon_time', 3);
$product_page_delay = get_option('replyit_product_page_delay', 20);
$checkout_help_enabled = get_option('replyit_checkout_help_enabled', true);
$order_tracking_enabled = get_option('replyit_order_tracking_enabled', true);
$product_search_enabled = get_option('replyit_product_search_enabled', true);

// Handle form submission
if (isset($_POST['submit']) && check_admin_referer('replyit_woocommerce_settings')) {
    update_option('replyit_woocommerce_enabled', isset($_POST['replyit_woocommerce_enabled']));
    update_option('replyit_cart_abandon_time', intval($_POST['replyit_cart_abandon_time']));
    update_option('replyit_product_page_delay', intval($_POST['replyit_product_page_delay']));
    update_option('replyit_checkout_help_enabled', isset($_POST['replyit_checkout_help_enabled']));
    update_option('replyit_order_tracking_enabled', isset($_POST['replyit_order_tracking_enabled']));
    update_option('replyit_product_search_enabled', isset($_POST['replyit_product_search_enabled']));

    // Sync settings to backend
    $sync_result = ReplyIt_API::update_woocommerce_settings();

    if (isset($sync_result['success']) && $sync_result['success']) {
        echo '<div class="notice notice-success is-dismissible"><p>' . esc_html__('WooCommerce settings saved and synced!', 'replyit-chat-widget') . '</p></div>';
    } else {
        echo '<div class="notice notice-warning is-dismissible"><p>' . esc_html__('Settings saved locally, but failed to sync with backend.', 'replyit-chat-widget') . '</p></div>';
    }

    // Refresh values
    $woo_enabled = get_option('replyit_woocommerce_enabled', false);
    $cart_abandon_time = get_option('replyit_cart_abandon_time', 3);
    $product_page_delay = get_option('replyit_product_page_delay', 20);
    $checkout_help_enabled = get_option('replyit_checkout_help_enabled', true);
    $order_tracking_enabled = get_option('replyit_order_tracking_enabled', true);
    $product_search_enabled = get_option('replyit_product_search_enabled', true);
}
?>

<div class="wrap">
    <h1><?php esc_html_e('WooCommerce Integration Settings', 'replyit-chat-widget'); ?></h1>

    <div class="replyit-woocommerce-settings">
        <form method="post" action="">
            <?php wp_nonce_field('replyit_woocommerce_settings'); ?>

            <div class="card">
                <h2><?php esc_html_e('Enable WooCommerce Features', 'replyit-chat-widget'); ?></h2>
                <table class="form-table">
                    <tr>
                        <th scope="row">
                            <?php esc_html_e('WooCommerce Integration', 'replyit-chat-widget'); ?>
                        </th>
                        <td>
                            <label>
                                <input type="checkbox"
                                       name="replyit_woocommerce_enabled"
                                       value="1"
                                       <?php checked($woo_enabled); ?>>
                                <?php esc_html_e('Enable WooCommerce integration', 'replyit-chat-widget'); ?>
                            </label>
                            <p class="description">
                                <?php esc_html_e('Send cart, product, and customer data to AI agent for personalized support.', 'replyit-chat-widget'); ?>
                            </p>
                        </td>
                    </tr>
                </table>
            </div>

            <div class="card">
                <h2><?php esc_html_e('Proactive Messages', 'replyit-chat-widget'); ?></h2>
                <p class="description">
                    <?php esc_html_e('Automatically open chat widget with helpful messages based on customer behavior.', 'replyit-chat-widget'); ?>
                </p>

                <table class="form-table">
                    <tr>
                        <th scope="row">
                            <label for="replyit_cart_abandon_time">
                                <?php esc_html_e('Cart Abandon Time', 'replyit-chat-widget'); ?>
                            </label>
                        </th>
                        <td>
                            <input type="number"
                                   id="replyit_cart_abandon_time"
                                   name="replyit_cart_abandon_time"
                                   value="<?php echo esc_attr($cart_abandon_time); ?>"
                                   min="1"
                                   max="30"
                                   class="small-text">
                            <span><?php esc_html_e('minutes', 'replyit-chat-widget'); ?></span>
                            <p class="description">
                                <?php esc_html_e('Show message if customer has items in cart but hasn\'t checked out.', 'replyit-chat-widget'); ?>
                            </p>
                        </td>
                    </tr>

                    <tr>
                        <th scope="row">
                            <label for="replyit_product_page_delay">
                                <?php esc_html_e('Product Page Delay', 'replyit-chat-widget'); ?>
                            </label>
                        </th>
                        <td>
                            <input type="number"
                                   id="replyit_product_page_delay"
                                   name="replyit_product_page_delay"
                                   value="<?php echo esc_attr($product_page_delay); ?>"
                                   min="5"
                                   max="120"
                                   class="small-text">
                            <span><?php esc_html_e('seconds', 'replyit-chat-widget'); ?></span>
                            <p class="description">
                                <?php esc_html_e('Show help message if customer stays on product page.', 'replyit-chat-widget'); ?>
                            </p>
                        </td>
                    </tr>

                    <tr>
                        <th scope="row">
                            <?php esc_html_e('Checkout Page Help', 'replyit-chat-widget'); ?>
                        </th>
                        <td>
                            <label>
                                <input type="checkbox"
                                       name="replyit_checkout_help_enabled"
                                       value="1"
                                       <?php checked($checkout_help_enabled); ?>>
                                <?php esc_html_e('Show help button on checkout page', 'replyit-chat-widget'); ?>
                            </label>
                            <p class="description">
                                <?php esc_html_e('Offer assistance when customer is on checkout page after 30 seconds.', 'replyit-chat-widget'); ?>
                            </p>
                        </td>
                    </tr>
                </table>
            </div>

            <div class="card">
                <h2><?php esc_html_e('AI Tools', 'replyit-chat-widget'); ?></h2>
                <p class="description">
                    <?php esc_html_e('Enable AI-powered tools to help customers with common questions.', 'replyit-chat-widget'); ?>
                </p>
                <table class="form-table">
                    <tr>
                        <th scope="row">
                            <?php esc_html_e('Order Tracking', 'replyit-chat-widget'); ?>
                        </th>
                        <td>
                            <label>
                                <input type="checkbox"
                                       name="replyit_order_tracking_enabled"
                                       value="1"
                                       <?php checked($order_tracking_enabled); ?>>
                                <?php esc_html_e('Allow customers to track orders via chat', 'replyit-chat-widget'); ?>
                            </label>
                            <p class="description">
                                <?php esc_html_e('AI can answer "Where is my order?" questions with real-time tracking info.', 'replyit-chat-widget'); ?>
                            </p>
                        </td>
                    </tr>
                    <tr>
                        <th scope="row">
                            <?php esc_html_e('Product Search', 'replyit-chat-widget'); ?>
                        </th>
                        <td>
                            <label>
                                <input type="checkbox"
                                       name="replyit_product_search_enabled"
                                       value="1"
                                       <?php checked($product_search_enabled); ?>>
                                <?php esc_html_e('Allow customers to search products via chat', 'replyit-chat-widget'); ?>
                            </label>
                            <p class="description">
                                <?php esc_html_e('AI can search products by name, SKU, or category and show stock status, prices, etc.', 'replyit-chat-widget'); ?>
                            </p>
                        </td>
                    </tr>
                </table>
            </div>

            <p class="submit">
                <button type="submit" name="submit" class="button button-primary">
                    <?php esc_html_e('Save WooCommerce Settings', 'replyit-chat-widget'); ?>
                </button>
            </p>
        </form>
    </div>
</div>

