/// Use Case: Search Products
///
/// Business logic for searching products by query string with pagination support.
library;

import 'package:worker/features/products/domain/entities/product.dart';
import 'package:worker/features/products/domain/repositories/products_repository.dart';

/// Search Products Use Case
///
/// Searches for products matching the given query string with pagination.
class SearchProducts {
  final ProductsRepository repository;

  const SearchProducts(this.repository);

  /// Execute the use case
  ///
  /// [query] - Search query string
  /// [limitStart] - Starting index for pagination (default: 0)
  /// [limitPageLength] - Number of items per page (default: 12)
  /// Returns list of products matching the query
  Future<List<Product>> call(
    String query, {
    int limitStart = 0,
    int limitPageLength = 12,
  }) async {
    // Return all products if query is empty
    if (query.trim().isEmpty) {
      return await repository.getAllProducts(
        limitStart: limitStart,
        limitPageLength: limitPageLength,
      );
    }

    return await repository.searchProducts(
      query,
      limitStart: limitStart,
      limitPageLength: limitPageLength,
    );
  }
}
