<?php
/**
 * Conditional single loader: require_once plugin PHP files by bucket and request type.
 * BASE + COMPAT + BACKGROUND always; ADMIN-UI when admin. REST bucket loaded on rest_api_init via fotogenic_ai_load_rest_bucket().
 *
 * @package Fotogenic_AI
 */

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

$fotogenic_ai_path = FOTOGENIC_AI_PATH;

if ( defined( 'FOTOGENIC_AI_LOAD_DEBUG' ) && FOTOGENIC_AI_LOAD_DEBUG ) {
	$GLOBALS['fotogenic_ai_loaded_buckets'] = [];
}

$fotogenic_ai_bucket = function ( string $name ): void {
	if ( isset( $GLOBALS['fotogenic_ai_loaded_buckets'] ) ) {
		$GLOBALS['fotogenic_ai_loaded_buckets'][] = $name;
	}
};

// --- BASE (always) ---
$fotogenic_ai_bucket( 'base' );
require_once $fotogenic_ai_path . 'includes/core/helpers.php';
require_once $fotogenic_ai_path . 'includes/core/class-platform-capabilities.php';
require_once $fotogenic_ai_path . 'includes/core/store-queue/class-store-queue-repo.php';
require_once $fotogenic_ai_path . 'includes/core/store-queue/class-store-queue-scheduler.php';
require_once $fotogenic_ai_path . 'includes/core/class-universal-data-migration.php';
require_once $fotogenic_ai_path . 'includes/core/interface-product-query-adapter.php';
require_once $fotogenic_ai_path . 'includes/core/interface-product-image-source-adapter.php';
require_once $fotogenic_ai_path . 'includes/core/interface-product-media-write-adapter.php';
require_once $fotogenic_ai_path . 'includes/core/interface-media-query-adapter.php';
require_once $fotogenic_ai_path . 'includes/core/interface-post-query-adapter.php';
require_once $fotogenic_ai_path . 'includes/core/interface-page-query-adapter.php';
require_once $fotogenic_ai_path . 'includes/core/interface-studio-cache-invalidator.php';
require_once $fotogenic_ai_path . 'includes/core/class-product-query-adapter-fallback.php';
require_once $fotogenic_ai_path . 'includes/core/class-product-image-source-adapter-fallback.php';
require_once $fotogenic_ai_path . 'includes/core/class-product-media-write-adapter-fallback.php';
require_once $fotogenic_ai_path . 'includes/core/services/workflows/class-upstream-identifier.php';
require_once $fotogenic_ai_path . 'includes/core/class-logger.php';
require_once $fotogenic_ai_path . 'includes/core/class-messages.php';
require_once $fotogenic_ai_path . 'includes/core/services/workflows/class-workflow-operational-limits.php';
require_once $fotogenic_ai_path . 'includes/core/services/workflows/class-workflow-quality-catalog.php';
require_once $fotogenic_ai_path . 'includes/core/services/workflows/class-upstream-request-identity.php';
require_once $fotogenic_ai_path . 'includes/core/class-api-endpoints.php';
require_once $fotogenic_ai_path . 'includes/core/class-settings.php';
require_once $fotogenic_ai_path . 'includes/core/class-auth-refresh-lock.php';
require_once $fotogenic_ai_path . 'includes/core/class-auth-state-store.php';
require_once $fotogenic_ai_path . 'includes/core/class-auth-client.php';
require_once $fotogenic_ai_path . 'includes/core/class-auth-session.php';
require_once $fotogenic_ai_path . 'includes/core/class-api-client.php';
require_once $fotogenic_ai_path . 'includes/core/class-credits-service.php';
require_once $fotogenic_ai_path . 'includes/core/class-experience-state-store.php';
require_once $fotogenic_ai_path . 'includes/core/class-notification-activity-store.php';
require_once $fotogenic_ai_path . 'includes/core/class-image-source-resolver.php';
require_once $fotogenic_ai_path . 'includes/core/class-job-manager.php';
require_once $fotogenic_ai_path . 'includes/core/class-media-saver.php';
require_once $fotogenic_ai_path . 'includes/core/class-presets.php';
require_once $fotogenic_ai_path . 'includes/core/class-price-service.php';
require_once $fotogenic_ai_path . 'includes/core/class-selection-schema.php';
require_once $fotogenic_ai_path . 'includes/core/services/class-service-configuration.php';
require_once $fotogenic_ai_path . 'includes/core/class-config-provider.php';
require_once $fotogenic_ai_path . 'includes/core/class-studio-targets-manifest.php';
require_once $fotogenic_ai_path . 'includes/core/class-studio-cache-store.php';
require_once $fotogenic_ai_path . 'includes/core/class-bulk-cache-version.php';
require_once $fotogenic_ai_path . 'includes/core/background/class-background-runtime-state.php';
require_once $fotogenic_ai_path . 'includes/core/services/workflows/interface-workflow-definition.php';
require_once $fotogenic_ai_path . 'includes/core/services/workflows/class-workflow-definition.php';
require_once $fotogenic_ai_path . 'includes/core/services/workflows/class-workflow-contract-guard.php';
require_once $fotogenic_ai_path . 'includes/core/services/workflows/class-workflow-registry.php';
require_once $fotogenic_ai_path . 'includes/core/class-studio-toolbar-view-model.php';
require_once $fotogenic_ai_path . 'includes/core/services/workflows/class-workflow-item-identity.php';
require_once $fotogenic_ai_path . 'includes/core/services/workflows/class-workflow-draft-normalizer.php';
require_once $fotogenic_ai_path . 'includes/core/services/workflows/class-campaign-set-recipe.php';
require_once $fotogenic_ai_path . 'includes/core/services/workflows/class-generate-images-multipart-builder.php';
require_once $fotogenic_ai_path . 'includes/core/services/workflows/class-campaign-set-multipart-builder.php';
require_once $fotogenic_ai_path . 'includes/core/services/workflows/class-increase-resolution-multipart-builder.php';
require_once $fotogenic_ai_path . 'includes/core/services/workflows/class-workflow-source-preflight.php';
require_once $fotogenic_ai_path . 'includes/core/services/workflows/class-workflow-configuration-authority.php';
require_once $fotogenic_ai_path . 'includes/core/services/workflows/class-workflow-accepted-authority.php';
require_once $fotogenic_ai_path . 'includes/core/services/workflows/class-workflow-preflight.php';
require_once $fotogenic_ai_path . 'includes/core/services/generate/class-generate-output-map.php';
require_once $fotogenic_ai_path . 'includes/core/services/generate/class-generate-campaign-admission.php';
require_once $fotogenic_ai_path . 'includes/core/services/generate/class-generate-result-authority.php';
require_once $fotogenic_ai_path . 'includes/core/services/workflows/class-workflow-response-factory.php';
require_once $fotogenic_ai_path . 'includes/core/services/workflows/class-workflow-upstream-response-parser.php';
require_once $fotogenic_ai_path . 'includes/core/services/workflows/class-workflow-request-reconciler.php';
require_once $fotogenic_ai_path . 'includes/core/services/workflows/class-workflow-request-store.php';
require_once $fotogenic_ai_path . 'includes/core/services/workflows/class-workflow-credit-settlement.php';
require_once $fotogenic_ai_path . 'includes/core/services/workflows/class-workflow-start-coordinator.php';
require_once $fotogenic_ai_path . 'includes/core/services/workflows/class-service-campaign-set-start.php';
require_once $fotogenic_ai_path . 'includes/core/services/workflows/class-service-increase-resolution-start.php';
require_once $fotogenic_ai_path . 'includes/core/services/workflows/class-universal-start-service.php';
require_once $fotogenic_ai_path . 'includes/core/bulk/class-bulk-repo.php';
require_once $fotogenic_ai_path . 'includes/core/bulk/class-bulk-scheduler.php';
require_once $fotogenic_ai_path . 'includes/core/bulk/class-universal-bulk-item-dispatcher.php';
require_once $fotogenic_ai_path . 'includes/core/bulk/class-bulk-runner.php';
require_once $fotogenic_ai_path . 'includes/core/services/class-service-bulk.php';
require_once $fotogenic_ai_path . 'includes/core/store-queue/class-store-queue-snapshot-builder.php';
require_once $fotogenic_ai_path . 'includes/core/store-queue/class-store-queue-preflight.php';
require_once $fotogenic_ai_path . 'includes/core/services/class-service-store-queue.php';
require_once $fotogenic_ai_path . 'includes/core/store-queue/class-store-queue-runner.php';

