<?php
/**
 * Generation cost estimator.
 *
 * @package SmartyPress_AI_Engine
 */

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

/**
 * Estimates model cost for generation pipelines.
 */
class SmartyPress_AI_Content_Generator_Generation_Cost_Estimator {

	/**
	 * Estimate legacy bulk generation cost.
	 *
	 * @param int    $total Total items.
	 * @param string $provider Provider.
	 * @param string $model Model.
	 * @return array
	 */
	public function estimate_legacy_bulk_cost( $total, $provider, $model ) {
		$total    = max( 0, absint( $total ) );
		$provider = sanitize_key( $provider );
		$model    = sanitize_text_field( $model );

		$input_tokens_per_item  = (int) apply_filters( 'smartypress_ai_bulk_estimated_input_tokens', 3500, $provider, $model );
		$output_tokens_per_item = (int) apply_filters( 'smartypress_ai_bulk_estimated_output_tokens', 2500, $provider, $model );
		$per_item               = array(
			'cost'   => 0.0,
			'source' => 'unknown',
		);

		if ( class_exists( 'SmartyPress_AI_Model_Pricing' ) ) {
			$per_item = SmartyPress_AI_Model_Pricing::calculate_cost(
				$provider,
				$model,
				$input_tokens_per_item,
				$output_tokens_per_item
			);
		}

		return array(
			'items'              => $total,
			'provider'           => $provider,
			'model'              => $model,
			'input_tokens_each'  => $input_tokens_per_item,
			'output_tokens_each' => $output_tokens_per_item,
			'per_item_cost'      => (float) ( $per_item['cost'] ?? 0 ),
			'total_cost'         => (float) ( $per_item['cost'] ?? 0 ) * $total,
			'source'             => sanitize_key( $per_item['source'] ?? 'unknown' ),
		);
	}
}
