<?php
/**
 * Plugin Name:       Sabriel AI
 * Plugin URI:        https://sabrielagency.com/sabriel-ai/
 * Description:       AI chat assistant for your site: answers customers using your own price list, opening hours and tone of voice. Bring your own key for Gemini, OpenAI or Claude.
 * Version:           1.0.27
 * Requires at least: 6.0
 * Tested up to:      7.0
 * Requires PHP:      7.4
 * Author:            Sabriel Agency
 * Author URI:        https://sabrielagency.com
 * License:           GPL v2 or later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       sabriel-ai
 * Domain Path:       /languages
 */

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

define( 'SABRIEL_AI_VERSION', '1.0.27' );

define( 'SABRIEL_AI_FILE', __FILE__ );
define( 'SABRIEL_AI_DIR', plugin_dir_path( __FILE__ ) );
define( 'SABRIEL_AI_URL', plugin_dir_url( __FILE__ ) );
define( 'SABRIEL_AI_BASENAME', plugin_basename( __FILE__ ) );

require_once SABRIEL_AI_DIR . 'includes/class-sabriel-base.php';
require_once SABRIEL_AI_DIR . 'includes/class-sabriel-loader.php';
require_once SABRIEL_AI_DIR . 'includes/class-sabriel-helpers.php';
require_once SABRIEL_AI_DIR . 'includes/class-sabriel-crypto.php';
require_once SABRIEL_AI_DIR . 'includes/class-sabriel-orari.php';
require_once SABRIEL_AI_DIR . 'includes/modules/class-sabriel-module.php';
require_once SABRIEL_AI_DIR . 'includes/modules/class-module-dashboard.php';
require_once SABRIEL_AI_DIR . 'includes/modules/class-module-core.php';
require_once SABRIEL_AI_DIR . 'includes/modules/class-module-widget.php';
require_once SABRIEL_AI_DIR . 'includes/modules/class-module-traffico.php';
require_once SABRIEL_AI_DIR . 'includes/modules/class-module-status.php';
require_once SABRIEL_AI_DIR . 'includes/modules/class-module-settings.php';
require_once SABRIEL_AI_DIR . 'includes/modules/class-module-prezzi.php';
require_once SABRIEL_AI_DIR . 'includes/modules/class-module-branding.php';
require_once SABRIEL_AI_DIR . 'includes/ai-providers/class-sabriel-ai-provider.php';
require_once SABRIEL_AI_DIR . 'includes/ai-providers/class-sabriel-ai-provider-gemini.php';
require_once SABRIEL_AI_DIR . 'includes/ai-providers/class-sabriel-ai-provider-openai.php';
require_once SABRIEL_AI_DIR . 'includes/ai-providers/class-sabriel-ai-provider-claude.php';
require_once SABRIEL_AI_DIR . 'includes/modules/class-module-ai.php';
require_once SABRIEL_AI_DIR . 'includes/modules/class-module-contatto.php';
require_once SABRIEL_AI_DIR . 'includes/modules/class-module-chat.php';
require_once SABRIEL_AI_DIR . 'includes/modules/class-module-documentazione.php';

add_action( 'init', 'sabriel_ai_load_textdomain' );
function sabriel_ai_load_textdomain() {
	load_plugin_textdomain( 'sabriel-ai', false, dirname( SABRIEL_AI_BASENAME ) . '/languages' );
}

add_action( 'change_locale', function () {
	unload_textdomain( 'sabriel-ai' );
	load_plugin_textdomain( 'sabriel-ai', false, dirname( SABRIEL_AI_BASENAME ) . '/languages' );
} );

register_activation_hook( __FILE__, 'sabriel_ai_activate' );
function sabriel_ai_activate() {

	if ( false === get_option( Sabriel_Base::OPTION_KEY ) ) {
		add_option( Sabriel_Base::OPTION_KEY, Sabriel_Base::get_defaults() );
	}

	set_transient( 'sabriel_ai_activation_redirect', true, DAY_IN_SECONDS );
}

register_deactivation_hook( __FILE__, 'sabriel_ai_deactivate' );
function sabriel_ai_deactivate() {

	wp_clear_scheduled_hook( Sabriel_Module_Core::CLEANUP_HOOK );
	wp_clear_scheduled_hook( Sabriel_Module_Core::CLEANUP_HOOK_LEGACY );

	wp_clear_scheduled_hook( 'sab_controllo_followup_lead' );
	wp_clear_scheduled_hook( 'sab_rinfresca_conoscenza' );
}

