<?php
/**
 * Plugin Name:       AmbassadorHub
 * Plugin URI:        https://ambassadorhub.nl
 * Description:       Peer-to-peer fundraising platform — accounts, campaigns, fundraisers, and donations, running entirely on your own WordPress site.
 * Version:           1.0.0
 * Requires at least: 6.7
 * Tested up to:      7.1
 * Requires PHP:      8.2
 * Author:            Trybes
 * Author URI:        https://trybes.nl
 * License:           GPL-2.0-or-later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       ambassadorhub
 * Domain Path:       /languages
 *
 * @package AmbassadorHub
 */

namespace AmbassadorHub;

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

// ---------------------------------------------------------------------------
// Plugin constants
// ---------------------------------------------------------------------------

define( 'AMBASSADOR_HUB_VERSION', '1.0.0' );
define( 'AMBASSADOR_HUB_FILE', __FILE__ );
define( 'AMBASSADOR_HUB_PATH', plugin_dir_path( __FILE__ ) );
define( 'AMBASSADOR_HUB_URL', plugin_dir_url( __FILE__ ) );
define( 'AMBASSADOR_HUB_BASENAME', plugin_basename( __FILE__ ) );

// ---------------------------------------------------------------------------
// Autoloader
// ---------------------------------------------------------------------------

require_once AMBASSADOR_HUB_PATH . 'vendor/autoload.php';
require_once AMBASSADOR_HUB_PATH . 'src/QueryLoop/HierarchyFilter.php';

// ---------------------------------------------------------------------------
// Action Scheduler
// ---------------------------------------------------------------------------

require_once AMBASSADOR_HUB_PATH . 'vendor/woocommerce/action-scheduler/action-scheduler.php';

// ---------------------------------------------------------------------------
// Bootstrap
// ---------------------------------------------------------------------------

/**
 * Initialize the plugin after all plugins are loaded.
 *
 * @return void
 */
function ambassador_hub_init(): void {
	Plugin::instance();
}
add_action( 'plugins_loaded', __NAMESPACE__ . '\\ambassador_hub_init' );

/**
 * Plugin activation hook.
 *
 * Registers post types so rewrite rules can be flushed correctly,
 * sets the stored version, and fires the activation action for modules
 * to hook into (e.g. to seed default options).
 *
 * @return void
 */
function ambassador_hub_activate(): void {
	// Boot the plugin so post types are registered before flushing rewrites.
	Plugin::instance();

	// Persist (or upgrade) the stored version.
	update_option( 'ambassador_hub_version', AMBASSADOR_HUB_VERSION );

	// Schedule the first-run redirect only when the wizard hasn't been completed
	// yet. Skipping this on re-activations (e.g. after deactivate/reactivate)
	// prevents users who finished setup from being pushed back into the wizard.
	if ( ! \AmbassadorHub\Admin\Wizard\WizardState::is_completed() ) {
		\AmbassadorHub\Admin\Wizard\WizardState::schedule_redirect();
	}

	/**
	 * Fires during plugin activation.
	 *
	 * Modules can hook here to seed default options or run one-time setup.
	 *
	 * @since 0.1.0
	 */
	do_action( 'ambassador_hub_activate' );

	// Flush after post types are registered so permalink rules are correct.
	flush_rewrite_rules();
}
register_activation_hook( __FILE__, __NAMESPACE__ . '\\ambassador_hub_activate' );

/**
 * Plugin deactivation hook.
 *
 * Fires the deactivation action first so modules can clean up (e.g. cancel
 * scheduled actions), then flushes stale rewrite rules.
 *
 * @return void
 */
function ambassador_hub_deactivate(): void {
	/**
	 * Fires during plugin deactivation.
	 *
	 * Modules can hook here to cancel Action Scheduler jobs or clean up
	 * transients before the plugin is disabled.
	 *
	 * @since 0.1.0
	 */
	do_action( 'ambassador_hub_deactivate' );

	flush_rewrite_rules();
}
register_deactivation_hook( __FILE__, __NAMESPACE__ . '\\ambassador_hub_deactivate' );
