<?php
/**
 * Bootstrap File
 *
 * Handles all class loading and plugin initialization.
 *
 * @package DonorCore
 */

defined( 'ABSPATH' ) || exit;

/**
 * Load core classes needed before WooCommerce
 */
function donor_core_load_early_classes() {
	$includes_dir = DONOR_CORE_PLUGIN_DIR . 'includes/';
	
	// Core utilities (needed immediately)
	require_once $includes_dir . 'Utils/class-container.php';
	require_once $includes_dir . 'Utils/class-hook-loader.php';
	
	// Database layer (needed for activation)
	require_once $includes_dir . 'Database/class-schema-installer.php';
	require_once $includes_dir . 'Database/class-migration-manager.php';
	
	// Security (needed for activation)
	require_once $includes_dir . 'Security/class-security-schema.php';
	require_once $includes_dir . 'Security/class-role-manager.php';
	
	// Performance (needed for activation)
	require_once $includes_dir . 'Performance/class-query-optimizer.php';
	
	// Main Plugin class (load last)
	require_once $includes_dir . 'class-plugin.php';
}

/**
 * Load all plugin classes after WooCommerce is available
 */
function donor_core_load_plugin_classes() {
	$includes_dir = DONOR_CORE_PLUGIN_DIR . 'includes/';
	
	// Database repositories
	require_once $includes_dir . 'Database/class-donation-log-repository.php';
	require_once $includes_dir . 'Database/class-donor-analytics-repository.php';
	
	// WooCommerce integration (requires WooCommerce classes)
	require_once $includes_dir . 'WooCommerce/class-product-type-registration.php';
	require_once $includes_dir . 'WooCommerce/class-wc-product-donation.php';
	require_once $includes_dir . 'WooCommerce/class-campaign-product-bridge.php';
	require_once $includes_dir . 'WooCommerce/class-checkout-manager.php';
	
	// Post types
	require_once $includes_dir . 'PostTypes/class-campaign-post-type.php';
	
	// Frontend
	require_once $includes_dir . 'Frontend/class-block-registration.php';
	require_once $includes_dir . 'Frontend/class-form-manager.php';
	
	// Receipts
	require_once $includes_dir . 'Receipts/class-pdf-generator.php';
	require_once $includes_dir . 'Receipts/class-receipt-manager.php';
	require_once $includes_dir . 'Receipts/class-email-sender.php';
	require_once $includes_dir . 'Receipts/class-receipt-downloader.php';
	
	// Performance
	require_once $includes_dir . 'Performance/class-asset-manager.php';
	require_once $includes_dir . 'Performance/class-cache-manager.php';
	
	// Privacy (GDPR compliance)
	require_once $includes_dir . 'Privacy/class-privacy-exporter.php';
	require_once $includes_dir . 'Privacy/class-privacy-eraser.php';
	require_once $includes_dir . 'Privacy/class-privacy-policy.php';
	
	// Admin
	require_once $includes_dir . 'Admin/class-menu-manager.php';
	
	// API Controllers
	require_once $includes_dir . 'API/class-rest-controller-base.php';
	require_once $includes_dir . 'API/class-campaigns-controller.php';
	require_once $includes_dir . 'API/class-analytics-controller.php';
	require_once $includes_dir . 'API/class-donors-controller.php';
	require_once $includes_dir . 'API/class-settings-controller.php';
}

/**
 * Initialize the plugin
 */
function donor_core_init() {
	// Check for WooCommerce dependency
	if ( ! class_exists( 'WooCommerce' ) ) {
		add_action( 'admin_notices', 'donor_core_woocommerce_missing_notice' );
		return;
	}
	
	// Load all plugin classes
	donor_core_load_plugin_classes();
	
	// Initialize singleton
	$plugin = \DonorCore\Plugin::instance();
	
	// Run activation tasks if needed (first time after activation)
	if ( get_option( 'donor_core_needs_activation' ) ) {
		\DonorCore\Plugin::activate();
		delete_option( 'donor_core_needs_activation' );
	}
}
add_action( 'plugins_loaded', 'donor_core_init' );

/**
 * Activation hook handler
 */
function donor_core_activate() {
	// Check for WooCommerce dependency before activation
	if ( ! class_exists( 'WooCommerce' ) ) {
		// Deactivate the plugin
		deactivate_plugins( plugin_basename( DONOR_CORE_PLUGIN_FILE ) );
		
		// Add admin notice about missing WooCommerce
		add_action( 'admin_notices', 'donor_core_activation_failed_notice' );
		
		return;
	}
	
	// Just set an activation flag, actual setup will run on plugins_loaded
	update_option( 'donor_core_needs_activation', true );
}

/**
 * Deactivation hook handler
 */
function donor_core_deactivate() {
	\DonorCore\Plugin::deactivate();
}

/**
 * Admin notice when WooCommerce is missing
 */
function donor_core_woocommerce_missing_notice() {
	?>
	<div class="notice notice-error">
		<p>
			<strong><?php esc_html_e( 'DonorCore', 'donorcore-donations' ); ?></strong>
			<?php esc_html_e( 'requires WooCommerce to be installed and activated.', 'donorcore-donations' ); ?>
			<a href="<?php echo esc_url( admin_url( 'plugin-install.php?s=woocommerce&tab=search&type=term' ) ); ?>">
				<?php esc_html_e( 'Install WooCommerce', 'donorcore-donations' ); ?>
			</a>
		</p>
	</div>
	<?php
}

/**
 * Admin notice when activation fails due to missing WooCommerce
 */
function donor_core_activation_failed_notice() {
	$install_url = admin_url( 'plugin-install.php?s=woocommerce&tab=search&type=term' );
	?>
	<div class="notice notice-error is-dismissible">
		<p>
			<strong><?php esc_html_e( 'DonorCore could not be activated', 'donorcore-donations' ); ?></strong>
		</p>
		<p>
			<?php esc_html_e( 'DonorCore requires WooCommerce to be installed and activated.', 'donorcore-donations' ); ?>
		</p>
		<p>
			<a href="<?php echo esc_url( $install_url ); ?>" class="button button-primary">
				<?php esc_html_e( 'Install WooCommerce', 'donorcore-donations' ); ?>
			</a>
			<a href="<?php echo esc_url( admin_url( 'plugins.php' ) ); ?>" class="button">
				<?php esc_html_e( 'Return to Plugins', 'donorcore-donations' ); ?>
			</a>
		</p>
	</div>
	<?php
}

/**
 * Admin notice when mbstring extension is missing
 */
function donor_core_mbstring_missing_notice() {
	?>
	<div class="notice notice-error">
		<p>
			<strong><?php esc_html_e( 'DonorCore', 'donorcore-donations' ); ?></strong>
			<?php esc_html_e( 'requires the PHP mbstring extension for PDF receipt generation.', 'donorcore-donations' ); ?>
		</p>
		<p>
			<?php esc_html_e( 'Please contact your hosting provider to enable the mbstring extension, or add this line to your php.ini:', 'donorcore-donations' ); ?>
			<code>extension=mbstring</code>
		</p>
	</div>
	<?php
}
