<?php
const WEBIEN_AI_VERSION = '0.2.7.3';
/**
 * Plugin Name: Webien AI
 * Plugin URI: https://webien.io
 * Author: Webien
 * Description: AI chat based on site content.
 * Author URI: https://webien.io
 * Version: 0.2.7.3
 */

define('WEBIEN_AI_PLUGIN_PATH', __DIR__);
define('WEBIEN_AI_PLUGIN_URI', plugin_dir_url(__FILE__));


if (! defined('ABSPATH')) {
    exit; // Exit if accessed directly.
}


if (file_exists(WEBIEN_AI_PLUGIN_PATH . '/vendor/autoload.php')) {
    require_once WEBIEN_AI_PLUGIN_PATH . '/vendor/autoload.php';
}

// Classes
add_action('init', function () {
    // Elementor
    (new \Webien\AI\Elementor\Elementor())->init();
}, 10, 0);


// Dashboard
add_action('acf/init', function () {
    // Dashboard
    (new \Webien\AI\Dashboard\Dashboard())->addOptionPage();
});

// Add markup to dom
add_action('wp_footer', function () {
    if(get_field('webien_ai_show_on_site', 'option')) {
        echo '<div class="webien-ai open-button"></div>';
    }
});


add_action('wp_enqueue_scripts', function () {

    $ver = WEBIEN_AI_VERSION;

    // First check for user limitations
    $limitUsers = get_field('webien_ai_limit_user', 'option');
    if($limitUsers && !in_array(get_current_user_id(), $limitUsers)) {
        return;
    }

    wp_enqueue_style('webien-ai',  plugin_dir_url( __FILE__ ) . 'dist/style.css' , [], $ver);
    wp_enqueue_script('webien-ai',  plugin_dir_url( __FILE__ ) . 'dist/app.js' , [], $ver, true);
    wp_localize_script( 'webien-ai', 'webienAiHelpers', [ 'ajaxurl' => admin_url( 'admin-ajax.php' )]);


    // Add inline script before above handle
    $settings = [
        'systemPrompt' => get_field('webien_ai_system_prompt', 'option'),
        'firstMsg' => get_field('webien_ai_first_msg', 'option'),
        //'file' => get_option('webien_ai_file'),
        'showInfo' => get_field('webien_ai_show_info', 'option'),
        'apiKey' => get_field('webien_ai_api_key', 'option'),
        'model' => get_field('webien_ai_model', 'option'),
        'assistant' => json_decode(get_option('webien_ai_assistant') ?? []),
    ];
    wp_add_inline_script('webien-ai', 'const webienAi = ' . json_encode($settings) . ';', 'before');

});


// Admin enqueues
function webien_ai_enqueue_admin() {
    wp_enqueue_style('webien-ai-admin',  plugin_dir_url( __FILE__ ) . 'dist/admin.css', [], WEBIEN_AI_VERSION);
}
add_action( 'admin_enqueue_scripts', 'webien_ai_enqueue_admin' );



// ACF Filter for selecting post types
add_filter('acf/load_field/name=webien_ai_select_posttype', 'acf_load_post_types');
//add_filter('acf/load_field/name=webien_ai_select_taxonomy', 'acf_load_taxonomies');

function acf_load_post_types($field)
{
    foreach ( get_post_types( '', 'objects' ) as $post_type ) {
        $field['choices'][$post_type->name] = $post_type->label . ' (' . $post_type->name . ')';
    }
    return $field;
}


function acf_load_taxonomies($field)
{
    foreach ( get_taxonomies([], 'objects') as $taxonomy ) {
        $field['choices'][$taxonomy->name] = $taxonomy->label . ' (' . $taxonomy->name . ')';
    }
    return $field;
}



add_action('wp_ajax_webien_ai_generate', function () {
    (new \Webien\AI\Dashboard\Dashboard())->updateAssistant(true);
});
add_action('wp_ajax_nopriv_webien_ai_generate', function () {
    (new \Webien\AI\Dashboard\Dashboard())->updateAssistant(true);
});
add_action('wp_ajax_webien_ai_update', function () {
    (new \Webien\AI\Dashboard\Dashboard())->updateAssistant();
});




if (defined('WP_CLI') && WP_CLI) {
    require_once WEBIEN_AI_PLUGIN_PATH . '/src/Dashboard/CLI.php';
}

// Detect plugin updates
if(is_admin() || (defined('WP_CLI') && WP_CLI)) {

    include WEBIEN_AI_PLUGIN_PATH . '/src/PluginUpdater.php';

    $version = WEBIEN_AI_VERSION;
    $slug = 'webien-ai';
    $indexFile = $slug . '/index.php';

    $info = 'https://plugins.webien.io/plugins/webien-ai/info.php?key=xxx';
    $updater = new \Webien\AI\PluginUpdater($info, $indexFile, $slug, $version);
}


// Add settings link in plugin list
add_filter( 'plugin_action_links_webien-ai/index.php', function( $links ) {
    $url = esc_url( add_query_arg(
        'page',
        'webien-ai',
        get_admin_url() . 'admin.php'
    ) );
    $settings_link = "<a href='$url'>" . __( 'Settings', 'webien' ) . '</a>';
    array_push(
        $links,
        $settings_link
    );
    return $links;
} );