<?php
/**
 * Plugin Activator - Creates database tables and sets default options
 *
 * @package ContentReply_AI
 */

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

class WPSC_Activator {

    /**
     * Run on plugin activation
     */
    public static function activate() {
        self::create_tables();
        self::set_default_options();
    }

    /**
     * Run on plugin deactivation
     */
    public static function deactivate() {
        wp_clear_scheduled_hook( 'wpsc_index_content' );
        wp_clear_scheduled_hook( 'wpsc_auto_index_content' );
    }

    /**
     * Create custom database tables
     */
    private static function create_tables() {
        global $wpdb;

        $charset_collate = $wpdb->get_charset_collate();

        // Content chunks table
        $table_chunks = $wpdb->prefix . 'wpsc_content_chunks';
        $sql_chunks = "CREATE TABLE $table_chunks (
            id bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
            post_id bigint(20) UNSIGNED NOT NULL,
            post_title varchar(255) NOT NULL,
            post_url varchar(500) NOT NULL,
            post_type varchar(50) NOT NULL,
            chunk_content longtext NOT NULL,
            chunk_index int(11) NOT NULL DEFAULT 0,
            indexed_at datetime DEFAULT CURRENT_TIMESTAMP,
            PRIMARY KEY (id),
            KEY post_id (post_id),
            KEY post_type (post_type),
            FULLTEXT KEY chunk_content (chunk_content)
        ) $charset_collate;";

        // Chat conversations table
        $table_chats = $wpdb->prefix . 'wpsc_conversations';
        $sql_chats = "CREATE TABLE $table_chats (
            id bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
            session_id varchar(100) NOT NULL,
            user_message text NOT NULL,
            bot_response text NOT NULL,
            sources text DEFAULT NULL,
            created_at datetime DEFAULT CURRENT_TIMESTAMP,
            ip_address varchar(45) DEFAULT NULL,
            page_url varchar(500) DEFAULT NULL,
            PRIMARY KEY (id),
            KEY session_id (session_id),
            KEY created_at (created_at)
        ) $charset_collate;";

        require_once ABSPATH . 'wp-admin/includes/upgrade.php';
        dbDelta( $sql_chunks );
        dbDelta( $sql_chats );
    }

    /**
     * Set default plugin options
     */
    private static function set_default_options() {
        $defaults = array(
            'wpsc_api_key'          => '',
            'wpsc_ai_provider'      => 'groq',
            'wpsc_ai_model'         => 'llama-3.3-70b-versatile',
            'wpsc_widget_position'  => 'bottom-right',
            'wpsc_primary_color'    => '#2e7d32',
            'wpsc_welcome_message'  => 'Hi! How can I help you today?',
            'wpsc_bot_name'         => 'Support Bot',
            'wpsc_fallback_message' => "I'm not sure about that. Would you like to contact our support team?",
            'wpsc_contact_url'      => '/contact',
            'wpsc_post_types'       => array( 'page', 'post' ),
            'wpsc_excluded_pages'   => array(),
            'wpsc_max_tokens'       => 500,
            'wpsc_show_on_mobile'   => true,
            'wpsc_auto_index'       => 'disabled',
            'wpsc_enabled'          => true,
            'wpsc_demo_mode'        => false,
        );

        foreach ( $defaults as $key => $value ) {
            if ( get_option( $key ) === false ) {
                update_option( $key, $value );
            }
        }
    }
}
