<?php
/**
 * Answyr — Knowledge sources helper.
 *
 * Describes which post types the AI can draw on as knowledge sources. The
 * selection UI + retrieval preview live in Settings ("Knowledge sources"); this
 * class just supplies the detected-source list the view renders.
 *
 * @package Answyr
 */

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

final class Answyr_Knowledge_Admin {

	/**
	 * Every detected post type with its label, description, published count, and
	 * whether it's currently excluded as a knowledge source.
	 *
	 * @return array<int,array{type:string,label:string,description:string,count:int,excluded:bool}>
	 */
	public static function sources(): array {
		$retrieval = new Answyr_Retrieval();
		$excluded  = (array) Answyr_Settings::get( 'excluded_post_types', array() );
		$out       = array();
		foreach ( $retrieval->detected_post_types() as $type ) {
			$counts = wp_count_posts( $type );
			$obj    = get_post_type_object( $type );
			$out[]  = array(
				'type'        => $type,
				'label'       => $obj ? $obj->labels->name : $type,
				'description' => $obj && '' !== (string) $obj->description ? (string) $obj->description : '',
				'count'       => isset( $counts->publish ) ? (int) $counts->publish : 0,
				'excluded'    => in_array( $type, $excluded, true ),
			);
		}
		return $out;
	}
}
