/// Use Case: Submit Review
///
/// Submits a new product review or updates an existing one.
library;

import 'package:worker/features/reviews/domain/repositories/reviews_repository.dart';

/// Use case for submitting a product review
class SubmitReview {
  const SubmitReview(this._repository);

  final ReviewsRepository _repository;

  /// Execute the use case
  ///
  /// [itemId] - Product item code
  /// [rating] - Rating value (0-1 scale for API)
  /// [comment] - Review comment text
  /// [name] - Optional review ID for updates
  ///
  /// Note: The rating should be in 0-1 scale for the API.
  /// If you have a 1-5 star rating, convert it first: `stars / 5.0`
  Future<void> call({
    required String itemId,
    required double rating,
    required String comment,
    String? name,
  }) async {
    // Validate rating range (0-1)
    if (rating < 0 || rating > 1) {
      throw ArgumentError(
        'Rating must be between 0 and 1. Got: $rating. '
        'If you have a 1-5 star rating, convert it first: stars / 5.0',
      );
    }

    // Validate comment length
    if (comment.trim().isEmpty) {
      throw ArgumentError('Review comment cannot be empty');
    }

    if (comment.trim().length < 20) {
      throw ArgumentError(
        'Review comment must be at least 20 characters. Got: ${comment.trim().length}',
      );
    }

    if (comment.length > 1000) {
      throw ArgumentError(
        'Review comment must not exceed 1000 characters. Got: ${comment.length}',
      );
    }

    await _repository.submitReview(
      itemId: itemId,
      rating: rating,
      comment: comment.trim(),
      name: name,
    );
  }
}
