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

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

$widget_id = get_option('replyit_widget_id', '');
$api_secret = get_option('replyit_api_secret', '');
$api_url = get_option('replyit_api_url', 'https://replyit.ai'); // Static API URL
$enabled = get_option('replyit_enabled', true);
$woo_enabled = get_option('replyit_woocommerce_enabled', false);
$woo_available = class_exists('WooCommerce');
$blog_search_enabled = get_option('replyit_blog_search_enabled', true);
$blog_content_enabled = get_option('replyit_blog_content_enabled', true);

// Handle settings save
if (isset($_POST['submit']) && check_admin_referer('replyit_chat_settings')) {
    update_option('replyit_widget_id', sanitize_text_field($_POST['replyit_widget_id'] ?? ''));
    update_option('replyit_api_secret', sanitize_text_field($_POST['replyit_api_secret'] ?? ''));
    update_option('replyit_enabled', isset($_POST['replyit_enabled']));

    // Blog settings
    update_option('replyit_blog_search_enabled', isset($_POST['replyit_blog_search_enabled']));
    update_option('replyit_blog_content_enabled', isset($_POST['replyit_blog_content_enabled']));

    if ($woo_available) {
        $woo_newly_enabled = isset($_POST['replyit_woocommerce_enabled']);
        update_option('replyit_woocommerce_enabled', $woo_newly_enabled);

        // If WooCommerce is enabled, sync default settings to backend
        if ($woo_newly_enabled) {
            // Set default values for WooCommerce settings if not already set
            if (get_option('replyit_order_tracking_enabled') === false) {
                update_option('replyit_order_tracking_enabled', true);
                update_option('replyit_cart_abandon_time', 3);
                update_option('replyit_product_page_delay', 20);
                update_option('replyit_checkout_help_enabled', true);
            }

            // Sync WooCommerce settings to backend
            ReplyIt_API::update_woocommerce_settings();
        }
    }

    // Sync blog settings to backend
    ReplyIt_API::update_blog_settings();

    echo '<div class="notice notice-success is-dismissible"><p>' . esc_html__('Settings saved successfully!', 'replyit-chat-widget') . '</p></div>';

    // Refresh values
    $widget_id = get_option('replyit_widget_id', '');
    $api_secret = get_option('replyit_api_secret', '');
    $enabled = get_option('replyit_enabled', true);
    $woo_enabled = get_option('replyit_woocommerce_enabled', false);
    $blog_search_enabled = get_option('replyit_blog_search_enabled', true);
    $blog_content_enabled = get_option('replyit_blog_content_enabled', true);
}

