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

use AI_BotKit\Integration\API_Handler;

$api_handler = new API_Handler();

global $wpdb;

global $ai_botkit_url;

// Get current page and items per page
$current_page = isset($_GET['paged']) ? max(1, intval($_GET['paged'])) : 1;
$items_per_page = 4;
$offset = ($current_page - 1) * $items_per_page;

$create_chatbot = isset($_GET['create']) ? true : false;

// Get chatbots
$chatbots_data = $api_handler->send_get_request('chatbots');
$chatbots = isset($chatbots_data['data']) ? $chatbots_data['data'] : [];
$total_chatbots = isset($chatbots_data['total']) ? $chatbots_data['total'] : 0;
$total_pages = ceil($total_chatbots / $items_per_page);

$sitewide_chatbot_id = get_option('ai_botkit_chatbot_sitewide_enabled', 0);

$nonce = wp_create_nonce('ai_botkit_chatbots');
?>
<div class="ai-botkit-dashboard-wrapper">
    <div class="ai-botkit-dashboard-container">
        <div class="ai-botkit-dashboard-header">
            <h1 class="ai-botkit-dashboard-title"><?php esc_html_e('My Chatbots', 'ai-botkit-for-lead-generation'); ?></h1>

            <a href="<?php echo esc_url( $ai_botkit_url . '/dashboard/my-bots/create' ); ?>" target="_blank" class="ai-botkit-create-btn">
                <i class="ti ti-plus"></i> <?php esc_html_e('Create New Bot', 'ai-botkit-for-lead-generation'); ?>
            </a>
        </div>

        <div class="ai-botkit-chatbot-grid">

            <?php
            if ( empty($chatbots) ) {
                ?>
                <!-- If there are no bots -->
                <div class="ai-botkit-empty-state">
                    <p class="ai-botkit-empty-text">
                    <?php esc_html_e('You don\'t have any chatbots yet.', 'ai-botkit-for-lead-generation'); ?>
                    </p>
                </div>
                <?php
            } else {
                ?>
                <!-- Else, if there are bots -->
                <div class="ai-botkit-grid-wrapper">
                    <?php foreach ($chatbots as $chatbot) {
                        $style = $chatbot['style'];
                        $messages_template = $chatbot['messagesTemplate'];

                        ?>
                    <div class="ai-botkit-chatbot-card">
                        <div class="ai-botkit-chatbot-header">
                            <div class="ai-botkit-chatbot-avatar" style="background-color: <?php echo $style['primaryColor']; ?>;">
                                <img src="<?php echo esc_url($ai_botkit_url . $style['botAvatar']); ?>" alt="Chatbot avatar" class="ai-botkit-avatar-img" />        
                            </div>
                            <div class="ai-botkit-chatbot-details">
                                <div class="ai-botkit-chatbot-badges">
                                    <h3 class="ai-botkit-chatbot-title"><?php echo esc_html($chatbot['name']); ?></h3>
                                </div>
                                <div class="ai-botkit-chatbot-badges">
                                    <span class="ai-botkit-badge ai-botkit-badge-status <?php echo 1 == $chatbot['active'] ? 'ai-botkit-status-active' : 'ai-botkit-status-inactive'; ?>"><?php echo 1 == $chatbot['active'] ? 'Active' : 'Inactive'; ?></span>
                                    <?php if( ($chatbot['messagesTemplate']['namespace']) === (get_option('ai_botkit_chatbot_sitewide_enabled', 0)) ) { ?>
                                        <span class="ai-botkit-badge ai-botkit-badge-sitewide">Sitewide</span>
                                    <?php } ?>
                                    <span class="ai-botkit-badge ai-botkit-badge-outline"><?php echo esc_html($messages_template['tone']); ?></span>
                                </div>
                                <div class="ai-botkit-chatbot-content">
                                    <div class="ai-botkit-chatbot-info">
                                        <div>
                                            <p class="ai-botkit-info-label"><?php esc_html_e('Messages', 'ai-botkit-for-lead-generation'); ?></p>
                                            <p class="ai-botkit-info-value"><?php echo esc_html($chatbot['totalMessages']); ?></p>
                                        </div>
                                    </div>
                                    <div class="ai-botkit-chatbot-info">
                                        <div>
                                            <p class="ai-botkit-info-label"><?php esc_html_e('Sessions', 'ai-botkit-for-lead-generation'); ?></p>
                                            <p class="ai-botkit-info-value"><?php echo esc_html($chatbot['totalConversations']); ?></p>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="ai-botkit-chatbot-footer">
                            <a href="<?php echo esc_url( $ai_botkit_url . '/dashboard/my-bots/' . $chatbot['id'] ); ?>" target="_blank" class="ai-botkit-btn-outline ai-masters-show-title" data-title="<?php esc_html_e('Edit Bot', 'ai-botkit-for-lead-generation'); ?>" data-chatbot-id="<?php echo esc_attr($chatbot['id']); ?>">
                                <i class="ti ti-edit"></i>
                            </a>
                            <a class="ai-botkit-btn-outline ai-botkit-view-chat" href="<?php echo esc_url(admin_url('admin.php?page=ai-botkit&tab=chatbots&bot_id=' . $chatbot['id'] . '&nonce=' . $nonce)); ?>">
                                <i class="ti ti-users"></i> <?php esc_html_e('View Conversations', 'ai-botkit-for-lead-generation'); ?>
                            </a>
                            <div class="ai-botkit-copy-code-wrapper">
                                <div class="ai-botkit-shortcode-wrapper" style="display: none;">
                                    <button class="ai-botkit-copy-shortcode" data-chatbot-id="<?php echo esc_attr($chatbot['id']); ?>">
                                        <i class="ti ti-copy"></i> <?php esc_html_e('Copy Shortcode', 'ai-botkit-for-lead-generation'); ?>
                                    </button>
                                    <button class="ai-botkit-copy-widget-code" data-chatbot-id="<?php echo esc_attr($chatbot['id']); ?>">
                                        <i class="ti ti-copy"></i> <?php esc_html_e('Copy Widget Code', 'ai-botkit-for-lead-generation'); ?>
                                    </button>
                                </div>
                                <button class="ai-botkit-btn-outline ai-botkit-copy-code" data-chatbot-id="<?php echo esc_attr($chatbot['messagesTemplate']['namespace']); ?>" <?php echo 1 == $chatbot['active'] ? '' : 'disabled style="cursor: not-allowed;"' ?>>
                                    <i class="ti ti-plus"></i> <?php esc_html_e('Activate', 'ai-botkit-for-lead-generation'); ?>
                                </button>
                            </div>
                            <a href="<?php echo esc_url( $ai_botkit_url . '/dashboard/my-bots/' ); ?>" target="_blank" class="ai-botkit-btn-outline ai-botkit-delete-bot ai-masters-show-title" data-title="<?php esc_html_e('Delete Bot', 'ai-botkit-for-lead-generation'); ?>" data-chatbot-id="<?php echo esc_attr($chatbot['id']); ?>">
                                <i class="ti ti-trash"></i>
                            </a>
                        </div>
                    </div>
                    <?php } ?>
                </div>
                <?php
            }
            ?>

            <!-- Pagination controls -->
            <?php if ( $total_pages > 1 ) { ?>
                <div class="ai-botkit-pagination" id="ai-botkit-pagination">
                    <a class="ai-botkit-btn-outline" href="<?php echo esc_url( add_query_arg(
                        array(
                            'page'    => 'ai-botkit',
                            'tab'     => 'chatbots',
                            'bot_id'  => intval( $bot_id ),
                            'paged'   => max( 1, $current_page - 1 ),
                            'nonce'   => $nonce,
                        ),
                        admin_url( 'admin.php' )
                    ) ); ?>">
                        <i class="ti ti-chevron-left"></i>
                    </a>

                    <span id="ai-botkit-page-info">
                        <?php
                            echo esc_html__( 'Page', 'ai-botkit-for-lead-generation' ) . ' ' .
                                esc_html( $current_page ) . ' ' .
                                esc_html__( 'of', 'ai-botkit-for-lead-generation' ) . ' ' .
                                esc_html( $total_pages );
                        ?>
                    </span>

                    <a class="ai-botkit-btn-outline" href="<?php echo esc_url( add_query_arg(
                        array(
                            'page'    => 'ai-botkit',
                            'tab'     => 'chatbots',
                            'bot_id'  => intval( $bot_id ),
                            'paged'   => min( $total_pages, $current_page + 1 ),
                            'nonce'   => $nonce,
                        ),
                        admin_url( 'admin.php' )
                    ) ); ?>">
                        <i class="ti ti-chevron-right"></i>
                    </a>
                </div>
            <?php } ?>
        </div>

        <!-- Support email and contact details -->
        <div class="ai-botkit-support-container">
            <p><?php esc_html_e('Need help? Feel Free to reach at ', 'ai-botkit-for-lead-generation'); ?> <a href="mailto:support@aibotkit.io">support@aibotkit.io</a> or <a href="https://aibotkit.io/contact-us/" target="_blank">contact us</a></p>
        </div>
    </div>
    <!-- Confirm Delete Modal -->
    <div id="ai-botkit-confirm-delete-chatbot-modal" class="ai-botkit-modal-overlay">
        <div class="ai-botkit-kb-modal">
            <div class="ai-botkit-modal-header">
                <h3><?php esc_html_e('Confirm Deletion', 'ai-botkit-for-lead-generation'); ?></h3>
                <p><?php esc_html_e('Are you sure you want to delete this chatbot? This action cannot be undone.', 'ai-botkit-for-lead-generation'); ?></p>
            </div>
            <div class="ai-botkit-modal-footer">
                <button id="ai-botkit-cancel-delete-chatbot" class="ai-botkit-btn-outline"><?php esc_html_e('Cancel', 'ai-botkit-for-lead-generation'); ?></button>
                <button id="ai-botkit-confirm-delete-chatbot" data-chatbot-id="" class="ai-botkit-btn ai-botkit-btn-danger"><?php esc_html_e('Delete', 'ai-botkit-for-lead-generation'); ?></button>
            </div>
        </div>
    </div>
