<?php
/**
 * PSR-4-style Autoloader
 *
 * Classmap-based autoloader for all Niwa AI classes, traits, and interfaces.
 * Replaces ~80 manual require_once statements with a single spl_autoload_register.
 *
 * @package Niwa_AI
 * @since   0.10.31
 */

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

class Niwa_AI_Autoloader {

	/**
	 * Static classmap: symbol name → file path (relative to plugin root).
	 *
	 * Includes classes, traits, and interfaces — PHP autoloads all of these
	 * via spl_autoload_register.
	 *
	 * @var array<string, string>
	 */
	private static $classmap = array(

		// ── Main Plugin ──
		'Niwa_AI_Plugin'                           => 'includes/class-niwa-ai.php',

		// ── Core Classes ──
		'Niwa_AI_Auth_Manager'                     => 'includes/core/class-auth-manager.php',
		'Niwa_AI_Cart_Tool_Service'                => 'includes/core/class-cart-tool-service.php',
		'Niwa_AI_Navigation_Tool_Service'          => 'includes/core/class-navigation-tool-service.php',
		'Niwa_AI_Card_Builder'                     => 'includes/core/class-card-builder.php',
		'Niwa_AI_Constants'                        => 'includes/core/class-constants.php',
		'Niwa_AI_Contact_Service'                  => 'includes/core/class-contact-service.php',
		'Niwa_AI_Appointment_Service'              => 'includes/core/class-appointment-service.php',
		'Niwa_AI_Appointment_Prospect_Summarizer'  => 'includes/core/class-appointment-prospect-summarizer.php',
		'Niwa_AI_Appointment_Notifier'             => 'includes/core/class-appointment-notifier.php',
		'Niwa_AI_Calendar_Credentials'             => 'includes/core/class-calendar-credentials.php',
		'Niwa_AI_Calendar_OAuth_State'             => 'includes/core/class-calendar-oauth-state.php',
		'Niwa_AI_Calendar_Install_Attestation'     => 'includes/core/class-calendar-install-attestation.php',
		'Niwa_AI_Calendar_OAuth_Relay_Client'      => 'includes/core/class-calendar-oauth-relay-client.php',
		'Niwa_AI_Google_Calendar_Client'           => 'includes/core/class-google-calendar-client.php',
		'Niwa_AI_Calendar_Integration_Service'     => 'includes/core/class-calendar-integration-service.php',
		'Niwa_AI_Scheduler_Ajax_Controller'        => 'includes/core/class-scheduler-ajax-controller.php',
		'Niwa_AI_Cron_Maintenance'                 => 'includes/core/class-cron-maintenance.php',
		'Niwa_AI_Customer_Sales_Assistant_Profile' => 'includes/core/class-customer-sales-assistant-profile.php',
		'Niwa_AI_Frontend_Conversation_Context'    => 'includes/core/class-frontend-conversation-context.php',
		'Niwa_AI_Frontend_Product_Card_Selector'   => 'includes/core/class-frontend-product-card-selector.php',
		'Niwa_AI_Frontend_Product_Matcher'         => 'includes/core/class-frontend-product-matcher.php',
		'Niwa_AI_Frontend_Product_Query_Plan'      => 'includes/core/class-frontend-product-query-plan.php',
		'Niwa_AI_Frontend_Product_Ranking_Service' => 'includes/core/class-frontend-product-ranking-service.php',
		'Niwa_AI_Frontend_Selected_Product_Option_Resolver' => 'includes/core/class-frontend-selected-product-option-resolver.php',
		'Niwa_AI_Frontend_Selected_Product_Response_Composer' => 'includes/core/class-frontend-selected-product-response-composer.php',
		'Niwa_AI_Frontend_Product_Response_Composer'          => 'includes/core/class-frontend-product-response-composer.php',
		'Niwa_AI_Frontend_Semantic_Lexicon'        => 'includes/core/class-frontend-semantic-lexicon.php',
		'Niwa_AI_Frontend_Response_Postprocessor'  => 'includes/core/class-frontend-response-postprocessor.php',
		'Niwa_AI_Lead_WhatsApp_Outreach_Service'   => 'includes/core/class-lead-whatsapp-outreach-service.php',
		'Niwa_AI_Phone_Normalizer'                  => 'includes/core/class-phone-normalizer.php',
		'Niwa_AI_Lead_Form_Ajax_Handler'           => 'includes/core/class-lead-form-ajax-handler.php',
		'Niwa_AI_Lead_Form_LLM_Router'             => 'includes/core/class-lead-form-llm-router.php',
		'Niwa_AI_Lead_Form_Mailer'                 => 'includes/core/class-lead-form-mailer.php',
		'Niwa_AI_Lead_Form_Runtime'                => 'includes/core/class-lead-form-runtime.php',
		'Niwa_AI_Lead_Form_Schema'                 => 'includes/core/class-lead-form-schema.php',
		'Niwa_AI_Lead_Form_Validator'              => 'includes/core/class-lead-form-validator.php',
		'Niwa_AI_Lead_Form_Session_Store'          => 'includes/core/class-lead-form-session-store.php',

		// ── Cart Recovery ──
		'Niwa_AI_Cart_Recovery_Line_Item'          => 'includes/core/cart-recovery/class-cart-recovery-line-item.php',
		'Niwa_AI_Cart_Recovery_Cart_Snapshot'      => 'includes/core/cart-recovery/class-cart-recovery-cart-snapshot.php',
		'Niwa_AI_Cart_Recovery_Snapshot_Builder'   => 'includes/core/cart-recovery/class-cart-recovery-snapshot-builder.php',
		'Niwa_AI_Cart_Recovery_Repository'         => 'includes/core/cart-recovery/class-cart-recovery-repository.php',
		'Niwa_AI_Cart_Recovery_Service'            => 'includes/core/cart-recovery/class-cart-recovery-service.php',
		'Niwa_AI_Cart_Recovery_Scheduler'          => 'includes/core/cart-recovery/class-cart-recovery-scheduler.php',
		'Niwa_AI_Cart_Recovery_Composition_Context' => 'includes/core/cart-recovery/class-cart-recovery-composition-context.php',
		'Niwa_AI_Cart_Recovery_Composition_Result' => 'includes/core/cart-recovery/class-cart-recovery-composition-result.php',
		'Niwa_AI_Cart_Recovery_Composer'           => 'includes/core/cart-recovery/class-cart-recovery-composer.php',
		'Niwa_AI_Cart_Recovery_Coupon_Config'      => 'includes/core/cart-recovery/class-cart-recovery-coupon-config.php',
		'Niwa_AI_Cart_Recovery_Settings'           => 'includes/core/cart-recovery/class-cart-recovery-settings.php',
		'Niwa_AI_Cart_Recovery_Coupon_Service'     => 'includes/core/cart-recovery/class-cart-recovery-coupon-service.php',
		'Niwa_AI_Cart_Recovery_Coupon_Listener'    => 'includes/core/cart-recovery/class-cart-recovery-coupon-listener.php',
		'Niwa_AI_Cart_Recovery_Transcript_Digester' => 'includes/core/cart-recovery/class-cart-recovery-transcript-digester.php',
		'Niwa_AI_Cart_Recovery_Template_Wrapper'   => 'includes/core/cart-recovery/class-cart-recovery-template-wrapper.php',
		'Niwa_AI_Cart_Recovery_Mailer'             => 'includes/core/cart-recovery/class-cart-recovery-mailer.php',
		'Niwa_AI_Cart_Recovery_Tracking_Controller' => 'includes/core/cart-recovery/class-cart-recovery-tracking-controller.php',
		'Niwa_AI_Cart_Recovery_Settings_UI'        => 'admin/class-cart-recovery-settings-ui.php',
		'Niwa_AI_Cart_Recovery_Privacy'            => 'includes/core/cart-recovery/class-cart-recovery-privacy.php',
		'Niwa_AI_Cart_Recovery_Analytics_Widget'   => 'includes/core/cart-recovery/class-cart-recovery-analytics-widget.php',
		'Niwa_AI_Cart_Recovery_Placeholder_Renderer' => 'includes/core/cart-recovery/class-cart-recovery-placeholder-renderer.php',

		// â”€â”€ Traffic Analysis â”€â”€
		'Niwa_AI_Traffic_Attribution'             => 'includes/core/traffic-analysis/class-traffic-attribution.php',
		'Niwa_AI_Traffic_DB'                      => 'includes/core/traffic-analysis/class-traffic-db.php',
		'Niwa_AI_Traffic_Repository'              => 'includes/core/traffic-analysis/class-traffic-repository.php',
		'Niwa_AI_Traffic_REST_Controller'         => 'includes/core/traffic-analysis/class-traffic-rest-controller.php',
		'Niwa_AI_Traffic_Conversion_Tracker'      => 'includes/core/traffic-analysis/class-traffic-conversion-tracker.php',
		'Niwa_AI_Traffic_Rollup_Worker'           => 'includes/core/traffic-analysis/class-traffic-rollup-worker.php',
		'Niwa_AI_Traffic_Dashboard_Widget'        => 'includes/core/traffic-analysis/class-traffic-dashboard-widget.php',
		'Niwa_AI_Traffic_Module'                  => 'includes/core/traffic-analysis/class-traffic-module.php',

		'Niwa_AI_Durable_Cache'                    => 'includes/core/class-durable-cache.php',
		'Niwa_AI_Agent_Profile_Summary_Service'    => 'includes/core/class-agent-profile-summary-service.php',
		'Niwa_AI_Agent_Memory_Manager'            => 'includes/core/class-agent-memory-manager.php',
		'Niwa_AI_Agent_Memory_Index'              => 'includes/core/class-agent-memory-index.php',
		'Niwa_AI_Content_Store'                    => 'includes/core/class-content-store.php',
		'Niwa_AI_Conversation_Store'               => 'includes/core/class-conversation-store.php',
		'Niwa_AI_Companion_Memory_Text'            => 'includes/core/class-companion-memory-text.php',
		'Niwa_AI_Companion_Memory_Manager'         => 'includes/core/class-companion-memory-manager.php',
		'Niwa_AI_Companion_Profile_Promoter'       => 'includes/core/class-companion-profile-promoter.php',
		'Niwa_AI_Identity_Name'                    => 'includes/core/class-identity-name.php',
		'Niwa_AI_Email_Validator'                  => 'includes/core/class-email-validator.php',
		'Niwa_AI_Embedding_Client'                 => 'includes/core/class-embedding-client.php',
		'Niwa_AI_Frontend_Question_Log'            => 'includes/core/class-frontend-question-log.php',
		'Niwa_AI_HTML_Content_Cleaner'             => 'includes/core/class-html-content-cleaner.php',
		'Niwa_AI_Blog_Quality_Gate'                => 'includes/core/class-blog-quality-gate.php',
		'Niwa_AI_Humanizer'                        => 'includes/core/class-humanizer.php',
		'Niwa_AI_Index_Sync'                       => 'includes/core/class-index-sync.php',
		'Niwa_AI_Index_Worker'                     => 'includes/core/class-index-worker.php',
		'Niwa_AI_Latency_Tracker'                  => 'includes/core/class-latency-tracker.php',
		'Niwa_AI_LLM_Audit_Logger'                 => 'includes/core/class-llm-audit-logger.php',
		'Niwa_AI_Memory_Embedder'                  => 'includes/core/class-memory-embedder.php',
		'Niwa_AI_Memory_Searcher'                  => 'includes/core/class-memory-searcher.php',
		'Niwa_AI_Product_Search_Evidence'          => 'includes/core/class-product-search-evidence.php',
		'Niwa_AI_Product_Variation_Preview'        => 'includes/core/class-product-variation-preview.php',
		'Niwa_AI_Model_Catalog'                    => 'includes/core/class-model-catalog.php',
		'Niwa_AI_Model_Router'                     => 'includes/core/class-model-router.php',
		'Niwa_AI_Name_Processor'                   => 'includes/core/class-name-processor.php',
		'Niwa_AI_OpenAI_Client'                    => 'includes/core/class-openai-client.php',
		'Niwa_AI_RAG_Fallback'                     => 'includes/core/class-rag-fallback.php',
		'Niwa_AI_AI_Client_Interface'              => 'includes/core/class-ai-client-interface.php',
		'Niwa_AI_Auth_Provider_Interface'          => 'includes/core/class-auth-provider-interface.php',
		'Niwa_AI_Content_Store_Interface'          => 'includes/core/class-content-store-interface.php',
		'Niwa_AI_RAG_Provider_Interface'           => 'includes/core/class-rag-provider-interface.php',
		'Niwa_AI_RLM_Engine'                       => 'includes/core/class-rlm-engine.php',
		'Niwa_AI_Safe_HTTP'                        => 'includes/core/class-safe-http.php',
		'Niwa_AI_Security_Service'                 => 'includes/core/class-security-service.php',
		'Niwa_AI_Session_Memory_Store'             => 'includes/core/class-session-memory-store.php',
		'Niwa_AI_Spark_Adapter'                    => 'includes/core/class-spark-adapter.php',
		'Niwa_AI_Session_Utils'                    => 'includes/core/class-session-utils.php',
		'Niwa_AI_Site_Knowledge_Extractor'         => 'includes/core/class-site-knowledge-extractor.php',
		'Niwa_AI_Store_Overview'                   => 'includes/core/class-store-overview.php',
		'Niwa_AI_Text_Normalizer'                  => 'includes/core/class-text-normalizer.php',
		'Niwa_AI_Woo_RAG_Provider'                 => 'includes/core/class-woo-rag-provider.php',
		'Niwa_AI_Performance_Settings'             => 'includes/core/class-performance-settings.php',
		'Niwa_AI_External_Page_Cache'               => 'includes/core/class-external-page-cache.php',

		// ── Frontend Classes ──
		'Niwa_AI_Asset_Loader'                     => 'includes/frontend/class-asset-loader.php',
		'Niwa_AI_Blog_Asset_Renderer'              => 'includes/frontend/class-blog-asset-renderer.php',
		'Niwa_AI_Chat_Renderer'                    => 'includes/frontend/class-chat-renderer.php',
		'Niwa_AI_Variation_Gallery'                => 'includes/frontend/class-variation-gallery.php',

		// ── Admin Classes ──
		'Niwa_AI_Admin_Settings'                   => 'admin/class-admin-settings.php',
		'Niwa_AI_Admin_Autonomy_Settings'          => 'admin/class-admin-autonomy-settings.php',
		'Niwa_AI_Analytics_Dashboard'              => 'admin/class-analytics-dashboard.php',
		'Niwa_AI_Agent_Mode_Settings'              => 'admin/class-agent-mode-settings.php',
		'Niwa_AI_Attention_Capture'                => 'admin/class-attention-capture.php',
		'Niwa_AI_Attention_Objection_Generator'     => 'includes/core/class-attention-objection-generator.php',
		'Niwa_AI_Conversion_Busters'               => 'admin/class-conversion-busters.php',
		'Niwa_AI_Calendar_Integration_UI'          => 'admin/class-calendar-integration-ui.php',
		'Niwa_AI_Conversation_History_Page'        => 'admin/class-conversation-history-page.php',
		'Niwa_AI_Lead_Capture_Settings'            => 'admin/class-lead-capture-settings.php',
		'Niwa_AI_Lead_Form_Builder_UI'             => 'admin/class-lead-form-builder-ui.php',
		'Niwa_AI_Leave_Stoper_Settings'            => 'admin/class-leave-stoper-settings.php',
		'Niwa_AI_UI_Settings'                      => 'admin/class-ui-settings.php',
		'Niwa_AI_Admin_Bloging_Settings'           => 'admin/class-admin-bloging-settings.php',
		'Niwa_AI_Performance_Settings_UI'          => 'admin/class-performance-settings-ui.php',
		'Niwa_AI_Template_Library_Page'            => 'admin/template-library/class-template-library-page.php',
		'Niwa_AI_Snippets_Page'                    => 'admin/template-library/class-snippets-page.php',

		// Template Library Classes.
		'Niwa_AI_Template_Library_Types'           => 'includes/template-library/class-template-library-types.php',
		'Niwa_AI_Template_Post_Type'               => 'includes/template-library/class-template-post-type.php',
		'Niwa_AI_Snippet_Post_Type'                => 'includes/template-library/class-snippet-post-type.php',
		'Niwa_AI_Template_Repository'              => 'includes/template-library/class-template-repository.php',
		'Niwa_AI_Template_Seeder'                  => 'includes/template-library/class-template-seeder.php',
		'Niwa_AI_Template_Design_Context'          => 'includes/template-library/class-template-design-context.php',
		'Niwa_AI_Snippet_Repository'               => 'includes/template-library/class-snippet-repository.php',
		'Niwa_AI_Snippet_Validator'                => 'includes/template-library/class-snippet-validator.php',
		'Niwa_AI_Snippet_Filesystem'               => 'includes/template-library/class-snippet-filesystem.php',
		'Niwa_AI_Shortcode_Runtime'                => 'includes/template-library/class-shortcode-runtime.php',
		'Niwa_AI_Template_AI_Generator'            => 'includes/template-library/class-template-ai-generator.php',
		'Niwa_AI_Template_Elementor_Builder'       => 'includes/template-library/class-template-elementor-builder.php',
		'Niwa_AI_Elementor_URL_Blueprint_Builder'  => 'includes/template-library/class-elementor-url-blueprint-builder.php',
		'Niwa_AI_Elementor_Reference_Blueprints'   => 'includes/template-library/class-elementor-reference-blueprints.php',
		'Niwa_AI_Elementor_Angie_Reference_Page_Renderer' => 'includes/template-library/class-elementor-angie-reference-page-renderer.php',
		'Niwa_AI_Elementor_Template_Widget_Base'   => 'includes/template-library/class-elementor-template-widget-base.php',
		'Niwa_AI_Elementor_Adapter'                => 'includes/template-library/class-elementor-adapter.php',
		'Niwa_AI_Template_Library_REST_Controller' => 'includes/template-library/class-template-library-rest-controller.php',
		'Niwa_AI_Template_Library_Module'          => 'includes/template-library/class-template-library-module.php',

		// ── Admin-Autonomy Classes ──
		'Niwa_AI_Admin_Autonomy'                   => 'includes/admin-autonomy/class-admin-autonomy.php',
		'Niwa_AI_Admin_Autonomy_Admin_Request_Helper' => 'includes/admin-autonomy/class-admin-autonomy-admin-request-helper.php',
		'Niwa_AI_Admin_Autonomy_Actor_Message_Preparer' => 'includes/admin-autonomy/class-admin-autonomy-actor-message-preparer.php',
		'Niwa_AI_Admin_Autonomy_Actor_Runtime_Lease_Keeper' => 'includes/admin-autonomy/class-admin-autonomy-actor-runtime-lease-keeper.php',
		'Niwa_AI_Admin_Autonomy_Actor_Runtime_Recovery' => 'includes/admin-autonomy/class-admin-autonomy-actor-runtime-recovery.php',
		'Niwa_AI_Admin_Autonomy_Actor_Runtime'     => 'includes/admin-autonomy/class-admin-autonomy-actor-runtime.php',
		'Niwa_AI_Admin_Autonomy_Background_Tick_Runner' => 'includes/admin-autonomy/class-admin-autonomy-background-tick-runner.php',
		'Niwa_AI_Admin_Autonomy_Background_Clock' => 'includes/admin-autonomy/class-admin-autonomy-background-clock.php',
		'Niwa_AI_Admin_Autonomy_Background_Wake_Loop' => 'includes/admin-autonomy/class-admin-autonomy-background-wake-loop.php',
		'Niwa_AI_Admin_Autonomy_Browser_Message_Context' => 'includes/admin-autonomy/class-admin-autonomy-browser-message-context.php',
		'Niwa_AI_Admin_Autonomy_Browser_Session' => 'includes/admin-autonomy/class-admin-autonomy-browser-session.php',
		'Niwa_AI_Admin_Autonomy_Callbacks'        => 'includes/admin-autonomy/class-admin-autonomy-callbacks.php',
		'Niwa_AI_Admin_Autonomy_Process_Runtime'   => 'includes/admin-autonomy/class-admin-autonomy-process-runtime.php',
		'Niwa_AI_Admin_Autonomy_Runner_Dispatcher' => 'includes/admin-autonomy/class-admin-autonomy-runner-dispatcher.php',
		'Niwa_AI_Admin_Autonomy_Runtime_Command_Handler' => 'includes/admin-autonomy/class-admin-autonomy-runtime-command-handler.php',
		'Niwa_AI_Admin_Autonomy_Runtime_Locks'     => 'includes/admin-autonomy/class-admin-autonomy-runtime-locks.php',
		'Niwa_AI_Admin_Autonomy_Runtime_Stopper'   => 'includes/admin-autonomy/class-admin-autonomy-runtime-stopper.php',
		'Niwa_AI_Admin_Autonomy_Runtime_Trace_Logger' => 'includes/admin-autonomy/class-admin-autonomy-runtime-trace-logger.php',
		'Niwa_AI_Admin_Autonomy_Runtime_Utils'     => 'includes/admin-autonomy/class-admin-autonomy-runtime-utils.php',
		'Niwa_AI_Admin_Autonomy_Task_Checkpoint'   => 'includes/admin-autonomy/class-admin-autonomy-task-checkpoint.php',
		'Niwa_AI_Admin_Autonomy_Task_Integrity'    => 'includes/admin-autonomy/class-admin-autonomy-task-integrity.php',
		'Niwa_AI_Admin_Autonomy_Transcript_Reader' => 'includes/admin-autonomy/class-admin-autonomy-transcript-reader.php',
		'Niwa_AI_Admin_Autonomy_Rollback_Runner'   => 'includes/admin-autonomy/class-admin-autonomy-rollback-runner.php',
		'Niwa_AI_Admin_Autonomy_Scheduled_Task_Runner' => 'includes/admin-autonomy/class-admin-autonomy-scheduled-task-runner.php',
		'Niwa_AI_Admin_Autonomy_Vendor_Integrity_Monitor' => 'includes/admin-autonomy/class-admin-autonomy-vendor-integrity-monitor.php',
		'Niwa_AI_Admin_Autonomy_Turn_Runtime_Policy' => 'includes/admin-autonomy/class-admin-autonomy-turn-runtime-policy.php',
		'Niwa_AI_Admin_Autonomy_Audio_Utils'       => 'includes/admin-autonomy/class-admin-autonomy-audio-utils.php',
		'Niwa_AI_Admin_Autonomy_Chat'              => 'includes/admin-autonomy/class-admin-autonomy-chat.php',
		'Niwa_AI_Admin_Autonomy_Code_Intelligence' => 'includes/admin-autonomy/class-admin-autonomy-code-intelligence.php',
		'Niwa_AI_Admin_Autonomy_Command_Utils'     => 'includes/admin-autonomy/class-admin-autonomy-command-utils.php',
		'Niwa_AI_Admin_Autonomy_Control_Plane'     => 'includes/admin-autonomy/class-admin-autonomy-control-plane.php',
		'Niwa_AI_Admin_Autonomy_Context_Compactor' => 'includes/admin-autonomy/class-admin-autonomy-context-compactor.php',
		'Niwa_AI_Admin_Autonomy_DB_Intelligence'   => 'includes/admin-autonomy/class-admin-autonomy-db-intelligence.php',
		'Niwa_AI_Admin_Autonomy_Email'             => 'includes/admin-autonomy/class-admin-autonomy-email.php',
		'Niwa_AI_Admin_Autonomy_External_Skills'   => 'includes/admin-autonomy/class-admin-autonomy-external-skills.php',
		'Niwa_AI_Admin_Autonomy_IMAP_IDLE_Listener' => 'includes/admin-autonomy/class-admin-autonomy-imap-idle-listener.php',
		'Niwa_AI_Admin_Autonomy_Executor'          => 'includes/admin-autonomy/class-admin-autonomy-executor.php',
		'Niwa_AI_Admin_Autonomy_File_Guard'        => 'includes/admin-autonomy/class-admin-autonomy-file-guard.php',
		'Niwa_AI_Admin_Autonomy_Founder_Message_Executor' => 'includes/admin-autonomy/class-admin-autonomy-founder-message-executor.php',
		'Niwa_AI_Admin_Autonomy_Delivery_Envelope_Normalizer' => 'includes/admin-autonomy/class-admin-autonomy-delivery-envelope-normalizer.php',
		'Niwa_AI_Admin_Autonomy_Founder_Identity_Helper' => 'includes/admin-autonomy/class-admin-autonomy-founder-identity-helper.php',
		'Niwa_AI_Admin_Autonomy_Help_Message_Builder' => 'includes/admin-autonomy/class-admin-autonomy-help-message-builder.php',
		'Niwa_AI_Admin_Autonomy_Loopback_Runner_Executor' => 'includes/admin-autonomy/class-admin-autonomy-loopback-runner-executor.php',
		'Niwa_AI_Admin_Autonomy_Media_Context_Builder' => 'includes/admin-autonomy/class-admin-autonomy-media-context-builder.php',
		'Niwa_AI_Admin_Autonomy_Manual_Diagnostic_Runner' => 'includes/admin-autonomy/class-admin-autonomy-manual-diagnostic-runner.php',
		'Niwa_AI_Admin_Autonomy_Outgoing_Reply_Formatter' => 'includes/admin-autonomy/class-admin-autonomy-outgoing-reply-formatter.php',
		'Niwa_AI_Admin_Autonomy_Pending_Runtime_State' => 'includes/admin-autonomy/class-admin-autonomy-pending-runtime-state.php',
		'Niwa_AI_Admin_Autonomy_Plan_Preview_Builder' => 'includes/admin-autonomy/class-admin-autonomy-plan-preview-builder.php',
		'Niwa_AI_Admin_Autonomy_Post_Reply_Memory_Queue' => 'includes/admin-autonomy/class-admin-autonomy-post-reply-memory-queue.php',
		'Niwa_AI_Admin_Autonomy_Staged_File_Transaction' => 'includes/admin-autonomy/class-admin-autonomy-staged-file-transaction.php',
		'Niwa_AI_Admin_Autonomy_Image_Utils'       => 'includes/admin-autonomy/class-admin-autonomy-image-utils.php',
		'Niwa_AI_Admin_Partner_Product_Image_Context' => 'includes/admin-autonomy/class-admin-partner-product-image-context.php',
		'Niwa_AI_Admin_Partner_Product_Label_Normalizer' => 'includes/admin-autonomy/class-admin-partner-product-label-normalizer.php',
		'Niwa_AI_Admin_Partner_Product_Static_Variant_Media_Parser' => 'includes/admin-autonomy/class-admin-partner-product-static-variant-media-parser.php',
		'Niwa_AI_Admin_Partner_Product_Shopify_Endpoint_Parser' => 'includes/admin-autonomy/class-admin-partner-product-shopify-endpoint-parser.php',
		'Niwa_AI_Admin_Partner_Product_Variant_Media' => 'includes/admin-autonomy/class-admin-partner-product-variant-media.php',
		'Niwa_AI_Admin_Partner_Product_Extractor'  => 'includes/admin-autonomy/class-admin-partner-product-extractor.php',
		'Niwa_AI_Admin_Partner_Product_Cloner'     => 'includes/admin-autonomy/class-admin-partner-product-cloner.php',
		'Niwa_AI_Admin_Product_Management_Service' => 'includes/admin-autonomy/class-admin-product-management-service.php',
		'Niwa_AI_Admin_Product_Target_Resolver'    => 'includes/admin-autonomy/class-admin-product-target-resolver.php',
		'Niwa_AI_Admin_Autonomy_Scheduler'         => 'includes/admin-autonomy/class-admin-autonomy-scheduler.php',
		'Niwa_AI_Admin_Autonomy_Self_Improvement_Manager' => 'includes/admin-autonomy/class-admin-autonomy-self-improvement-manager.php',
		'Niwa_AI_Admin_Autonomy_Session_State'     => 'includes/admin-autonomy/class-admin-autonomy-session-state.php',
		'Niwa_AI_Admin_Autonomy_Session_Actor_Message_Runner' => 'includes/admin-autonomy/class-admin-autonomy-session-actor-message-runner.php',
		'Niwa_AI_Admin_Autonomy_Site_Discovery'    => 'includes/admin-autonomy/class-admin-autonomy-site-discovery.php',
		'Niwa_AI_Admin_Autonomy_Skills'            => 'includes/admin-autonomy/class-admin-autonomy-skills.php',
		'Niwa_AI_Admin_Autonomy_Status_Message_Builder' => 'includes/admin-autonomy/class-admin-autonomy-status-message-builder.php',
		'Niwa_AI_Admin_Autonomy_Store'             => 'includes/admin-autonomy/class-admin-autonomy-store.php',
		'Niwa_AI_Admin_Autonomy_Subagent_Manager'  => 'includes/admin-autonomy/class-admin-autonomy-subagent-manager.php',
		'Niwa_AI_Admin_Autonomy_Tool_Catalog_Index' => 'includes/admin-autonomy/class-admin-autonomy-tool-catalog-index.php',
		'Niwa_AI_Blog_Migrator'                    => 'includes/admin-autonomy/class-niwa-blog-migrator.php',
		'Niwa_AI_Tool_Integrity_Validator'         => 'includes/admin-autonomy/class-niwa-tool-integrity-validator.php',
		'Niwa_AI_Admin_Autonomy_Telegram'          => 'includes/admin-autonomy/class-admin-autonomy-telegram.php',
		'Niwa_AI_Admin_Autonomy_Actor_Intake'      => 'includes/admin-autonomy/class-admin-autonomy-actor-intake.php',
		'Niwa_AI_Admin_Autonomy_Telegram_Authorization' => 'includes/admin-autonomy/class-admin-autonomy-telegram-authorization.php',
		'Niwa_AI_Admin_Autonomy_Telegram_Buffer_Event_Logger' => 'includes/admin-autonomy/class-admin-autonomy-telegram-buffer-event-logger.php',
		'Niwa_AI_Admin_Autonomy_Telegram_Ingress'  => 'includes/admin-autonomy/class-admin-autonomy-telegram-ingress.php',
		'Niwa_AI_Admin_Autonomy_Telegram_Message_Gates' => 'includes/admin-autonomy/class-admin-autonomy-telegram-message-gates.php',
		'Niwa_AI_Admin_Autonomy_Telegram_Message_Context_Builder' => 'includes/admin-autonomy/class-admin-autonomy-telegram-message-context-builder.php',
		'Niwa_AI_Admin_Autonomy_Message_Handler'   => 'includes/admin-autonomy/class-admin-autonomy-message-handler.php',
		'Niwa_AI_Admin_Autonomy_Telegram_Message_Preprocessor' => 'includes/admin-autonomy/class-admin-autonomy-telegram-message-preprocessor.php',
		'Niwa_AI_Admin_Autonomy_Telegram_Media_Fetcher' => 'includes/admin-autonomy/class-admin-autonomy-telegram-media-fetcher.php',
		'Niwa_AI_Admin_Autonomy_Telegram_Poll_Runner' => 'includes/admin-autonomy/class-admin-autonomy-telegram-poll-runner.php',
		'Niwa_AI_Admin_Autonomy_Telegram_Reactions' => 'includes/admin-autonomy/class-admin-autonomy-telegram-reactions.php',
		'Niwa_AI_Admin_Autonomy_Telegram_Voice_Transcriber' => 'includes/admin-autonomy/class-admin-autonomy-telegram-voice-transcriber.php',
		'Niwa_AI_Admin_Autonomy_Telegram_Album_Buffer' => 'includes/admin-autonomy/class-admin-autonomy-telegram-album-buffer.php',
		'Niwa_AI_Admin_Autonomy_Telegram_Sticker_Cache' => 'includes/admin-autonomy/class-admin-autonomy-telegram-sticker-cache.php',
		'Niwa_AI_Admin_Autonomy_Temp_Files'        => 'includes/admin-autonomy/class-admin-autonomy-temp-files.php',
		'Niwa_AI_Admin_Autonomy_WhatsApp'        => 'includes/admin-autonomy/class-admin-autonomy-whatsapp.php',
		'Niwa_AI_Admin_Autonomy_WhatsApp_Access_Policy' => 'includes/admin-autonomy/class-admin-autonomy-whatsapp-access-policy.php',
		'Niwa_AI_Admin_Autonomy_WhatsApp_Command_Access' => 'includes/admin-autonomy/class-admin-autonomy-whatsapp-command-access.php',
		'Niwa_AI_Admin_Autonomy_WhatsApp_Identity' => 'includes/admin-autonomy/class-admin-autonomy-whatsapp-identity.php',
		'Niwa_AI_Admin_Autonomy_WhatsApp_Text_Batcher' => 'includes/admin-autonomy/class-admin-autonomy-whatsapp-text-batcher.php',
		'Niwa_AI_Admin_Autonomy_Document_Parser'   => 'includes/admin-autonomy/class-admin-autonomy-document-parser.php',
		'Niwa_AI_Admin_Autonomy_Timed_Automation_Fallback' => 'includes/admin-autonomy/class-admin-autonomy-timed-automation-fallback.php',
		'Niwa_AI_Admin_Autonomy_Turn_Stream_Buffer' => 'includes/admin-autonomy/class-admin-autonomy-turn-stream-buffer.php',
		'Niwa_AI_Admin_Autonomy_Turn_Stream_Lifecycle' => 'includes/admin-autonomy/class-admin-autonomy-turn-stream-lifecycle.php',
		'Niwa_AI_Admin_Autonomy_Turn_Tool_Status_Messenger' => 'includes/admin-autonomy/class-admin-autonomy-turn-tool-status-messenger.php',
		'Niwa_AI_Admin_Autonomy_Turn_Progress_Callback' => 'includes/admin-autonomy/class-admin-autonomy-turn-progress-callback.php',
		'Niwa_AI_Admin_Autonomy_Typing_Feedback'   => 'includes/admin-autonomy/class-admin-autonomy-typing-feedback.php',
		'Niwa_AI_Admin_Autonomy_Turn_Tool_Progress_Translator' => 'includes/admin-autonomy/class-admin-autonomy-turn-tool-progress-translator.php',
		'Niwa_AI_Admin_Autonomy_Tools'             => 'includes/admin-autonomy/class-admin-autonomy-tools.php',
		'Niwa_AI_Admin_Autonomy_Turn_Engine'       => 'includes/admin-autonomy/class-admin-autonomy-turn-engine.php',
		'Niwa_AI_Admin_Autonomy_Workspace'         => 'includes/admin-autonomy/class-admin-autonomy-workspace.php',
		'Niwa_AI_Admin_Autonomy_Spreadsheet_Reader' => 'includes/admin-autonomy/class-admin-autonomy-spreadsheet-reader.php',

		// ── Agent Loop Classes (Pi-like architecture) ──
		'Niwa_AI_Agent_Context'                    => 'includes/admin-autonomy/agent-loop/class-agent-context.php',
		'Niwa_AI_Agent_Event_Emitter'              => 'includes/admin-autonomy/agent-loop/class-agent-event-emitter.php',
		'Niwa_AI_Agent_Stream_Reader'              => 'includes/admin-autonomy/agent-loop/class-agent-stream-reader.php',
		'Niwa_AI_Error_Recovery'                   => 'includes/admin-autonomy/agent-loop/class-error-recovery.php',
		'Niwa_AI_Context_Window_Guard'             => 'includes/admin-autonomy/agent-loop/class-context-window-guard.php',
		'Niwa_AI_Message_Validator'                => 'includes/admin-autonomy/agent-loop/class-message-validator.php',
		'Niwa_AI_Agent_Tool_Execution_Outcome'     => 'includes/admin-autonomy/agent-loop/class-agent-tool-execution-outcome.php',
		'Niwa_AI_Agent_Terminal_Audit'             => 'includes/admin-autonomy/agent-loop/class-agent-terminal-audit.php',
		'Niwa_AI_Agent_Workspace_Apply_Finalizer'  => 'includes/admin-autonomy/agent-loop/class-agent-workspace-apply-finalizer.php',
		'Niwa_AI_Agent_Tool_Payload_Normalizer'    => 'includes/admin-autonomy/agent-loop/class-agent-tool-payload-normalizer.php',
		'Niwa_AI_Agent_Empty_Response_Diagnostic'  => 'includes/admin-autonomy/agent-loop/class-agent-empty-response-diagnostic.php',
		'Niwa_AI_Agent_Message_Utils'              => 'includes/admin-autonomy/agent-loop/class-agent-message-utils.php',
		'Niwa_AI_Agent_Pseudo_Tool_Parser'         => 'includes/admin-autonomy/agent-loop/class-agent-pseudo-tool-parser.php',
		'Niwa_AI_Agent_Token_Usage'                => 'includes/admin-autonomy/agent-loop/class-agent-token-usage.php',
		'Niwa_AI_Agent_Tool_Call_Utils'            => 'includes/admin-autonomy/agent-loop/class-agent-tool-call-utils.php',
		'Niwa_AI_Agent_Loop'                       => 'includes/admin-autonomy/agent-loop/class-agent-loop.php',

		// ── Main Plugin Traits (used by Niwa_AI_Plugin) ──
		'Niwa_AI_Admin_Dashboard'                  => 'includes/traits/trait-niwa-admin-dashboard.php',
		'Niwa_AI_Admin_Chat_Queue'                 => 'includes/traits/trait-niwa-admin-chat-queue.php',
		'Niwa_AI_Ajax_Handlers'                    => 'includes/traits/trait-niwa-ajax-handlers.php',
		'Niwa_AI_Frontend_Chat'                    => 'includes/traits/trait-niwa-frontend-chat.php',
		'Niwa_AI_Frontend_Queue'                   => 'includes/traits/trait-niwa-frontend-queue.php',

		// ── Admin Trait ──
		'Niwa_AI_Admin_Field_Helpers'              => 'admin/trait-admin-field-helpers.php',

		// ── Admin-Autonomy Tools Traits (used by Niwa_AI_Admin_Autonomy_Tools) ──
		'Niwa_AI_Tools_Registry'                   => 'includes/admin-autonomy/traits/trait-tools-registry.php',
		'Niwa_AI_Tools_Product_Ops'                => 'includes/admin-autonomy/traits/trait-tools-product-ops.php',
		'Niwa_AI_Tools_Variation_Ops'              => 'includes/admin-autonomy/traits/trait-tools-variation-ops.php',
		'Niwa_AI_Tools_Product_Search'             => 'includes/admin-autonomy/traits/trait-tools-product-search.php',
		'Niwa_AI_Tools_Bulk_Pricing'               => 'includes/admin-autonomy/traits/trait-tools-bulk-pricing.php',
		'Niwa_AI_Tools_Content_Ops'                => 'includes/admin-autonomy/traits/trait-tools-content-ops.php',
		'Niwa_AI_Tools_Elementor'                  => 'includes/admin-autonomy/traits/trait-tools-elementor.php',
		'Niwa_AI_Tools_File_Ops'                   => 'includes/admin-autonomy/traits/trait-tools-file-ops.php',
		'Niwa_AI_Tools_Email'                      => 'includes/admin-autonomy/traits/trait-tools-email.php',
		'Niwa_AI_Tools_Skills'                     => 'includes/admin-autonomy/traits/trait-tools-skills.php',
		'Niwa_AI_Tools_Blog_Posts'                 => 'includes/admin-autonomy/traits/trait-tools-blog-posts.php',
		'Niwa_AI_Tools_Rollback'                   => 'includes/admin-autonomy/traits/trait-tools-rollback.php',
		'Niwa_AI_Tools_Individual_Pricing'         => 'includes/admin-autonomy/traits/trait-tools-individual-pricing.php',
		'Niwa_AI_Tools_Users'                      => 'includes/admin-autonomy/traits/trait-tools-users.php',
		'Niwa_AI_Tools_Taxonomy'                   => 'includes/admin-autonomy/traits/trait-tools-taxonomy.php',
		'Niwa_AI_Tools_Settings'                   => 'includes/admin-autonomy/traits/trait-tools-settings.php',
		'Niwa_AI_Tools_Niwa_Settings'              => 'includes/admin-autonomy/traits/trait-tools-niwa-settings.php',
		'Niwa_AI_Tools_Pages'                      => 'includes/admin-autonomy/traits/trait-tools-pages.php',
		'Niwa_AI_Tools_Media_Management'           => 'includes/admin-autonomy/traits/trait-tools-media-management.php',
		'Niwa_AI_Tools_Elementor_Widgets'          => 'includes/admin-autonomy/traits/trait-tools-elementor-widgets.php',
		'Niwa_AI_Tools_Elementor_URL_Builder'      => 'includes/admin-autonomy/traits/trait-tools-elementor-url-builder.php',
		'Niwa_AI_Tools_Woo_Orders'                 => 'includes/admin-autonomy/traits/trait-tools-woo-orders.php',
		'Niwa_AI_Tools_Cache'                      => 'includes/admin-autonomy/traits/trait-tools-cache.php',
		'Niwa_AI_Tools_Woo_Taxonomy'               => 'includes/admin-autonomy/traits/trait-tools-woo-taxonomy.php',
		'Niwa_AI_Tools_WP_Extras'                  => 'includes/admin-autonomy/traits/trait-tools-wp-extras.php',
		'Niwa_AI_Tools_Bloging'                    => 'includes/admin-autonomy/traits/trait-tools-bloging.php',
		'Niwa_AI_Tools_Blog_Pipeline'              => 'includes/admin-autonomy/traits/trait-tools-blog-pipeline.php',
		'Niwa_AI_Tools_Template_Library'           => 'includes/admin-autonomy/traits/trait-tools-template-library.php',

		// ── MCP Classes ──
		'Niwa_AI_MCP_JWT_Auth'                     => 'includes/mcp/class-niwa-mcp-jwt-auth.php',
		'Niwa_AI_MCP_Tools_Bridge'                 => 'includes/mcp/class-niwa-mcp-tools-bridge.php',
		'Niwa_AI_MCP_Server'                       => 'includes/mcp/class-niwa-mcp-server.php',
		'Niwa_AI_Admin_MCP_Settings'               => 'admin/class-admin-mcp-settings.php',

		// ── Admin-Autonomy Chat Traits (used by Niwa_AI_Admin_Autonomy_Chat) ──
		'Niwa_AI_Chat_Tool_Definitions'            => 'includes/admin-autonomy/traits/trait-chat-tool-definitions.php',
		'Niwa_AI_Chat_System_Instructions'         => 'includes/admin-autonomy/traits/trait-chat-system-instructions.php',
		'Niwa_AI_Chat_Heartbeat'                   => 'includes/admin-autonomy/traits/trait-chat-heartbeat.php',
		'Niwa_AI_Chat_Metrics'                     => 'includes/admin-autonomy/traits/trait-chat-metrics.php',
		'Niwa_AI_Chat_Site_Profile'                => 'includes/admin-autonomy/traits/trait-chat-site-profile.php',
		'Niwa_AI_Chat_Onboarding'                  => 'includes/admin-autonomy/traits/trait-chat-onboarding.php',
		'Niwa_AI_Chat_Session'                     => 'includes/admin-autonomy/traits/trait-chat-session.php',
		'Niwa_AI_Chat_Tool_Executor'               => 'includes/admin-autonomy/traits/trait-chat-tool-executor.php',
		'Niwa_AI_Chat_Media'                       => 'includes/admin-autonomy/traits/trait-chat-media.php',
		'Niwa_AI_Chat_Content_Access'              => 'includes/admin-autonomy/traits/trait-chat-content-access.php',
		'Niwa_AI_Chat_Code_Search'                 => 'includes/admin-autonomy/traits/trait-chat-code-search.php',
		'Niwa_AI_Chat_Daily_Reports'               => 'includes/admin-autonomy/traits/trait-chat-daily-reports.php',
		'Niwa_AI_Chat_Context'                     => 'includes/admin-autonomy/traits/trait-chat-context.php',
	);

	/**
	 * Register the autoloader with spl_autoload_register.
	 */
	public static function register() {
		spl_autoload_register( array( __CLASS__, 'load' ) );
	}

	/**
	 * Autoload callback.
	 *
	 * @param string $class The fully-qualified class/trait/interface name.
	 */
	public static function load( $class ) {
		if ( ! isset( self::$classmap[ $class ] ) ) {
			return;
		}

		$file = NIWA_AI_DIR . self::$classmap[ $class ];

		if ( file_exists( $file ) ) {
			require_once $file;
		}
	}
}
