<?php
/**
 * Bootstrap Class
 *
 * Centralizes all file loading logic for the Freefactu plugin.
 * Ensures proper loading order: interfaces → abstracts → implementations.
 *
 * @package Freefactu
 * @since 0.5.0
 */

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

/**
 * Class Freefactu_Bootstrap
 *
 * Handles loading of all plugin classes in the correct dependency order.
 */
class Freefactu_Bootstrap {

	/**
	 * Initialize the bootstrap process.
	 *
	 * What this function does:
	 * - Loads all plugin classes in the correct dependency order
	 * - Ensures interfaces are loaded before implementations
	 * - Loads abstracts before concrete classes
	 *
	 * Loading order:
	 * 1. Interfaces (no dependencies)
	 * 2. Abstracts (depend on interfaces)
	 * 3. Core classes (depend on interfaces/abstracts)
	 * 4. Utilities (depend on core)
	 * 5. API classes (depend on interfaces/abstracts/utilities)
	 * 6. Admin classes (depend on utilities)
	 * 7. Generators (standalone DOMPDF-based)
	 *
	 * Where it's called from:
	 * - freefactu.php: Freefactu_Bootstrap::init() at plugin initialization
	 *
	 * @since 0.5.0
	 * @return void
	 */
	public static function init() {
		self::load_interfaces();
		self::load_abstracts();
		self::load_core();
		self::load_utilities();
		self::load_error_handling();
		self::load_api();
		self::load_admin();
		self::load_generators();
		self::load_extensions();
	}

	/**
	 * Load interface files.
	 *
	 * What this function does:
	 * - Loads Generator_Interface (defines PDF generation contract)
	 * - Loads Environment_Interface (defines environment-specific operations contract)
	 * - Must be loaded first as they have no dependencies
	 *
	 * Where it's called from:
	 * - class-freefactu-bootstrap.php: init() method
	 *
	 * @since 0.5.0
	 * @return void
	 */
	public static function load_interfaces() {
		require_once FREEFACTU_PLUGIN_DIR . 'includes/interfaces/interface-generator.php';
		require_once FREEFACTU_PLUGIN_DIR . 'includes/interfaces/interface-environment.php';
	}

	/**
	 * Load abstract base classes.
	 *
	 * What this function does:
	 * - Loads Base_Generator abstract class (shared PDF generation logic)
	 * - Loads Base_Environment abstract class (shared environment logic)
	 * - Provides base implementations that concrete classes extend
	 *
	 * Where it's called from:
	 * - class-freefactu-bootstrap.php: init() method
	 *
	 * @since 0.5.0
	 * @return void
	 */
	public static function load_abstracts() {
		require_once FREEFACTU_PLUGIN_DIR . 'includes/abstracts/abstract-base-generator.php';
		require_once FREEFACTU_PLUGIN_DIR . 'includes/api/environments/abstract-base-environment.php';
	}

	/**
	 * Load core classes.
	 *
	 * What this function does:
	 * - Loads Factory (creates generator and environment instances)
	 *
	 * Where it's called from:
	 * - class-freefactu-bootstrap.php: init() method
	 *
	 * @since 0.5.0
	 * @return void
	 */
	public static function load_core() {
		require_once FREEFACTU_PLUGIN_DIR . 'includes/core/class-freefactu-factory.php';
	}

	/**
	 * Load utility classes.
	 *
	 * What this function does:
	 * - Loads Vendor_Loader (vendor namespace resolution for scoped dependencies)
	 * - Loads Filesystem (safe file operations wrapper)
	 * - Loads Logger (logging system)
	 * - Loads DB (database operations for invoices)
	 * - Loads Series_Store (invoice series and numbering management)
	 * - Loads Extension_Series_Store (modular series for extensions like presupuestos)
	 * - Loads Snapshot_Utility (invoice snapshot management)
	 * - Loads Declaration_Manager (SIF declaration handling)
	 *
	 * Where it's called from:
	 * - class-freefactu-bootstrap.php: init() method
	 *
	 * @since 0.5.0
	 * @return void
	 */
	public static function load_utilities() {
		require_once FREEFACTU_PLUGIN_DIR . 'includes/utilities/class-freefactu-vendor-loader.php';
		require_once FREEFACTU_PLUGIN_DIR . 'includes/utilities/class-freefactu-filesystem.php';
		require_once FREEFACTU_PLUGIN_DIR . 'includes/utilities/class-freefactu-logger.php';
		require_once FREEFACTU_PLUGIN_DIR . 'includes/utilities/class-freefactu-db.php';
		require_once FREEFACTU_PLUGIN_DIR . 'includes/utilities/class-freefactu-series-store.php';
		require_once FREEFACTU_PLUGIN_DIR . 'includes/utilities/class-freefactu-extension-series-store.php';
		require_once FREEFACTU_PLUGIN_DIR . 'includes/utilities/class-freefactu-snapshot-utility.php';
		require_once FREEFACTU_PLUGIN_DIR . 'includes/utilities/class-freefactu-declaration-manager.php';
	}