</div>
<div id="ai-botkit-add-to-website-chatbot-modal" class="ai-botkit-modal-overlay">
    <div class="ai-botkit-kb-modal">
        <div class="ai-botkit-modal-header">
            <h3 class="success-message"><?php esc_html_e('Add to Website', 'ai-botkit-for-lead-generation'); ?></h3>
            <i id="ai-botkit-close-add-to-website-chatbot-modal" class="ti ti-x" style="font-size: 1.5rem;"></i>
        </div>
        <div class="ai-botkit-modal-content">
            <div id="ai-botkit-activation-notice" class="ai-botkit-notice" style="margin-bottom: 1rem; display: none;"></div>
            <div class="ai-botkit-modal-activate-wrapper">
                <div class="ai-botkit-form-toggle">
                    <div>
                        <label for="enable_chatbot_sitewide" class="ai-botkit-full-site-label"><?php esc_html_e('Add Chatbot to full site', 'ai-botkit-for-lead-generation'); ?></label>
                    </div>
                    <label class="ai-botkit-switch">
                        <input type="hidden" id="chatbot_id" value="0">
                        <input type="hidden" id="sitewide_chatbot_id" value="<?php echo esc_attr($sitewide_chatbot_id); ?>">
                        <input type="checkbox" id="enable_chatbot_sitewide" name="enable_chatbot_sitewide">
                        <span class="ai-botkit-slider"></span>
                    </label>
                </div>
                <p class="ai-botkit-or-label"><?php esc_html_e('OR', 'ai-botkit-for-lead-generation'); ?></p>
                <div class="ai-botkit-search-pages-container">
                    <input type="text" id="ai-botkit-search-pages" placeholder="Search page">
                    <div id="ai-botkit-pages-list">
                        <!-- Pages will be loaded here via AJAX -->
                    </div>
                </div>
                <p class="ai-botkit-or-label"><?php esc_html_e('OR', 'ai-botkit-for-lead-generation'); ?></p>
                <div class="ai-botkit-search-pages-container">
                    <p class="ai-botkit-help-text" style="margin-bottom: 0.5rem; font-weight: 600;"><?php esc_html_e('Enable on post types:', 'ai-botkit-for-lead-generation'); ?></p>
                    <div id="ai-botkit-post-types-list">
                        <!-- Post types will be loaded here via AJAX -->
                    </div>
                </div>
            </div>
            <div class="ai-botkit-shortcode-container">
                <label class="ai-botkit-label"><?php esc_html_e('Widget code', 'ai-botkit-for-lead-generation'); ?></label>
                <div class="ai-botkit-widget-code-container">
                    <input readonly type="text" class="widget-code-text" id="widget-code-text" value="[ai-botkit-widget id='20']">
                    <button class="ai-botkit-btn ai-botkit-copy-shortcode-btn ai-botkit-copy-widget-code" id="widget-code-btn" data-chatbot-id="1"><i class="ti ti-copy"></i> <?php esc_html_e('Copy', 'ai-botkit-for-lead-generation'); ?></button>
                </div>
            </div>
            <div class="ai-botkit-shortcode-container">
                <label class="ai-botkit-label"><?php esc_html_e('Share link', 'ai-botkit-for-lead-generation'); ?></label>
                <div class="ai-botkit-widget-code-container">
                    <input readonly type="text" class="share-link-text" id="share-link-text" value="<?php echo esc_url( $ai_botkit_url . '/chatbot/20' ); ?>">
                    <button class="ai-botkit-btn ai-botkit-copy-shortcode-btn ai-botkit-copy-share-link" id="share-link-btn" data-chatbot-id="1"><i class="ti ti-copy"></i> <?php esc_html_e('Copy', 'ai-botkit-for-lead-generation'); ?></button>
                </div>
            </div>
        </div>
    </div>
</div>

