<?php
defined('ABSPATH') || exit;

// nonce check
if (!isset($_GET['nonce']) || !wp_verify_nonce( sanitize_text_field( wp_unslash ( $_GET['nonce'] ) ), 'ai_botkit_chatbots' ) ) {
    wp_die(__('Invalid request', 'ai-botkit-for-lead-generation'));
}

use AI_BotKit\Integration\API_Handler;

$available_post_types = API_Handler::get_available_post_types();
$sync_settings = get_option('ai_botkit_post_sync_settings', []);
$enabled_post_types = $sync_settings['enabled_post_types'] ?? [];
$auto_sync = $sync_settings['auto_sync'] ?? 1;
?>

<div class="ai-botkit-dashboard-wrapper">
    <div class="ai-botkit-dashboard-container">
        <div class="ai-botkit-dashboard-header">
            <div class="ai-botkit-knowledge-header-left">
                <h1 class="ai-botkit-dashboard-title"><?php esc_html_e('Content Sync Settings', 'ai-botkit-for-lead-generation'); ?></h1>
                <p class="ai-botkit-dashboard-subtitle"><?php esc_html_e('Configure which post types should be automatically synced to your AI BotKit knowledge base.', 'ai-botkit-for-lead-generation'); ?></p>
            </div>
        </div>

        <div class="ai-botkit-card">
            <div class="ai-botkit-card-header">
                <h3><?php esc_html_e('Post Type Sync Configuration', 'ai-botkit-for-lead-generation'); ?></h3>
                <p><?php esc_html_e('Select which post types should be automatically synced when they are updated.', 'ai-botkit-for-lead-generation'); ?></p>
            </div>
            
            <div class="ai-botkit-card-body">
                <form method="post" action="">
                    <?php wp_nonce_field('ai_botkit_post_sync_settings', 'ai_botkit_post_sync_nonce'); ?>
                    
                    <div class="ai-botkit-form-group">
                        <label class="ai-botkit-label">
                            <input type="checkbox" name="auto_sync" value="1" <?php checked($auto_sync, 1); ?> />
                            <?php esc_html_e('Enable automatic content sync', 'ai-botkit-for-lead-generation'); ?>
                        </label>
                        <p class="ai-botkit-help-text"><?php esc_html_e('When enabled, selected post types will be automatically synced to your knowledge base when they are updated.', 'ai-botkit-for-lead-generation'); ?></p>
                    </div>

                    <div class="ai-botkit-form-group">
                        <label class="ai-botkit-label"><?php esc_html_e('Select Post Types to Sync:', 'ai-botkit-for-lead-generation'); ?></label>
                        <div class="ai-botkit-post-types-grid">
                            <?php foreach ($available_post_types as $post_type): ?>
                                <div class="ai-botkit-post-type-item">
                                    <label class="ai-botkit-post-type-label">
                                        <input type="checkbox" 
                                               name="enabled_post_types[]" 
                                               value="<?php echo esc_attr($post_type['name']); ?>"
                                               <?php checked(in_array($post_type['name'], $enabled_post_types)); ?> />
                                        <div class="ai-botkit-post-type-info">
                                            <span class="ai-botkit-post-type-name"><?php echo esc_html($post_type['label']); ?></span>
                                            <span class="ai-botkit-post-type-slug"><?php echo esc_html($post_type['name']); ?></span>
                                            <?php if (!empty($post_type['description'])): ?>
                                                <span class="ai-botkit-post-type-desc"><?php echo esc_html($post_type['description']); ?></span>
                                            <?php endif; ?>
                                        </div>
                                    </label>
                                </div>
                            <?php endforeach; ?>
                        </div>
                    </div>

                    <div class="ai-botkit-card-footer">
                        <button type="submit" name="ai_botkit_post_sync_settings" class="ai-botkit-btn-primary">
                            <?php esc_html_e('Save Settings', 'ai-botkit-for-lead-generation'); ?>
                        </button>
                    </div>
                </form>
            </div>
        </div>

        <div class="ai-botkit-card">
            <div class="ai-botkit-card-header">
                <h3><?php esc_html_e('Sync Information', 'ai-botkit-for-lead-generation'); ?></h3>
            </div>
            <div class="ai-botkit-card-body">
                <div class="ai-botkit-info-grid">
                    <div class="ai-botkit-info-item">
                        <h4><?php esc_html_e('How it works:', 'ai-botkit-for-lead-generation'); ?></h4>
                        <ul>
                            <li><?php esc_html_e('The sync will only be applicable for posts that are added in chatbot knowledge base.', 'ai-botkit-for-lead-generation'); ?></li>
                            <li><?php esc_html_e('When you update a post of the selected types, it will be automatically synced to your AI BotKit knowledge base.', 'ai-botkit-for-lead-generation'); ?></li>
                            <li><?php esc_html_e('Only published posts are synced to ensure your knowledge base stays current.', 'ai-botkit-for-lead-generation'); ?></li>
                            <li><?php esc_html_e('Deleted posts will be removed from your knowledge base automatically.', 'ai-botkit-for-lead-generation'); ?></li>
                            <li><?php esc_html_e('The sync happens in the background and won\'t slow down your website.', 'ai-botkit-for-lead-generation'); ?></li>
                        </ul>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
