# frozen_string_literal: true

require_relative "products/scores"
require_relative "products/insights"

module Sentilink
  class Client
    attr_writer :account_id, :account_token, :env

    PRODUCTION_BASE_URL = "https://api.sentilink.com"
    SANDBOX_BASE_URL = "https://api.sandbox-sentilink.com"

    def initialize(account_id: nil, account_token: nil, env: nil)
      @account_id = account_id
      @account_token = account_token
      @env = env
    end

    def url
      env == :sandbox ? SANDBOX_BASE_URL : PRODUCTION_BASE_URL
    end

    def account_id
      @account_id || configuration.account_id
    end

    def account_token
      @account_token || configuration.account_token
    end

    def env
      @env || configuration.env
    end

    # Products

    def scores(request = {})
      scores = Scores.new(connection)
      scores.query(request)
    end

    def insights(request = {})
      insights = Insights.new(connection)
      insights.query(request)
    end

    private

    def connection
      # TODO: raise if no account info is set

      connection = Faraday.new(url: url, headers: { "Content-Type" => "application/json" })
      connection.basic_auth(account_id, account_token)

      connection
    end

    def configuration
      Sentilink.configuration
    end
  end
end
