<?php
/**
 * Plugin Name: BubblaV AI Chat Widget
 * Plugin URI: https://www.bubblav.com/integrations/wordpress
 * Description: AI chatbot and AI assistant for customer support. One-click installation, no coding required. Provide instant 24/7 support with an AI chatbot that learns from your website content. Perfect for WooCommerce stores.
 * Version: 1.0.6
 * Requires at least: 6.0
 * Requires PHP: 7.4
 * Author: BubblaV
 * License: GPL v2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: bubblav-chat
 * Domain Path: /languages
 *
 * @package BubblaV_Chat
 */

// Prevent direct access.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

// Define plugin constants.
define( 'BUBBLAV_VERSION', '1.0.6' );
define( 'BUBBLAV_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'BUBBLAV_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
define( 'BUBBLAV_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );

// Optional: Override API URL for local development.
// Add this to wp-config.php: define( 'BUBBLAV_API_URL', 'http://localhost:3000' );
if ( ! defined( 'BUBBLAV_API_URL' ) ) {
	define( 'BUBBLAV_API_URL', 'https://www.bubblav.com' );
}

// Register class autoloader.
spl_autoload_register( 'bubblav_autoload' );

/**
 * Autoloader for plugin classes
 *
 * @param string $class_name Class name to autoload.
 * @return void
 */
function bubblav_autoload( $class_name ) {
	// Only autoload Bubblav_ classes.
	if ( strpos( $class_name, 'Bubblav_' ) !== 0 ) {
		return;
	}

	// Convert class name to file name.
	// Example: Bubblav_Core -> includes/class-bubblav-core.php.
	$file = BUBBLAV_PLUGIN_DIR . 'includes/class-' . strtolower( str_replace( '_', '-', $class_name ) ) . '.php';

	if ( file_exists( $file ) ) {
		require_once $file;
	}
}

// Initialize plugin.
add_action( 'plugins_loaded', 'bubblav_init' );

/**
 * Initialize the plugin
 *
 * @return void
 */
function bubblav_init() {
	// Initialize plugin core.
	$bubblav = new Bubblav_Core();
	$bubblav->init();
}

// Register custom order status for returns.
add_action( 'init', 'bubblav_register_return_status' );

/**
 * Register custom order status for return requests
 */
function bubblav_register_return_status() {
	register_post_status( 'wc-return-requested', array(
		'label'                     => _x( 'Return Requested', 'bubblav-chat' ),
		'public'                    => false,
		'exclude_from_search'       => false,
		'show_in_admin_all_list'    => true,
		'show_in_admin_status_list' => true,
		'label_count'               => _n_noop( 'Return Requested <span class="count">(%s)</span>', 'Return Requested <span class="count">(%s)</span>', 'bubblav-chat' ),
	) );
}

// Add custom status to WooCommerce order statuses.
add_filter( 'wc_order_statuses', 'bubblav_add_return_status_to_list' );

/**
 * Add return request status to WooCommerce order status list
 *
 * @param array $order_statuses Existing order statuses.
 * @return array Updated order statuses.
 */
function bubblav_add_return_status_to_list( $order_statuses ) {
	$order_statuses['wc-return-requested'] = _x( 'Return Requested', 'bubblav-chat' );
	return $order_statuses;
}

// Activation hook.
register_activation_hook( __FILE__, 'bubblav_activate' );

/**
 * Plugin activation callback
 *
 * @return void
 */
function bubblav_activate() {
	// Set default options.
	if ( ! get_option( 'bubblav_website_id' ) ) {
		add_option( 'bubblav_website_id', '' );
		add_option( 'bubblav_connection_status', 'disconnected' );
		add_option( 'bubblav_site_url', home_url() );
		add_option( 'bubblav_site_name', get_bloginfo( 'name' ) );
		add_option( 'bubblav_is_woocommerce', class_exists( 'WooCommerce' ) ? '1' : '0' );
	}

	// Flush rewrite rules.
	flush_rewrite_rules();
}

// Deactivation hook.
register_deactivation_hook( __FILE__, 'bubblav_deactivate' );

/**
 * Plugin deactivation callback
 *
 * @return void
 */
function bubblav_deactivate() {
	// Clear any scheduled hooks.
	wp_clear_scheduled_hook( 'bubblav_cleanup_expired_oauth' );

	// Flush rewrite rules.
	flush_rewrite_rules();
}

// Uninstall hook.
register_uninstall_hook( __FILE__, 'bubblav_uninstall' );

/**
 * Plugin uninstall callback
 *
 * @return void
 */
function bubblav_uninstall() {
	// Delete all plugin options.
	delete_option( 'bubblav_website_id' );
	delete_option( 'bubblav_connection_status' );
	delete_option( 'bubblav_site_url' );
	delete_option( 'bubblav_site_name' );
	delete_option( 'bubblav_is_woocommerce' );

	// Delete any transients.
	delete_transient( 'bubblav_settings_errors' );
}

/**
 * Get plugin template part
 *
 * @param string $slug Template slug.
 * @param string $name Optional template name (unused, for compatibility).
 * @return void
 */
function bubblav_get_template_part( $slug, $name = null ) {
	$template = BUBBLAV_PLUGIN_DIR . 'admin/views/' . $slug . '.php';

	// Silence the unused parameter warning.
	isset( $name ); // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter

	if ( file_exists( $template ) ) {
		load_template( $template, false );
	}
}

/**
 * Plugin action links.
 *
 * @param array $links Existing plugin action links.
 * @return array Modified plugin action links.
 */
function bubblav_plugin_action_links( $links ) {
	$settings_link = sprintf(
		'<a href="%s">%s</a>',
		admin_url( 'options-general.php?page=bubblav-chat' ),
		esc_html__( 'Settings', 'bubblav-chat' )
	);

	array_unshift( $links, $settings_link );
	return $links;
}
add_filter( 'plugin_action_links_' . BUBBLAV_PLUGIN_BASENAME, 'bubblav_plugin_action_links' );

/**
 * Get plugin upgrade URL
 *
 * @return string Upgrade URL
 */
function bubblav_get_upgrade_url() {
	return 'https://www.bubblav.com/pricing?utm_source=wordpress-plugin&utm_medium=plugin&utm_campaign=settings';
}

/**
 * Get plugin support URL
 *
 * @return string Support URL
 */
function bubblav_get_support_url() {
	return 'https://www.bubblav.com/support?utm_source=wordpress-plugin&utm_medium=plugin&utm_campaign=settings';
}
