<?php
/**
 * My AI Agent admin page — Agent Hub.
 *
 * Top-level agent type selector with per-agent tab sets.
 * Voice & Text Agent tabs remain unchanged. Blog Agent added behind feature flag.
 *
 * @package    SiteStaffr
 * @subpackage SiteStaffr/admin/views
 * @since      1.17.0
 * @updated    1.21.0 — Agent hub restructure for Blog Agent
 */

if ( ! defined( 'WPINC' ) ) {
    die;
}

// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- template variables scoped to this view file.
$settings = get_option( 'sitestaffr_settings', array() );
$admin = $this;
?>
<div class="wrap sitestaffr-wrap sitestaffr-my-agent-wrap">
    <?php
    $sitestaffr_page_title = __( 'My AI Agent', 'sitestaffr' );
    include SITESTAFFR_PLUGIN_DIR . 'admin/partials/sitestaffr-header.php';
    ?>

    <!-- Agent Section Selector — Knowledge first (the shared brain), then the agents -->
    <div class="sitestaffr-agent-selector" role="radiogroup" aria-label="<?php esc_attr_e( 'AI agent sections', 'sitestaffr' ); ?>">
        <button type="button" class="sitestaffr-agent-type-card is-active" data-agent="knowledge" role="radio" aria-checked="true">
            <span class="dashicons dashicons-database" aria-hidden="true"></span>
            <span class="sitestaffr-agent-type-label"><?php esc_html_e( 'Knowledge', 'sitestaffr' ); ?></span>
        </button>
        <button type="button" class="sitestaffr-agent-type-card" data-agent="voice-text" role="radio" aria-checked="false">
            <span class="dashicons dashicons-microphone" aria-hidden="true"></span>
            <span class="sitestaffr-agent-type-label"><?php esc_html_e( 'Voice & Text Agent', 'sitestaffr' ); ?></span>
        </button>
        <button type="button" class="sitestaffr-agent-type-card" data-agent="blog" role="radio" aria-checked="false">
            <span class="dashicons dashicons-edit-large" aria-hidden="true"></span>
            <span class="sitestaffr-agent-type-label"><?php esc_html_e( 'Blog Agent', 'sitestaffr' ); ?></span>
        </button>
    </div>

    <!-- Knowledge Panel — first tab; the shared brain that powers every agent -->
    <div class="sitestaffr-agent-panel" data-agent="knowledge" id="sitestaffr-knowledge">
        <div class="sitestaffr-agent-tabpanel sitestaffr-settings">
            <section class="sitestaffr-settings-card">
                <header class="sitestaffr-settings-card-header">
                    <h2>
                        <span class="dashicons dashicons-database" aria-hidden="true"></span>
                        <?php esc_html_e( 'Knowledge', 'sitestaffr' ); ?>
                        <span class="sitestaffr-knowledge-hub-badge"><?php esc_html_e( 'Powers all your agents', 'sitestaffr' ); ?></span>
                    </h2>
                </header>
                <div class="sitestaffr-settings-card-body">
                    <p class="description"><?php esc_html_e( 'Select pages and posts your AI learns from. Every agent — voice, text chat, and blog — answers and writes from this shared knowledge.', 'sitestaffr' ); ?></p>
                    <?php
                    $sitestaffr_embedded = true;
                    require SITESTAFFR_PLUGIN_DIR . 'admin/views/admin-knowledge.php';
                    ?>
                </div>
            </section>
        </div>
    </div>

    <!-- Voice & Text Agent Panel -->
    <div class="sitestaffr-agent-panel" data-agent="voice-text" style="display: none;">

        <div class="sitestaffr-agent-tabpanel sitestaffr-settings">

            <!-- Voice Section -->
            <section class="sitestaffr-settings-card">
                <header class="sitestaffr-settings-card-header">
                    <h2>
                        <span class="dashicons dashicons-microphone" aria-hidden="true"></span>
                        <?php esc_html_e( 'AI Voice Agent', 'sitestaffr' ); ?>
                    </h2>
                </header>
                <div class="sitestaffr-settings-card-body">
                    <p><?php esc_html_e( 'Select and preview the AI voice that represents your business.', 'sitestaffr' ); ?></p>
                    <div class="sitestaffr-settings-voice-field">
                        <?php $admin->render_ai_voice_selector(); ?>
                    </div>
                    <?php if ( $admin->is_custom_greeting_allowed_for_plan( $admin->get_current_plan_key() ) ) : ?>
                        <div class="sitestaffr-agent-greeting-field">
                            <label for="custom_greeting">
                                <?php esc_html_e( 'Custom Greeting', 'sitestaffr' ); ?>
                                <span class="sitestaffr-tooltip-trigger" tabindex="0" role="button" aria-label="<?php esc_attr_e( 'Help', 'sitestaffr' ); ?>" data-tooltip="<?php esc_attr_e( 'Set a short custom opening line your AI voice agent uses at the start of each conversation.', 'sitestaffr' ); ?>">
                                    <span class="dashicons dashicons-info" aria-hidden="true"></span>
                                </span>
                                <br><small class="sitestaffr-optional-label">(<?php esc_html_e( 'optional', 'sitestaffr' ); ?>)</small>
                            </label>
                            <?php
                            $admin->render_custom_greeting_field( array(
                                'field_id'    => 'custom_greeting',
                                'placeholder' => 'Hi! Thanks for reaching out to [business name]! How can we help you today?',
                            ) );
                            ?>
                        </div>
                    <?php endif; ?>
                    <div class="sitestaffr-agent-voice-save-wrap">
                        <button type="button" id="sitestaffr-save-agent-voice" class="button button-primary">
                            <span class="dashicons dashicons-saved" aria-hidden="true"></span>
                            <?php esc_html_e( 'Save Voice Settings', 'sitestaffr' ); ?>
                        </button>
                        <span id="sitestaffr-agent-voice-save-status" class="sitestaffr-inline-status"></span>
                    </div>
                </div>
            </section>

            <!-- AI Instructions Section -->
            <?php
            $ai_collect_contact = isset( $settings['ai_collect_contact'] ) ? (bool) $settings['ai_collect_contact'] : true;
            $ai_disregard_personal_info = isset( $settings['ai_disregard_personal_info'] ) ? (bool) $settings['ai_disregard_personal_info'] : false;
            ?>
            <section class="sitestaffr-settings-card">
                <header class="sitestaffr-settings-card-header">
                    <h2>
                        <span class="dashicons dashicons-admin-settings" aria-hidden="true"></span>
                        <?php esc_html_e( 'AI Instructions', 'sitestaffr' ); ?>
                    </h2>
                </header>
                <div class="sitestaffr-settings-card-body">
                    <p><?php esc_html_e( 'Control how your AI agent interacts with visitors.', 'sitestaffr' ); ?></p>

                    <div class="sitestaffr-toggle-row">
                        <label class="sitestaffr-toggle-switch">
                            <input type="checkbox"
                                class="sitestaffr-ai-instruction-toggle"
                                data-setting-key="ai_collect_contact"
                                <?php checked( $ai_collect_contact ); ?> />
                            <span class="sitestaffr-toggle-slider"></span>
                        </label>
                        <div class="sitestaffr-toggle-label">
                            <span><?php esc_html_e( 'Collect visitor contact information', 'sitestaffr' ); ?></span>
                            <span class="sitestaffr-tooltip-trigger" tabindex="0" role="button" aria-label="<?php esc_attr_e( 'Help', 'sitestaffr' ); ?>" data-tooltip="<?php esc_attr_e( 'When enabled, your AI agent will ask visitors for their name, phone, email, and reason for reaching out.', 'sitestaffr' ); ?>">
                                <span class="dashicons dashicons-info" aria-hidden="true"></span>
                            </span>
                            <span class="sitestaffr-toggle-status"></span>
                        </div>
                    </div>

                    <div class="sitestaffr-toggle-row">
                        <label class="sitestaffr-toggle-switch">
                            <input type="checkbox"
                                class="sitestaffr-ai-instruction-toggle"
                                data-setting-key="ai_disregard_personal_info"
                                <?php checked( $ai_disregard_personal_info ); ?> />
                            <span class="sitestaffr-toggle-slider"></span>
                        </label>
                        <div class="sitestaffr-toggle-label">
                            <span><?php esc_html_e( 'Disregard personal details shared by visitors', 'sitestaffr' ); ?></span>
                            <span class="sitestaffr-tooltip-trigger" tabindex="0" role="button" aria-label="<?php esc_attr_e( 'Help', 'sitestaffr' ); ?>" data-tooltip="<?php esc_attr_e( 'When enabled, your AI agent won\'t acknowledge, repeat, or store personal information that visitors voluntarily share during conversation. Recommended for medical portals, legal sites, or any context where visitors may share sensitive details.', 'sitestaffr' ); ?>">
                                <span class="dashicons dashicons-info" aria-hidden="true"></span>
                            </span>
                            <span class="sitestaffr-toggle-status"></span>
                        </div>
                    </div>
                </div>
            </section>

            <?php
            $ci_limit = SiteStaffr_Admin::get_custom_instructions_limit( $admin->get_current_plan_key() );
            if ( $ci_limit > 0 ) :
                $ci_list = isset( $settings['custom_instructions_list'] ) ? $settings['custom_instructions_list'] : array();
            ?>
            <section class="sitestaffr-settings-card">
                <header class="sitestaffr-settings-card-header">
                    <h2>
                        <span class="dashicons dashicons-edit" aria-hidden="true"></span>
                        <?php esc_html_e( 'Custom Instructions', 'sitestaffr' ); ?>
                        <span class="sitestaffr-ci-count-badge"><?php echo count( $ci_list ); ?> / <?php echo (int) $ci_limit; ?></span>
                    </h2>
                    <button type="button" id="sitestaffr-add-ci" class="button button-secondary"
                        <?php disabled( count( $ci_list ) >= $ci_limit ); ?>>
                        <span class="dashicons dashicons-plus-alt2" aria-hidden="true"></span>
                        <?php esc_html_e( 'Add Instruction', 'sitestaffr' ); ?>
                    </button>
                </header>
                <div class="sitestaffr-settings-card-body">
                    <p><?php esc_html_e( 'Add specific instructions for your AI agent. These directives are followed on every conversation.', 'sitestaffr' ); ?></p>

                    <div id="sitestaffr-ci-form" class="sitestaffr-ci-form" style="display:none;">
                        <textarea id="sitestaffr-ci-text" class="sitestaffr-ci-textarea" maxlength="200" rows="3" placeholder="<?php esc_attr_e( 'e.g., After answering the first question, ask for name, email, and phone number', 'sitestaffr' ); ?>"></textarea>
                        <div class="sitestaffr-ci-form-footer">
                            <span class="sitestaffr-ci-char-count"><span class="sitestaffr-ci-char-current">0</span> / 200</span>
                            <div class="sitestaffr-ci-form-actions">
                                <button type="button" id="sitestaffr-ci-cancel" class="button"><?php esc_html_e( 'Cancel', 'sitestaffr' ); ?></button>
                                <button type="button" id="sitestaffr-ci-save" class="button button-primary" disabled><?php esc_html_e( 'Save', 'sitestaffr' ); ?></button>
                            </div>
                        </div>
                        <input type="hidden" id="sitestaffr-ci-edit-id" value="" />
                    </div>

                    <div id="sitestaffr-ci-list">
                        <?php foreach ( $ci_list as $ci ) : ?>
                            <div class="sitestaffr-ci-card" data-ci-id="<?php echo esc_attr( $ci['id'] ); ?>">
                                <label class="sitestaffr-toggle-switch">
                                    <input type="checkbox" class="sitestaffr-ci-toggle" <?php checked( ! empty( $ci['enabled'] ) ); ?> />
                                    <span class="sitestaffr-toggle-slider"></span>
                                </label>
                                <span class="sitestaffr-ci-text"><?php echo esc_html( $ci['text'] ); ?></span>
                                <span class="sitestaffr-ci-char-badge"><?php echo absint( mb_strlen( $ci['text'] ) ); ?> / 200</span>
                                <span class="sitestaffr-ci-status"></span>
                                <div class="sitestaffr-ci-actions">
                                    <button type="button" class="sitestaffr-ci-edit" title="<?php esc_attr_e( 'Edit', 'sitestaffr' ); ?>">
                                        <span class="dashicons dashicons-edit-page"></span>
                                    </button>
                                    <button type="button" class="sitestaffr-ci-delete" title="<?php esc_attr_e( 'Delete', 'sitestaffr' ); ?>">
                                        <span class="dashicons dashicons-trash"></span>
                                    </button>
                                </div>
                            </div>
                        <?php endforeach; ?>
                    </div>
                </div>
            </section>
            <?php endif; ?>

            <!-- Conversation email notifications (this agent's own recipients; the
                 Blog agent has its own separate list on the Blog Settings tab). -->
            <section class="sitestaffr-settings-card sitestaffr-settings-card-email sitestaffr-email-notifications">
                <header class="sitestaffr-settings-card-header">
                    <h2>
                        <span class="dashicons dashicons-email-alt" aria-hidden="true"></span>
                        <?php esc_html_e( 'Conversation Notifications', 'sitestaffr' ); ?>
                    </h2>
                </header>
                <div class="sitestaffr-settings-card-body">
                <?php
                $sitestaffr_notif_settings    = get_option( 'sitestaffr_settings', array() );
                $notifications_enabled  = ! isset( $sitestaffr_notif_settings['email_notifications_enabled'] ) || $sitestaffr_notif_settings['email_notifications_enabled'];
                $notification_email     = ! empty( $sitestaffr_notif_settings['notification_email'] ) ? $sitestaffr_notif_settings['notification_email'] : get_option( 'admin_email' );
                $notification_cc_emails = ! empty( $sitestaffr_notif_settings['notification_cc_emails'] ) ? $sitestaffr_notif_settings['notification_cc_emails'] : '';
                $notification_scope     = SiteStaffr_Notification_Scope::from_settings( $sitestaffr_notif_settings );
                ?>
                <div class="sitestaffr-email-layout">
                    <div class="sitestaffr-email-primary">
                        <div class="sitestaffr-email-row">
                            <div class="sitestaffr-email-row-label">
                                <label for="sitestaffr_email_notifications_enabled"><?php esc_html_e( 'Enable Notifications', 'sitestaffr' ); ?></label>
                                <span class="sitestaffr-tooltip-trigger" tabindex="0" role="button" aria-label="<?php esc_attr_e( 'Help', 'sitestaffr' ); ?>" data-tooltip="<?php esc_attr_e( 'When enabled, SiteStaffr sends an email recap after each completed conversation, including summary and follow-up details, with a button to view full conversation details.', 'sitestaffr' ); ?>">
                                    <span class="dashicons dashicons-info" aria-hidden="true"></span>
                                </span>
                            </div>
                            <div class="sitestaffr-email-row-control">
                                <label class="sitestaffr-email-checkbox-label">
                                    <input type="checkbox" id="sitestaffr_email_notifications_enabled" name="sitestaffr_email_notifications_enabled" value="1" <?php checked( $notifications_enabled ); ?> />
                                    <?php esc_html_e( 'Send email notifications when conversations are completed', 'sitestaffr' ); ?>
                                </label>
                            </div>
                        </div>

                        <div class="sitestaffr-email-row">
                            <div class="sitestaffr-email-row-label">
                                <label for="sitestaffr_notification_scope"><?php esc_html_e( 'Email Me About', 'sitestaffr' ); ?></label>
                                <span class="sitestaffr-tooltip-trigger" tabindex="0" role="button" aria-label="<?php esc_attr_e( 'Help', 'sitestaffr' ); ?>" data-tooltip="<?php esc_attr_e( 'A qualified lead is a conversation where the visitor left their name plus a phone number or email address. Choosing qualified leads only means you still see every conversation in your dashboard — you just are not emailed about the ones with no way to follow up.', 'sitestaffr' ); ?>">
                                    <span class="dashicons dashicons-info" aria-hidden="true"></span>
                                </span>
                            </div>
                            <div class="sitestaffr-email-row-control">
                                <select id="sitestaffr_notification_scope" name="sitestaffr_notification_scope" class="regular-text">
                                    <option value="<?php echo esc_attr( SiteStaffr_Notification_Scope::SCOPE_ALL ); ?>" <?php selected( $notification_scope, SiteStaffr_Notification_Scope::SCOPE_ALL ); ?>>
                                        <?php esc_html_e( 'Every conversation', 'sitestaffr' ); ?>
                                    </option>
                                    <option value="<?php echo esc_attr( SiteStaffr_Notification_Scope::SCOPE_QUALIFIED ); ?>" <?php selected( $notification_scope, SiteStaffr_Notification_Scope::SCOPE_QUALIFIED ); ?>>
                                        <?php esc_html_e( 'Qualified leads only', 'sitestaffr' ); ?>
                                    </option>
                                </select>
                            </div>
                        </div>

                        <div class="sitestaffr-email-row">
                            <div class="sitestaffr-email-row-label">
                                <label for="sitestaffr_notification_email"><?php esc_html_e( 'Notification Email', 'sitestaffr' ); ?></label>
                            </div>
                            <div class="sitestaffr-email-row-control">
                                <input type="email" id="sitestaffr_notification_email" name="sitestaffr_notification_email" value="<?php echo esc_attr( $notification_email ); ?>" class="regular-text" placeholder="your@email.com" />
                            </div>
                        </div>

                        <div class="sitestaffr-email-row">
                            <div class="sitestaffr-email-row-label">
                                <label for="sitestaffr_notification_cc_emails"><?php esc_html_e( 'Additional Emails (CC)', 'sitestaffr' ); ?></label>
                                <span class="sitestaffr-tooltip-trigger" tabindex="0" role="button" aria-label="<?php esc_attr_e( 'Help', 'sitestaffr' ); ?>" data-tooltip="<?php esc_attr_e( 'Separate multiple emails with commas. Up to 5 additional addresses can be CC\'d, and each address will receive new message notifications.', 'sitestaffr' ); ?>">
                                    <span class="dashicons dashicons-info" aria-hidden="true"></span>
                                </span>
                            </div>
                            <div class="sitestaffr-email-row-control">
                                <input type="text" id="sitestaffr_notification_cc_emails" name="sitestaffr_notification_cc_emails" value="<?php echo esc_attr( $notification_cc_emails ); ?>" class="regular-text sitestaffr-notification-cc-input" placeholder="manager@example.com, team@example.com" />
                            </div>
                        </div>

                        <div class="sitestaffr-email-row sitestaffr-email-row-actions">
                            <button type="button" id="sitestaffr-save-notification-settings" class="button button-primary">
                                <span class="dashicons dashicons-saved"></span>
                                <span class="sitestaffr-save-notif-text"><?php esc_html_e( 'Save Notification Settings', 'sitestaffr' ); ?></span>
                            </button>
                            <span id="sitestaffr-notif-save-status" class="sitestaffr-inline-status" style="margin-left: 10px;"></span>
                        </div>
                    </div>

                    <aside class="sitestaffr-email-test-panel">
                        <h3><?php esc_html_e( 'Test Email Notifications', 'sitestaffr' ); ?></h3>
                        <p><?php esc_html_e( 'Send a test email to verify your notification settings are working correctly.', 'sitestaffr' ); ?></p>
                        <button type="button" id="sitestaffr-send-test-email" class="button button-secondary">
                            <span class="dashicons dashicons-email"></span>
                            <?php esc_html_e( 'Send Test Email', 'sitestaffr' ); ?>
                        </button>
                        <span id="sitestaffr-test-email-status" class="sitestaffr-inline-status"></span>
                    </aside>
                </div>
                </div>
            </section>

        </div>
    </div>

    <!-- Blog Agent Panel -->
    <div class="sitestaffr-agent-panel" data-agent="blog" style="display: none;">
        <?php require SITESTAFFR_PLUGIN_DIR . 'admin/views/admin-blog-agent.php'; ?>
    </div>

</div>
<?php // phpcs:enable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound ?>
