<?php
/**
 * Plugin Name:       CelerSearch
 * Plugin URI:        https://celersearch.com/
 * Description:       Extends WordPress search with a powerful MeiliSearch integration
 * Version:           1.4.1
 * Author:            CelerSearch
 * Author URI:        https://celersearch.com/
 * License:           GPL-2.0+
 * License URI:       http://www.gnu.org/licenses/gpl-2.0.txt
 * Text Domain:       celersearch
 * Domain Path:       /languages
 * Requires at least: 6.0
 * Requires PHP:      8.1
 */

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

define( 'CELERSEARCH_PLUGIN_VERSION', '1.4.1' );
define( 'CELERSEARCH_PLUGIN_PATH', rtrim( plugin_dir_path( __FILE__ ), DIRECTORY_SEPARATOR ) . DIRECTORY_SEPARATOR );
define( 'CELERSEARCH_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
define( 'CELERSEARCH_REVIEW_URL', 'https://wordpress.org/support/plugin/celersearch/reviews/#new-post' );

if ( ! file_exists( CELERSEARCH_PLUGIN_PATH . 'vendor/autoload.php' ) ) {
	add_action( 'admin_notices', function () {
		printf(
			'<div class="%1$s"><p>%2$s</p></div>',
			'notice notice-error',
			'You are running development versio of <strong>CelerSearch</strong>, please download the plugin from WordPress.org in order to be able to use it.'
		);
	} );

	return;
}

// Load scoped (prefixed) vendor dependencies first
if ( file_exists( CELERSEARCH_PLUGIN_PATH . 'vendor_prefixed/vendor/autoload.php' ) ) {
	require_once CELERSEARCH_PLUGIN_PATH . 'vendor_prefixed/vendor/autoload.php';
}

require_once CELERSEARCH_PLUGIN_PATH . 'vendor/autoload.php';

// Initialize Action Scheduler if bundled (for sites without WooCommerce)
if ( file_exists( CELERSEARCH_PLUGIN_PATH . 'vendor/woocommerce/action-scheduler/action-scheduler.php' ) ) {
	require_once CELERSEARCH_PLUGIN_PATH . 'vendor/woocommerce/action-scheduler/action-scheduler.php';
}

// Initialize plugin on plugins_loaded hook
add_action( 'plugins_loaded', function () {
	$GLOBALS['celersearch'] = new \CelerSearch\Boot();
	$GLOBALS['celersearch']->register();
}, 10 );

// First-run welcome notice: ensure the dismiss option exists. Existing
// installs reactivating after an update (i.e. they already have services
// configured) get the notice auto-dismissed so we don't nag returning users.
// The notice itself is rendered by Admin\WelcomeNotice on the plugin's
// admin screen — no redirect needed.
register_activation_hook( __FILE__, function () {
	add_option( 'celersearch_welcome_dismissed', false, '', false );

	// Record first-activation timestamp. Used as the "3+ days of usage" gate
	// for the review nudge card; set only once so upgrades keep the original
	// date.
	add_option( 'celersearch_activated_at', time(), '', false );

	global $wpdb;
	$services_table = $wpdb->prefix . 'celersearch_services';
	// Suppress errors — table may not exist yet on a fresh install (Setup::migrate
	// runs on plugins_loaded, after activation).
	$suppress = $wpdb->suppress_errors( true );
	$existing = (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$services_table}" );
	$wpdb->suppress_errors( $suppress );

	if ( $existing > 0 ) {
		update_option( 'celersearch_welcome_dismissed', true );
	}
} );

// Register WP-CLI commands.
if ( defined( 'WP_CLI' ) && WP_CLI ) {
	WP_CLI::add_command( 'celersearch reindex', \CelerSearch\CLI\ReindexCommand::class );
	WP_CLI::add_command( 'celersearch index', \CelerSearch\CLI\IndexCommand::class );
	WP_CLI::add_command( 'celersearch service', \CelerSearch\CLI\ServiceCommand::class );
	WP_CLI::add_command( 'celersearch settings', \CelerSearch\CLI\SettingsCommand::class );
}

