<?php
/**
 * Manager for Knowledge Sources.
 *
 * @package Replyan_AI
 */

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

class Replyan_AI_Source_Manager {
	private array $providers = array();

	public function __construct() {
		$settings = Replyan_AI_Settings::get();
		$sources  = $settings['knowledge_sources'] ?? array();

		if ( in_array( 'post', $sources, true ) ) {
			$this->providers[] = new Replyan_AI_Post_Source_Provider();
		}
		if ( in_array( 'page', $sources, true ) ) {
			$this->providers[] = new Replyan_AI_Page_Source_Provider();
		}
		
		if ( Replyan_AI_License::isPro() ) {
			if ( in_array( 'product', $sources, true ) && class_exists( 'WooCommerce' ) ) {
				$this->providers[] = new Replyan_AI_Product_Source_Provider();
			}

			foreach ( $sources as $source ) {
				if ( str_starts_with( $source, 'cpt:' ) ) {
					$slug = substr( $source, 4 );
					if ( post_type_exists( $slug ) ) {
						$this->providers[] = new Replyan_AI_CPT_Source_Provider( $slug );
					}
				}
			}
		}
	}

	public function getDocuments(): array {
		$all_documents = array();

		foreach ( $this->providers as $provider ) {
			$docs = $provider->getDocuments();
			$all_documents = array_merge( $all_documents, $docs );
		}

		return $all_documents;
	}
}
