# frozen_string_literal: true

require "google/cloud/translate/v3"

class GoogleCloudTranslateService
  def self.translate_document(document_path:, target_language_code:, source_language_code: nil)
    location = "global"
    client = Google::Cloud::Translate::V3::TranslationService::Client.new

    parent = client.location_path(
      project: Rails.application.credentials.gcs.project,
      location:
    )

    request = Google::Cloud::Translate::V3::TranslateDocumentRequest.new(
      parent:,
      document_input_config: {
        gcs_source: {
          # The GCS path of the document to translate.
          # Format: gs://<bucket-name>/<file-path>
          input_uri: document_path
        }
      },
      target_language_code:
    )

    request.source_language_code = source_language_code unless source_language_code.nil?
    client.translate_document request
  end
end