	/**
	 * Load error handling classes.
	 *
	 * What this function does:
	 * - Loads AEAT error handling layer classes
	 * - Includes error categories, classifiers, and decision logic
	 * - Provides subsanation management functionality
	 *
	 * Where it's called from:
	 * - class-freefactu-bootstrap.php: init() method
	 *
	 * @since 1.1.0
	 * @return void
	 */
	public static function load_error_handling() {
		$error_dir = FREEFACTU_PLUGIN_DIR . 'includes/error-handling/';
		
		// Load in dependency order
		require_once $error_dir . 'class-freefactu-aeat-error-categories.php';
		require_once $error_dir . 'class-freefactu-aeat-next-action.php';
		require_once $error_dir . 'class-freefactu-aeat-response.php';
		require_once $error_dir . 'class-freefactu-aeat-error-classifier.php';
		require_once $error_dir . 'class-freefactu-aeat-next-action-decider.php';
		require_once $error_dir . 'class-freefactu-aeat-subsanation-service.php';
		require_once $error_dir . 'class-subsanation-validator.php';
		require_once $error_dir . 'class-freefactu-aeat-ui-helpers.php';
		require_once $error_dir . 'class-freefactu-aeat-test-scenarios.php';
	}

	/**
	 * Load API classes.
	 *
	 * What this function does:
	 * - Loads Client (handles invoice submission to AEAT)
	 * - Loads Signer (XML signing with digital certificates)
	 * - Loads Production_Environment (live AEAT endpoints)
	 * - Loads Test_Environment (AEAT test endpoints)
	 *
	 * Where it's called from:
	 * - class-freefactu-bootstrap.php: init() method
	 *
	 * @since 0.5.0
	 * @return void
	 */
	public static function load_api() {
		require_once FREEFACTU_PLUGIN_DIR . 'includes/api/class-freefactu-client.php';

		
		// Load environment implementations
		require_once FREEFACTU_PLUGIN_DIR . 'includes/api/environments/class-production-environment.php';
		require_once FREEFACTU_PLUGIN_DIR . 'includes/api/environments/class-test-environment.php';
	}

	/**
	 * Load admin classes.
	 *
	 * What this function does:
	 * - Loads Admin (settings page, invoice management UI)
	 * - Loads Debug (debug logs and chaining restoration)
	 * - Loads History (invoice history list)
	 * - Only loads when is_admin() is true
	 *
	 * Where it's called from:
	 * - class-freefactu-bootstrap.php: init() method
	 *
	 * @since 0.5.0
	 * @return void
	 */
	public static function load_admin() {
		if ( ! is_admin() ) {
			return;
		}

		require_once FREEFACTU_PLUGIN_DIR . 'includes/helpers/class-freefactu-client-error-helper.php';
		require_once FREEFACTU_PLUGIN_DIR . 'includes/helpers/class-freefactu-error-fix-helper.php';
		require_once FREEFACTU_PLUGIN_DIR . 'includes/admin/class-freefactu-admin.php';
		require_once FREEFACTU_PLUGIN_DIR . 'includes/admin/class-freefactu-debug.php';
		require_once FREEFACTU_PLUGIN_DIR . 'includes/admin/class-freefactu-history.php';
		require_once FREEFACTU_PLUGIN_DIR . 'includes/admin/class-freefactu-export.php';
		require_once FREEFACTU_PLUGIN_DIR . 'includes/admin/class-freefactu-extensions.php';

		// Initialize export handlers
		Freefactu_Export::init();
	}

	/**
	 * Load generator classes.
	 *
	 * What this function does:
	 * - Loads Standalone_Generator (PDF generation using DOMPDF)
	 * - Loads Standalone_Hooks (hooks for standalone mode)
	 *
	 * Where it's called from:
	 * - class-freefactu-bootstrap.php: init() method
	 *
	 * @since 0.5.0
	 * @return void
	 */
	public static function load_generators() {
		require_once FREEFACTU_PLUGIN_DIR . 'includes/generators/standalone/class-standalone-generator.php';
		require_once FREEFACTU_PLUGIN_DIR . 'includes/generators/standalone/class-standalone-hooks.php';
	}

	/**
	 * Load extension loader.
	 *
	 * What this function does:
	 * - Loads the Extension_Loader class which handles automatic loading of active extensions
	 * - Extensions are loaded from the extensions/ directory
	 * - Only active extensions (as stored in options) are loaded
	 *
	 * Where it's called from:
	 * - class-freefactu-bootstrap.php: init() method
	 *
	 * @since 1.2.0
	 * @return void
	 */
	public static function load_extensions() {
		require_once FREEFACTU_PLUGIN_DIR . 'includes/core/class-freefactu-extension-loader.php';
	}
}