add_action( 'admin_init', 'sabriel_ai_maybe_redirect_to_wizard' );
function sabriel_ai_maybe_redirect_to_wizard() {
	if ( ! get_transient( 'sabriel_ai_activation_redirect' ) ) {
		return;
	}

	if ( ! current_user_can( 'manage_options' ) || isset( $_GET['activate-multi'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- semplice lettura della pagina corrente, nessun dato viene salvato.
		return;
	}
	if ( wp_doing_ajax() || wp_doing_cron() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) {
		return;
	}

	delete_transient( 'sabriel_ai_activation_redirect' );

	wp_safe_redirect( admin_url( 'admin.php?page=sabriel-ai-wizard' ) );
	exit;
}

add_action( 'admin_menu', function() {
	global $submenu;
	if ( empty( $submenu['sabriel-ai'] ) ) {
		return;
	}
	$ordine_voluto = [

		'sabriel-ai',
		'sabriel-ai-wizard',
		'sabriel-ai-motore',
		'sabriel-ai-branding',
		'sabriel-ai-settings',
		'sabriel-ai-status',
		'sabriel-ai-docs',
		'sabriel-ai-prezzi',
	];
	$per_slug = [];
	foreach ( $submenu['sabriel-ai'] as $voce ) {
		$per_slug[ $voce[2] ] = $voce;
	}
	$riordinato = [];
	foreach ( $ordine_voluto as $slug ) {
		if ( isset( $per_slug[ $slug ] ) ) {
			$riordinato[] = $per_slug[ $slug ];
			unset( $per_slug[ $slug ] );
		}
	}

	foreach ( $per_slug as $voce ) {
		$riordinato[] = $voce;
	}
	$submenu['sabriel-ai'] = $riordinato; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- si riordina solo il sottomenu di questo plugin, non si tocca nient'altro.
}, 999 );

add_action( 'admin_enqueue_scripts', 'sabriel_ai_stili_admin' );
function sabriel_ai_stili_admin() {
	wp_register_style( 'sabriel-ai-admin', false, [], SABRIEL_AI_VERSION );
	wp_enqueue_style( 'sabriel-ai-admin' );

	$css = '#adminmenu #toplevel_page_sabriel-ai ul.wp-submenu{display:flex;flex-direction:column;}'
		. '#adminmenu #toplevel_page_sabriel-ai ul.wp-submenu>li.wp-submenu-head{order:-2;}'
		. '#adminmenu #toplevel_page_sabriel-ai ul.wp-submenu>li:has(>a[href*="page=sabriel-ai-wizard"]){order:-1;}';


	wp_add_inline_style( 'sabriel-ai-admin', $css );
}

function sabriel_ai_run() {
	$loader = new Sabriel_Loader();

	$loader->register_module( new Sabriel_Module_Dashboard() );
	$loader->register_module( new Sabriel_Module_AI() );
	$loader->register_module( new Sabriel_Module_Branding() );
	$loader->register_module( new Sabriel_Module_Settings() );
	$loader->register_module( new Sabriel_Module_Prezzi() );
	$loader->register_module( new Sabriel_Module_Status() );
	$loader->register_module( new Sabriel_Module_Core() );
	$loader->register_module( new Sabriel_Module_Widget() );
	$loader->register_module( new Sabriel_Module_Traffico() );
	$loader->register_module( new Sabriel_Module_Contatto() );
	$loader->register_module( new Sabriel_Module_Chat() );
	$loader->register_module( new Sabriel_Module_Documentazione() );

	$loader->run();
}
add_action( 'switch_blog', [ 'Sabriel_Base', 'svuota_cache' ] );

add_action( 'plugins_loaded', 'sabriel_ai_run' );

add_filter( 'plugin_action_links_' . SABRIEL_AI_BASENAME, function ( $links ) {

	$url_premium = admin_url( 'admin.php?page=sabriel-ai-prezzi' );
	$links['sabriel-premium'] = '<a href="' . esc_url( $url_premium ) . '">' . esc_html__( 'Complete edition', 'sabriel-ai' ) . '</a>';
	return $links;
} );

add_filter( 'plugin_row_meta', function ( $meta, $basename ) {
	if ( SABRIEL_AI_BASENAME === $basename ) {
		$url_docs = admin_url( 'admin.php?page=sabriel-ai-docs' );
		$meta[]   = '<a href="' . esc_url( $url_docs ) . '">' . esc_html__( 'Documentation', 'sabriel-ai' ) . '</a>';
	}
	return $meta;
}, 10, 2 );
