<?php
/**
 * Bulk legacy generation pipeline.
 *
 * @package SmartyPress_AI_Engine
 */

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

/**
 * Generates a single item through the fast legacy bulk flow.
 */
class SmartyPress_AI_Content_Generator_Bulk_Legacy_Generation_Pipeline {

	/**
	 * Prompt service.
	 *
	 * @var SmartyPress_AI_Content_Generator_Bulk_Prompt_Service
	 */
	private $prompt_service;

	/**
	 * Item context service.
	 *
	 * @var SmartyPress_AI_Content_Generator_Bulk_Item_Context_Service
	 */
	private $item_context_service;

	/**
	 * Constructor.
	 *
	 * @param SmartyPress_AI_Content_Generator_Bulk_Prompt_Service $prompt_service Prompt service.
	 * @param SmartyPress_AI_Content_Generator_Bulk_Item_Context_Service $item_context_service Item context service.
	 */
	public function __construct( SmartyPress_AI_Content_Generator_Bulk_Prompt_Service $prompt_service, SmartyPress_AI_Content_Generator_Bulk_Item_Context_Service $item_context_service ) {
		$this->prompt_service       = $prompt_service;
		$this->item_context_service = $item_context_service;
	}

	/**
	 * Generate and create one bulk item.
	 *
	 * @param SmartyPress_AI_Abstract_Post_Generator $generator Generator facade.
	 * @param string                                 $topic Topic.
	 * @param array                                  $settings Generation settings.
	 * @return array
	 */
	public function generate_item( SmartyPress_AI_Abstract_Post_Generator $generator, $topic, array $settings = array() ) {
		$context             = $settings['context'] ?? '';
		$settings['context'] = $this->item_context_service->build( $topic, $context, $settings['item'] ?? array(), $settings );

		$default_prompts = $this->prompt_service->get_default_prompts();
		$prompts         = $settings['prompts'] ?? array();
		foreach ( array( 'title', 'sections', 'content', 'excerpt' ) as $key ) {
			if ( empty( $prompts[ $key ] ) ) {
				$prompts[ $key ] = $default_prompts[ $key ];
			}
		}
		$settings['prompts'] = $prompts;

		return $generator->generate_and_create_post( $topic, $settings );
	}
}
