<?php

namespace AIPT\Api\BulkGenerator\Desc\Services;

use AIPT\Utils\ProductFilter\ProductFilterBuilder;
use AIPT\Utils\ProductFilter\ProductFilterMapper;

/**
 * ProductFilterService - Handles product filtering and retrieval
 *
 * Encapsulates the logic for fetching filtered products from the database.
 * Coordinates between request parameters, filter mapping, and product builder.
 *
 * @since 2.4.3
 */
class ProductFilterService {

	/**
	 * Fetches filtered products based on request parameters
	 *
	 * @param array $params Request parameters from REST endpoint
	 * @return array Adapted response with products and pagination info
	 */
	public function fetchFilteredProducts( array $params ): array {

		$filter_params = ProductFilterMapper::fromRequest( $params );

		$builder = new ProductFilterBuilder();
		ProductFilterMapper::applyToBuilder( $builder, $filter_params );
		$result = $builder->getProducts();

		$adapted = ProductFilterMapper::adaptResponse( $result, $filter_params );

		return $adapted;
	}
}
