<?php
/**
 * Plugin Loader
 *
 * @package SureDonation
 */

namespace SureDonation;

use SureDonation\Inc\Admin\Admin;
use SureDonation\Inc\Ajax\Donation_Handler;
use SureDonation\Inc\Assets\Register as Assets_Register;
use SureDonation\Inc\Blocks\Register as Blocks_Register;
use SureDonation\Inc\Campaigns\Campaign_Cpt;
use SureDonation\Inc\Database\Register as Database_Register;
use SureDonation\Inc\FormEditor\Assets as FormEditor_Assets;
use SureDonation\Inc\Payments\Stripe\Stripe_Frontend;
use SureDonation\Inc\Payments\Stripe\Stripe_Settings;
use SureDonation\Inc\Payments\Stripe\Stripe_Webhook;
use SureDonation\Inc\Post_Types\Donation_Form;
use SureDonation\Inc\Rest_Api;
use SureDonation\Inc\Shortcodes\Donation_Form as Donation_Form_Shortcode;

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Main Plugin Class
 *
 * @since 0.0.1
 */
final class Plugin_Loader {
	/**
	 * Instance of this class.
	 *
	 * @var Plugin_Loader
	 */
	private static $instance = null;

	/**
	 * Constructor
	 */
	private function __construct() {
		$this->setup_autoloader();
		$this->init_hooks();
	}

	/**
	 * Get instance of this class.
	 *
	 * @return Plugin_Loader
	 */
	public static function get_instance() {
		if ( null === self::$instance ) {
			self::$instance = new self();
		}
		return self::$instance;
	}

	/**
	 * Autoload classes.
	 *
	 * @param string $class_name Class name.
	 * @return void
	 */
	public function autoload( $class_name ) {
		// Check if class belongs to our namespace.
		if ( strpos( $class_name, 'SureDonation\\' ) !== 0 ) {
			return;
		}

		// Remove namespace prefix.
		$class_name = str_replace( 'SureDonation\\', '', $class_name );

		// Remove 'Inc\' prefix if exists.
		$class_name = str_replace( 'Inc\\', '', $class_name );

		// Convert namespace separators to directory separators.
		$class_name = str_replace( '\\', DIRECTORY_SEPARATOR, $class_name );

		// Convert class name parts to lowercase with hyphens.
		$parts = explode( DIRECTORY_SEPARATOR, $class_name );
		$parts = array_map(
			static function ( $part ) {
				// Convert CamelCase to kebab-case.
				$converted = preg_replace( '/([a-z])([A-Z])/', '$1-$2', $part );
				return strtolower( str_replace( '_', '-', is_string( $converted ) ? $converted : $part ) );
			},
			$parts
		);

		$class_name = implode( DIRECTORY_SEPARATOR, $parts );

		// Build file path.
		$file = SUREDONATION_DIR . 'inc/' . $class_name . '.php';

		// Load file if exists.
		if ( file_exists( $file ) ) {
			require_once $file;
		}
	}

	/**
	 * Load plugin components.
	 *
	 * @return void
	 */
	public function load_plugin() {
		// Initialize database tables.
		Database_Register::init();

		// Initialize Campaign CPT.
		Campaign_Cpt::get_instance();

		// Initialize Donation Form CPT.
		Donation_Form::get_instance();

		// Initialize Admin.
		if ( is_admin() ) {
			Admin::get_instance();
		}

		// Initialize REST API.
		Rest_Api::get_instance();

		// Initialize blocks (must be before init hook fires).
		Blocks_Register::get_instance();

		// Initialize frontend assets.
		Assets_Register::get_instance();

		// Initialize form editor assets (sidebar, meta, etc.).
		FormEditor_Assets::get_instance();

		// Initialize AJAX handlers.
		Donation_Handler::get_instance();

		// Initialize shortcodes.
		Donation_Form_Shortcode::get_instance();

		// Initialize Stripe payment gateway.
		Stripe_Settings::get_instance();
		Stripe_Webhook::get_instance();
		Stripe_Frontend::get_instance();
	}

	/**
	 * Setup autoloader for plugin classes.
	 *
	 * @return void
	 */
	private function setup_autoloader() {
		spl_autoload_register( [ $this, 'autoload' ] );
	}

	/**
	 * Initialize plugin hooks.
	 *
	 * @return void
	 */
	private function init_hooks() {
		// Activation hook.
		register_activation_hook(
			SUREDONATION_FILE,
			static function () {
				// Reset rewrite rules.
				delete_option( 'rewrite_rules' );

				// Set redirect flag for onboarding.
				update_option( '__suredonation_do_redirect', true );
			}
		);

		// Deactivation hook.
		register_deactivation_hook(
			SUREDONATION_FILE,
			static function () {
				update_option( '__suredonation_do_redirect', false );
			}
		);

		// Initialize plugin after WordPress loads.
		add_action( 'plugins_loaded', [ $this, 'load_plugin' ], 99 );
	}
}

// Initialize plugin.
Plugin_Loader::get_instance();
