<?php
/**
 * Premium modules entry point.
 *
 * Loads and bootstraps all available premium features.
 * Each module is conditionally loaded only if its file exists.
 *
 * @package Replyan_AI
 */

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

function replyan_ai_premium_load_modules() {

	// Agent Takeover is available for all plans (Free & Pro)
	$agent_takeover_path = REPLYAN_AI_DIR . 'includes/premium/agent-takeover/class-replyan-ai-agent-takeover.php';
	if ( file_exists( $agent_takeover_path ) ) {
		require_once $agent_takeover_path;
	}

	if ( ! Replyan_AI_License::isPro() ) {
		return;
	}

	$replyan_premium_modules = array(
		'notes'                 => 'class-replyan-ai-notes.php',
		'leads'                 => 'class-replyan-ai-leads.php',
		'forms'                 => 'class-replyan-ai-forms.php',
		'conversation-insights' => 'class-replyan-conversation-insights.php',
		'meta'                  => 'class-replyan-ai-meta.php',
	);

	foreach ( $replyan_premium_modules as $folder => $file ) {
		$path = REPLYAN_AI_DIR . "includes/premium/{$folder}/{$file}";
		if ( file_exists( $path ) ) {
			require_once $path;

			// Load sub-modules for leads
			if ( 'leads' === $folder ) {
				$capture_path = REPLYAN_AI_DIR . "includes/premium/{$folder}/class-replyan-ai-capture-leads.php";
				if ( file_exists( $capture_path ) ) {
					require_once $capture_path;
				}
			}

			// Load sub-modules for conversation insights
			if ( 'conversation-insights' === $folder ) {
				$analyzer_path = REPLYAN_AI_DIR . "includes/premium/{$folder}/class-replyan-conversation-analyzer.php";
				if ( file_exists( $analyzer_path ) ) {
					require_once $analyzer_path;
				}
			}
		}
	}
}

replyan_ai_premium_load_modules();

/**
 * Register hooks for all loaded premium modules.
 */
function replyan_ai_premium_register_hooks(): void {
	if ( class_exists( 'Replyan_AI_Agent_Takeover' ) ) {
		( new Replyan_AI_Agent_Takeover() )->register_hooks();
	}

	if ( class_exists( 'Replyan_AI_Notes' ) ) {
		( new Replyan_AI_Notes() )->register_hooks();
	}

	if ( class_exists( 'Replyan_AI_Leads' ) ) {
		( new Replyan_AI_Leads() )->register_hooks();
	}

	if ( class_exists( 'Replyan_AI_Forms' ) ) {
		( new Replyan_AI_Forms() )->register_hooks();
	}

	if ( class_exists( 'Replyan_AI_Capture_Leads' ) ) {
		( new Replyan_AI_Capture_Leads() )->register_hooks();
	}

	if ( class_exists( 'Replyan_Conversation_Insights' ) ) {
		( new Replyan_Conversation_Insights() )->register_hooks();
	}

	if ( class_exists( 'Replyan_Conversation_Analyzer' ) ) {
		( new Replyan_Conversation_Analyzer() )->register_hooks();
	}

	if ( class_exists( 'Replyan_AI_Meta' ) ) {
		( new Replyan_AI_Meta() )->register_hooks();
	}
}

/**
 * Create tables for all loaded premium modules.
 */
function replyan_ai_premium_create_tables(): void {
	if ( class_exists( 'Replyan_AI_Notes' ) ) {
		Replyan_AI_Notes::create_table();
	}

	if ( class_exists( 'Replyan_AI_Leads' ) ) {
		Replyan_AI_Leads::create_table();
	}

	if ( class_exists( 'Replyan_AI_Forms' ) ) {
		Replyan_AI_Forms::create_table();
	}

	if ( class_exists( 'Replyan_Conversation_Insights' ) ) {
		Replyan_Conversation_Insights::create_table();
	}
}
