# Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.

# typed: true
# frozen_string_literal: true

require 'faraday'
require 'faraday/multipart'
require 'sorbet-runtime'

module PlexRubySDK
  extend T::Sig
  class Search
    extend T::Sig
    # API Calls that perform search operations with Plex Media Server
    # 

    sig { params(sdk_config: SDKConfiguration).void }
    def initialize(sdk_config)
      @sdk_configuration = sdk_config
    end


    sig { params(query: ::String, section_id: T.nilable(::Float), limit: T.nilable(::Float)).returns(::PlexRubySDK::Operations::PerformSearchResponse) }
    def perform_search(query, section_id = nil, limit = nil)
      # perform_search - Perform a search
      # This endpoint performs a search across all library sections, or a single section, and returns matches as hubs, split up by type. It performs spell checking, looks for partial matches, and orders the hubs based on quality of results. In addition, based on matches, it will return other related matches (e.g. for a genre match, it may return movies in that genre, or for an actor match, movies with that actor).
      # 
      # In the response's items, the following extra attributes are returned to further describe or disambiguate the result:
      # 
      # - `reason`: The reason for the result, if not because of a direct search term match; can be either:
      #   - `section`: There are multiple identical results from different sections.
      #   - `originalTitle`: There was a search term match from the original title field (sometimes those can be very different or in a foreign language).
      #   - `<hub identifier>`: If the reason for the result is due to a result in another hub, the source hub identifier is returned. For example, if the search is for "dylan" then Bob Dylan may be returned as an artist result, an a few of his albums returned as album results with a reason code of `artist` (the identifier of that particular hub). Or if the search is for "arnold", there might be movie results returned with a reason of `actor`
      # - `reasonTitle`: The string associated with the reason code. For a section reason, it'll be the section name; For a hub identifier, it'll be a string associated with the match (e.g. `Arnold Schwarzenegger` for movies which were returned because the search was for "arnold").
      # - `reasonID`: The ID of the item associated with the reason for the result. This might be a section ID, a tag ID, an artist ID, or a show ID.
      # 
      # This request is intended to be very fast, and called as the user types.
      # 
      request = ::PlexRubySDK::Operations::PerformSearchRequest.new(
        
        query: query,
        section_id: section_id,
        limit: limit
      )
      url, params = @sdk_configuration.get_server_details
      base_url = Utils.template_url(url, params)
      url = "#{base_url}/hubs/search"
      headers = {}
      query_params = Utils.get_query_params(::PlexRubySDK::Operations::PerformSearchRequest, request, @sdk_configuration.globals)
      headers['Accept'] = 'application/json'
      headers['user-agent'] = @sdk_configuration.user_agent

      r = @sdk_configuration.client.get(url) do |req|
        req.headers = headers
        req.params = query_params
        Utils.configure_request_security(req, @sdk_configuration.security) if !@sdk_configuration.nil? && !@sdk_configuration.security.nil?
      end

      content_type = r.headers.fetch('Content-Type', 'application/octet-stream')

      res = ::PlexRubySDK::Operations::PerformSearchResponse.new(
        status_code: r.status, content_type: content_type, raw_response: r
      )
      if r.status == 200
      elsif r.status == 400
        if Utils.match_content_type(content_type, 'application/json')
          out = Utils.unmarshal_complex(r.env.response_body, ::PlexRubySDK::Operations::PerformSearchBadRequest)
          res.bad_request = out
        end
      elsif r.status == 401
        if Utils.match_content_type(content_type, 'application/json')
          out = Utils.unmarshal_complex(r.env.response_body, ::PlexRubySDK::Operations::PerformSearchUnauthorized)
          res.unauthorized = out
        end
      end
      res
    end


    sig { params(query: ::String, section_id: T.nilable(::Float), limit: T.nilable(::Float)).returns(::PlexRubySDK::Operations::PerformVoiceSearchResponse) }
    def perform_voice_search(query, section_id = nil, limit = nil)
      # perform_voice_search - Perform a voice search
      # This endpoint performs a search specifically tailored towards voice or other imprecise input which may work badly with the substring and spell-checking heuristics used by the `/hubs/search` endpoint. 
      # It uses a [Levenshtein distance](https://en.wikipedia.org/wiki/Levenshtein_distance) heuristic to search titles, and as such is much slower than the other search endpoint. 
      # Whenever possible, clients should limit the search to the appropriate type. 
      # Results, as well as their containing per-type hubs, contain a `distance` attribute which can be used to judge result quality.
      # 
      request = ::PlexRubySDK::Operations::PerformVoiceSearchRequest.new(
        
        query: query,
        section_id: section_id,
        limit: limit
      )
      url, params = @sdk_configuration.get_server_details
      base_url = Utils.template_url(url, params)
      url = "#{base_url}/hubs/search/voice"
      headers = {}
      query_params = Utils.get_query_params(::PlexRubySDK::Operations::PerformVoiceSearchRequest, request, @sdk_configuration.globals)
      headers['Accept'] = 'application/json'
      headers['user-agent'] = @sdk_configuration.user_agent

      r = @sdk_configuration.client.get(url) do |req|
        req.headers = headers
        req.params = query_params
        Utils.configure_request_security(req, @sdk_configuration.security) if !@sdk_configuration.nil? && !@sdk_configuration.security.nil?
      end

      content_type = r.headers.fetch('Content-Type', 'application/octet-stream')

      res = ::PlexRubySDK::Operations::PerformVoiceSearchResponse.new(
        status_code: r.status, content_type: content_type, raw_response: r
      )
      if r.status == 200
      elsif r.status == 400
        if Utils.match_content_type(content_type, 'application/json')
          out = Utils.unmarshal_complex(r.env.response_body, ::PlexRubySDK::Operations::PerformVoiceSearchBadRequest)
          res.bad_request = out
        end
      elsif r.status == 401
        if Utils.match_content_type(content_type, 'application/json')
          out = Utils.unmarshal_complex(r.env.response_body, ::PlexRubySDK::Operations::PerformVoiceSearchUnauthorized)
          res.unauthorized = out
        end
      end
      res
    end


    sig { params(query: ::String).returns(::PlexRubySDK::Operations::GetSearchResultsResponse) }
    def get_search_results(query)
      # get_search_results - Get Search Results
      # This will search the database for the string provided.
      request = ::PlexRubySDK::Operations::GetSearchResultsRequest.new(
        
        query: query
      )
      url, params = @sdk_configuration.get_server_details
      base_url = Utils.template_url(url, params)
      url = "#{base_url}/search"
      headers = {}
      query_params = Utils.get_query_params(::PlexRubySDK::Operations::GetSearchResultsRequest, request, @sdk_configuration.globals)
      headers['Accept'] = 'application/json'
      headers['user-agent'] = @sdk_configuration.user_agent

      r = @sdk_configuration.client.get(url) do |req|
        req.headers = headers
        req.params = query_params
        Utils.configure_request_security(req, @sdk_configuration.security) if !@sdk_configuration.nil? && !@sdk_configuration.security.nil?
      end

      content_type = r.headers.fetch('Content-Type', 'application/octet-stream')

      res = ::PlexRubySDK::Operations::GetSearchResultsResponse.new(
        status_code: r.status, content_type: content_type, raw_response: r
      )
      if r.status == 200
        if Utils.match_content_type(content_type, 'application/json')
          out = Utils.unmarshal_complex(r.env.response_body, ::PlexRubySDK::Operations::GetSearchResultsResponseBody)
          res.object = out
        end
      elsif r.status == 400
        if Utils.match_content_type(content_type, 'application/json')
          out = Utils.unmarshal_complex(r.env.response_body, ::PlexRubySDK::Operations::GetSearchResultsBadRequest)
          res.bad_request = out
        end
      elsif r.status == 401
        if Utils.match_content_type(content_type, 'application/json')
          out = Utils.unmarshal_complex(r.env.response_body, ::PlexRubySDK::Operations::GetSearchResultsUnauthorized)
          res.unauthorized = out
        end
      end
      res
    end
  end
end