// --- COMPAT (always; single source: load-compat.php requires WooCommerce adapters) ---
$fotogenic_ai_bucket( 'compat' );
require_once $fotogenic_ai_path . 'includes/compat/load-compat.php';
require_once $fotogenic_ai_path . 'includes/compat/class-compat-manager.php';

// --- BACKGROUND (always; cron/finalizer runs outside admin) ---
$fotogenic_ai_bucket( 'background' );
require_once $fotogenic_ai_path . 'includes/core/services/generate/class-generate-batch-response-parser.php';
require_once $fotogenic_ai_path . 'includes/core/services/generate/class-generate-job-finalizer.php';
require_once $fotogenic_ai_path . 'includes/core/services/class-service-history.php';
require_once $fotogenic_ai_path . 'includes/core/services/generate/class-generate-terminal-reconciler.php';
require_once $fotogenic_ai_path . 'includes/core/services/class-service-library.php';
require_once $fotogenic_ai_path . 'includes/core/services/generate/class-service-generate-start.php';
require_once $fotogenic_ai_path . 'includes/core/services/generate/class-service-generate-save.php';
require_once $fotogenic_ai_path . 'includes/core/services/generate/class-service-generate-poll.php';
require_once $fotogenic_ai_path . 'includes/core/services/generate/class-service-generate-active.php';
require_once $fotogenic_ai_path . 'includes/core/class-plugin-cache-refresher.php';
require_once $fotogenic_ai_path . 'includes/core/background/class-background-lock.php';
require_once $fotogenic_ai_path . 'includes/core/services/workflows/class-result-delivery-coordinator.php';
require_once $fotogenic_ai_path . 'includes/core/background/class-background-scheduler.php';
require_once $fotogenic_ai_path . 'includes/core/background/class-background-finalizer.php';

/**
 * Load REST bucket (services, router, handlers, Rest). Call from rest_api_init so routes register reliably.
 */
function fotogenic_ai_load_rest_bucket(): void {
	require_once FOTOGENIC_AI_PATH . 'includes/load-rest.php';
}

if ( is_admin() ) {
	// --- ADMIN-UI (admin only) ---
	$fotogenic_ai_bucket( 'admin-ui' );
	require_once $fotogenic_ai_path . 'includes/core/class-assets-manifest.php';
	require_once $fotogenic_ai_path . 'includes/core/class-admin.php';
	require_once $fotogenic_ai_path . 'includes/studio/modules/class-modules-registry.php';
	require_once $fotogenic_ai_path . 'includes/studio/modules/class-studio-module.php';
	require_once $fotogenic_ai_path . 'includes/studio/class-studio-page.php';
	require_once $fotogenic_ai_path . 'includes/settings/class-settings-page.php';
}