// Show settings errors
settings_errors('replyit_messages');
?>

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

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

                <div class="card">
                    <h2><?php esc_html_e('Widget Configuration', 'replyit-chat-widget'); ?></h2>
                    <p class="description">
                        <?php esc_html_e('Get your Widget ID and API Secret from', 'replyit-chat-widget'); ?>
                        <a href="<?php echo esc_url($api_url . '/chat-widgets'); ?>" target="_blank">
                            <?php esc_html_e('ReplyIt Dashboard', 'replyit-chat-widget'); ?> →
                        </a>
                    </p>

                    <table class="form-table">
                        <tr>
                            <th scope="row">
                                <label for="replyit_widget_id"><?php esc_html_e('Widget ID', 'replyit-chat-widget'); ?> *</label>
                            </th>
                            <td>
                                <input type="text"
                                       id="replyit_widget_id"
                                       name="replyit_widget_id"
                                       value="<?php echo esc_attr($widget_id); ?>"
                                       class="regular-text"
                                       placeholder="widget_xxxxxxxxxxxx"
                                       required>
                                <p class="description">
                                    <?php esc_html_e('Format: widget_XXXXXXXXXXXX', 'replyit-chat-widget'); ?>
                                </p>
                            </td>
                        </tr>

                        <tr>
                            <th scope="row">
                                <label for="replyit_api_secret"><?php esc_html_e('API Secret', 'replyit-chat-widget'); ?> *</label>
                            </th>
                            <td>
                                <input type="password"
                                       id="replyit_api_secret"
                                       name="replyit_api_secret"
                                       value="<?php echo esc_attr($api_secret); ?>"
                                       class="regular-text"
                                       placeholder="64-character hex string"
                                       required>
                                <button type="button" class="button" id="replyit-toggle-secret">
                                    <?php esc_html_e('Show', 'replyit-chat-widget'); ?>
                                </button>
                                <p class="description">
                                    <?php esc_html_e('Keep this secret secure. Required for API authentication.', 'replyit-chat-widget'); ?>
                                </p>
                            </td>
                        </tr>

                        <tr>
                            <th scope="row">
                                <?php esc_html_e('Enable Widget', 'replyit-chat-widget'); ?>
                            </th>
                            <td>
                                <label>
                                    <input type="checkbox"
                                           name="replyit_enabled"
                                           value="1"
                                           <?php checked($enabled); ?>>
                                    <?php esc_html_e('Display chat widget on your website', 'replyit-chat-widget'); ?>
                                </label>
                            </td>
                        </tr>
                    </table>
                </div>

                <!-- Blog Content Integration -->
                <div class="card">
                    <h2><?php esc_html_e('Blog Content Integration', 'replyit-chat-widget'); ?></h2>
                    <p class="description">
                        <?php esc_html_e('Allow AI to search and discuss your blog posts with visitors', 'replyit-chat-widget'); ?>
                    </p>

                    <table class="form-table">
                        <tr>
                            <th scope="row">
                                <?php esc_html_e('Blog Post Search', 'replyit-chat-widget'); ?>
                            </th>
                            <td>
                                <label>
                                    <input type="checkbox"
                                           name="replyit_blog_search_enabled"
                                           value="1"
                                           <?php checked($blog_search_enabled); ?>>
                                    <?php esc_html_e('Enable AI to search through blog posts', 'replyit-chat-widget'); ?>
                                </label>
                                <p class="description">
                                    <?php esc_html_e('AI can find relevant posts by title, content, category, tag, or author', 'replyit-chat-widget'); ?>
                                </p>
                            </td>
                        </tr>
                        <tr>
                            <th scope="row">
                                <?php esc_html_e('Blog Content Retrieval', 'replyit-chat-widget'); ?>
                            </th>
                            <td>
                                <label>
                                    <input type="checkbox"
                                           name="replyit_blog_content_enabled"
                                           value="1"
                                           <?php checked($blog_content_enabled); ?>>
                                    <?php esc_html_e('Enable AI to read full blog post content', 'replyit-chat-widget'); ?>
                                </label>
                                <p class="description">
                                    <?php esc_html_e('AI can summarize articles and answer questions about post content', 'replyit-chat-widget'); ?>
                                </p>
                            </td>
                        </tr>
                    </table>
                </div>

                <?php if ($woo_available): ?>
                    <div class="card">
                        <h2><?php esc_html_e('WooCommerce Integration', 'replyit-chat-widget'); ?></h2>
                        <p class="description">
                            <?php esc_html_e('Enhanced features for WooCommerce stores', 'replyit-chat-widget'); ?>
                        </p>

                        <table class="form-table">
                            <tr>
                                <th scope="row">
                                    <?php esc_html_e('Enable WooCommerce Features', 'replyit-chat-widget'); ?>
                                </th>
                                <td>
                                    <label>
                                        <input type="checkbox"
                                               name="replyit_woocommerce_enabled"
                                               value="1"
                                               <?php checked($woo_enabled); ?>>
                                        <?php esc_html_e('Send cart, product, and customer data to AI agent', 'replyit-chat-widget'); ?>
                                    </label>
                                    <p class="description">
                                        <?php esc_html_e('Enables: Cart recovery, Product recommendations, Order tracking, Personalized support', 'replyit-chat-widget'); ?>
                                    </p>
                                </td>
                            </tr>
                        </table>
                    </div>
                <?php endif; ?>

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

        <div class="replyit-settings-sidebar">
            <div class="card">
                <h3><?php esc_html_e('Quick Start Guide', 'replyit-chat-widget'); ?></h3>
                <ol>
                    <li><?php esc_html_e('Create a widget at', 'replyit-chat-widget'); ?> <a href="<?php echo esc_url($api_url); ?>" target="_blank">ReplyIt.ai</a></li>
                    <li><?php esc_html_e('Copy Widget ID and API Secret', 'replyit-chat-widget'); ?></li>
                    <li><?php esc_html_e('Paste them in the form above', 'replyit-chat-widget'); ?></li>
                    <li><?php esc_html_e('Save settings and enjoy!', 'replyit-chat-widget'); ?></li>
                </ol>
            </div>

            <?php if (!empty($widget_id) && !empty($api_secret)): ?>
                <div class="card">
                    <h3><?php esc_html_e('Domain Registration', 'replyit-chat-widget'); ?></h3>
                    <p><?php esc_html_e('Register your domain to allow widget to work:', 'replyit-chat-widget'); ?></p>
                    <p><strong><?php echo esc_html(parse_url(get_site_url(), PHP_URL_HOST)); ?></strong></p>

                    <form method="post">
                        <?php wp_nonce_field('replyit_register_domain'); ?>
                        <button type="submit" name="replyit_register_domain" class="button button-secondary">
                            <?php esc_html_e('Register Domain', 'replyit-chat-widget'); ?>
                        </button>
                    </form>
                </div>
            <?php endif; ?>

            <div class="card">
                <h3><?php esc_html_e('Need Help?', 'replyit-chat-widget'); ?></h3>
                <ul>
                    <li><a href="<?php echo esc_url($api_url . '/docs/wordpress-plugin'); ?>" target="_blank"><?php esc_html_e('Documentation', 'replyit-chat-widget'); ?></a></li>
                    <li><a href="<?php echo esc_url($api_url . '/support'); ?>" target="_blank"><?php esc_html_e('Support', 'replyit-chat-widget'); ?></a></li>
                </ul>
            </div>
        </div>
    </div>
</div>

