<?php
/**
 * REST API Router
 *
 * @package Ugcify\Admin
 */

namespace Ugcify\Admin;

use Ugcify\Helpers\FeatureGate;

/**
 * Class API
 *
 * Main API router that registers all API endpoints
 */
class API {
	/**
	 * Register all API routes
	 */
	public function register_routes() {
		// Reviews API.
		require_once UGCIFY_PATH . 'includes/Review/HelpfulVotes.php';
		require_once UGCIFY_PATH . 'includes/Admin/API/ReviewsAPI.php';
		$reviews_api = new API\ReviewsAPI();
		$reviews_api->register_routes();

		require_once UGCIFY_PATH . 'includes/Admin/API/HelpfulVotesAPI.php';
		$helpful_votes_api = new API\HelpfulVotesAPI();
		$helpful_votes_api->register_routes();

		// Reviews Bulk API.
		require_once UGCIFY_PATH . 'includes/Admin/API/ReviewsBulkAPI.php';
		$bulk_api = new API\ReviewsBulkAPI();
		$bulk_api->register_routes();

		// Settings API.
		require_once UGCIFY_PATH . 'includes/Admin/API/SettingsAPI.php';
		$settings_api = new API\SettingsAPI();
		$settings_api->register_routes();

		// Review Submission API (Frontend).
		require_once UGCIFY_PATH . 'includes/Admin/API/ReviewAPI.php';
		$review_api = new API\ReviewAPI();
		$review_api->register_routes();

		// Creator portal API (public + logged-in creator endpoints).
		if ( FeatureGate::is_enabled( 'influencer_portal' ) && file_exists( UGCIFY_PATH . 'includes/Influencer/InvitationService.php' ) ) {
			require_once UGCIFY_PATH . 'includes/Admin/API/PortalAPI.php';
			$portal_api = new API\PortalAPI();
			$portal_api->register_routes();
		}

		// Email Settings API.
		require_once UGCIFY_PATH . 'includes/Admin/API/EmailSettingsAPI.php';
		$email_api = new API\EmailSettingsAPI();
		$email_api->register_routes();

		// Spam Settings API.
		require_once UGCIFY_PATH . 'includes/Admin/API/SpamSettingsAPI.php';
		$spam_api = new API\SpamSettingsAPI();
		$spam_api->register_routes();
		
		// Review Migration API.
		require_once UGCIFY_PATH . 'includes/Admin/API/ReviewMigrationAPI.php';
		$review_migration_api = new API\ReviewMigrationAPI();
		$review_migration_api->register_routes();

		// Dashboard API.
		require_once UGCIFY_PATH . 'includes/Admin/API/DashboardAPI.php';
		$dashboard_api = new API\DashboardAPI();
		$dashboard_api->register_routes();

		// Analytics API (event tracking + dashboard overview).
		require_once UGCIFY_PATH . 'includes/Admin/API/AnalyticsAPI.php';
		$analytics_api = new API\AnalyticsAPI();
		$analytics_api->register_routes();

		// Setup Wizard API.
		require_once UGCIFY_PATH . 'includes/Admin/API/SetupWizardAPI.php';
		$setup_wizard_api = new API\SetupWizardAPI();
		$setup_wizard_api->register_routes();

		// Onboarding Tour API.
		require_once UGCIFY_PATH . 'includes/Admin/API/OnboardingTourAPI.php';
		$onboarding_tour_api = new API\OnboardingTourAPI();
		$onboarding_tour_api->register_routes();

		// Plugins API (wizard install/activate).
		require_once UGCIFY_PATH . 'includes/Admin/API/PluginsAPI.php';
		$plugins_api = new API\PluginsAPI();
		$plugins_api->register_routes();

		// Block Editor data API (always needed for gallery block).
		require_once UGCIFY_PATH . 'includes/Admin/API/BlockDataAPI.php';
		$block_data_api = new API\BlockDataAPI();
		$block_data_api->register_routes();

		// GROWTH: social aggregator API.
		if ( FeatureGate::is_enabled( 'social_import' ) && file_exists( UGCIFY_PATH . 'includes/Integrations/Social/SocialManager.php' ) ) {
			require_once UGCIFY_PATH . 'includes/Admin/API/SocialAPI.php';
			$social_api = new API\SocialAPI();
			$social_api->register_routes();
		}

		// Social share API (public share token create/track).
		require_once UGCIFY_PATH . 'includes/Admin/API/SocialShareAPI.php';
		$social_share_api = new API\SocialShareAPI();
		$social_share_api->register_routes();

		// GROWTH: review ↔ product linker API.
		if ( FeatureGate::is_enabled( 'product_tagging' ) && file_exists( UGCIFY_PATH . 'includes/Admin/API/LinkerAPI.php' ) ) {
			require_once UGCIFY_PATH . 'includes/Admin/API/LinkerAPI.php';
			$linker_api = new API\LinkerAPI();
			$linker_api->register_routes();

			// Product hotspot tags API.
			require_once UGCIFY_PATH . 'includes/Admin/API/ProductTagsAPI.php';
			$product_tags_api = new API\ProductTagsAPI();
			$product_tags_api->register_routes();

			// AI-assisted hotspot suggestions API.
			require_once UGCIFY_PATH . 'includes/AI/AIJobRepository.php';
			require_once UGCIFY_PATH . 'includes/AI/AIProviderInterface.php';
			require_once UGCIFY_PATH . 'includes/AI/AISettings.php';
			require_once UGCIFY_PATH . 'includes/AI/AbstractAIProvider.php';
			require_once UGCIFY_PATH . 'includes/AI/WordPressAIProvider.php';
			require_once UGCIFY_PATH . 'includes/AI/AnthropicProvider.php';
			require_once UGCIFY_PATH . 'includes/AI/OpenAIProvider.php';
			require_once UGCIFY_PATH . 'includes/AI/GeminiProvider.php';
			require_once UGCIFY_PATH . 'includes/AI/AIProviderFactory.php';
			require_once UGCIFY_PATH . 'includes/AI/AnthropicClient.php';
			require_once UGCIFY_PATH . 'includes/AI/ProductDescriptorService.php';
			require_once UGCIFY_PATH . 'includes/AI/CandidateProductService.php';
			require_once UGCIFY_PATH . 'includes/AI/HotspotSuggestionService.php';
			require_once UGCIFY_PATH . 'includes/Admin/API/AIHotspotAPI.php';
			$ai_hotspot_api = new API\AIHotspotAPI();
			$ai_hotspot_api->register_routes();
		}

		// GROWTH: automation API (post-purchase email triggers).
		if ( FeatureGate::is_enabled( 'email_automation' ) && file_exists( UGCIFY_PATH . 'includes/Integrations/MailMint/MailMint.php' ) ) {
			require_once UGCIFY_PATH . 'includes/Admin/API/AutomationAPI.php';
			$automation_api = new API\AutomationAPI();
			$automation_api->register_routes();
		}

		// Reward settings API — always registered so admins can configure
		// reward/badge settings regardless of license plan. The actual
		// coupon-firing and gamification logic is still gated by FeatureGate.
		require_once UGCIFY_PATH . 'includes/Admin/API/RewardSettingsAPI.php';
		$reward_api = new API\RewardSettingsAPI();
		$reward_api->register_routes();

		// BUSINESS: user rewards API (frontend points balance).
		if ( FeatureGate::is_enabled( 'advanced_gamification' ) && file_exists( UGCIFY_PATH . 'includes/Points/PointsManager.php' ) ) {
			require_once UGCIFY_PATH . 'includes/Admin/API/UserRewardsAPI.php';
			$user_rewards_api = new API\UserRewardsAPI();
			$user_rewards_api->register_routes();
		}

		// BUSINESS: A/B testing API.
		if ( FeatureGate::is_enabled( 'ab_testing' ) && file_exists( UGCIFY_PATH . 'includes/Analytics/ABTestAnalytics.php' ) ) {
			require_once UGCIFY_PATH . 'includes/Admin/API/ABTestingAPI.php';
			$ab_testing_api = new API\ABTestingAPI();
			$ab_testing_api->register_routes();
		}

		// GROWTH: S3 storage settings API (autoloaded via Composer PSR-4).
		// Guard on file_exists: in the FREE build these files are stripped, but an
		// active pro license still passes the gate — without the guard PHP fatals
		// with "Class StorageSettingsAPI not found" until the pro build is installed.
		if ( FeatureGate::is_enabled( 's3_storage' )
			&& file_exists( UGCIFY_PATH . 'includes/Admin/API/StorageSettingsAPI.php' )
		) {
			require_once UGCIFY_PATH . 'includes/Admin/API/StorageSettingsAPI.php';
			$storage_api = new API\StorageSettingsAPI();
			$storage_api->register_routes();

			// S3 media migration API.
			require_once UGCIFY_PATH . 'includes/Admin/API/StorageMigrationAPI.php';
			$storage_migration_api = new API\StorageMigrationAPI();
			$storage_migration_api->register_routes();
		}

		// Influencer APIs — only when the Influencer module is present.
		if ( FeatureGate::is_enabled( 'influencer_portal' ) && file_exists( UGCIFY_PATH . 'includes/Influencer/InvitationService.php' ) ) {
			// Influencer invitation management API.
			require_once UGCIFY_PATH . 'includes/Admin/API/InfluencerInvitationsAPI.php';
			$influencer_invitations_api = new API\InfluencerInvitationsAPI();
			$influencer_invitations_api->register_routes();

			// Influencer creator management API.
			require_once UGCIFY_PATH . 'includes/Admin/API/InfluencerCreatorsAPI.php';
			$influencer_creators_api = new API\InfluencerCreatorsAPI();
			$influencer_creators_api->register_routes();

			// Influencer portal settings API.
			require_once UGCIFY_PATH . 'includes/Admin/API/InfluencerSettingsAPI.php';
			$influencer_settings_api = new API\InfluencerSettingsAPI();
			$influencer_settings_api->register_routes();
		}
	}
}
