<?php

if (!defined('ABSPATH')) {
    exit;
}

/**
 * The admin-specific functionality of the plugin.
 *
 * @since      1.6.5
 */
class NewCodeByte_Chatbot_Admin {

    /**
     * Register the hooks related to the admin area functionality.
     *
     * @since    1.6.5
     */
    public function add_hooks() {
        add_action('admin_menu', [$this, 'add_admin_page']);
        add_action('admin_enqueue_scripts', [$this, 'enqueue_scripts']);
        add_action('admin_enqueue_scripts', [$this, 'add_admin_menu_tooltip']);
    }

    /**
     * Add the admin menu page for the plugin.
     *
     * @since    1.6.5
     */
    public function add_admin_page() {
        $unread_count = $this->get_unread_count();
        $menu_title = __('Chatbot', NCB_CHATBOT_TEXT_DOMAIN);

        if ($unread_count > 0) {
            $badge = sprintf(' <span class="awaiting-mod"><span class="pending-count">%d</span></span>', $unread_count);
            $menu_title .= $badge;
        }

        add_menu_page(
            __('Chatbot Settings', NCB_CHATBOT_TEXT_DOMAIN),
            $menu_title,
            'manage_options',
            'newcodebyte-chatbot-settings',
            [$this, 'render_settings_page'],
            'dashicons-admin-generic',
            81
        );
    }

    /**
     * Render the settings page view.
     *
     * @since    1.6.5
     */
    public function render_settings_page() {
        require_once NCB_CHATBOT_PLUGIN_DIR . 'admin/views/settings-page.php';
    }

    /**
     * Enqueue scripts and styles for the admin area.
     *
     * @param string $hook The current admin page.
     * @since    1.6.5
     */
    public function enqueue_scripts($hook) {
        if ('toplevel_page_newcodebyte-chatbot-settings' !== $hook) {
            return;
        }
        wp_enqueue_style('chatbot-admin-style', NCB_CHATBOT_PLUGIN_URL . 'assets/css/chatbot-admin.css', [], NCB_CHATBOT_VERSION);
        wp_enqueue_media();
        wp_enqueue_style('wp-color-picker');
        wp_enqueue_script('chatbot-admin-js', NCB_CHATBOT_PLUGIN_URL . 'assets/js/chatbot-admin.js', ['jquery', 'wp-color-picker'], NCB_CHATBOT_VERSION, true);

        wp_localize_script('chatbot-admin-js', 'chatbot_admin_vars', [
            'ajaxurl' => admin_url('admin-ajax.php'),
            'get_messages_nonce' => wp_create_nonce('newcodebyte_chatbot_admin_nonce'),
            'chatbot_delete_nonce' => wp_create_nonce('newcodebyte_chatbot_delete_messages'),
            'chatbot_export_nonce' => wp_create_nonce('newcodebyte_chatbot_export_messages'),
            'delete_confirmation' => __('Are you sure you want to delete all messages?', NCB_CHATBOT_TEXT_DOMAIN),
            'delete_error' => __('Error while deleting messages.', NCB_CHATBOT_TEXT_DOMAIN),
            'export_format_selection' => __('Select an export format.', NCB_CHATBOT_TEXT_DOMAIN),
            'loading_messages' => __('Loading messages...', NCB_CHATBOT_TEXT_DOMAIN),
        ]);
    }

    /**
     * Add a tooltip to the admin menu item if there are unread messages.
     *
     * @since    1.6.5
     */
    public function add_admin_menu_tooltip() {
        $unread_count = $this->get_unread_count();
        if ($unread_count > 0) {
            $tooltip_text = sprintf(
                esc_attr(_n('You have %d unread conversation.', 'You have %d unread conversations.', $unread_count, NCB_CHATBOT_TEXT_DOMAIN)),
                $unread_count
            );
            $script = "jQuery(document).ready(function($) { $('#toplevel_page_newcodebyte-chatbot-settings > a').attr('title', '" . $tooltip_text . "'); });";
            wp_add_inline_script('jquery', $script);
        }
    }

    /**
     * Get the number of unread conversations from the database.
     *
     * @return int The number of unread conversations.
     * @since    1.6.5
     */
    public function get_unread_count() {
        $cache_key = 'newcodebyte_chatbot_unread_count';
        $unread_count = get_transient($cache_key);

        if (false === $unread_count) {
            global $wpdb;
            $table_name = $wpdb->prefix . 'newcodebyte_chatbot_messages';
            $unread_count = $wpdb->get_var("SELECT COUNT(DISTINCT conversation_id) FROM {$table_name} WHERE is_read = 0");
            set_transient($cache_key, $unread_count, 5 * MINUTE_IN_SECONDS);
        }
        return (int) $unread_count;
    }
}