module Iwoca
  # For more information about webhooks, see:
  # https://iwoca.stoplight.io/docs/lapi-notifications/branches/2.0.0/ZG9jOjI4NzI5MTI5-webhooks-configuration
  class Webhooks
    def self.connection
      @connection ||= Connection.new('notifications')
    end

    # Will return a payload that looks like this:
    #
    # {
    #   "data": {
    #     "webhook_url": "https://app.finpoint.co.uk/fast_lender_webhooks/iwoca_event",
    #     "webhook_secret_token": "cddafc72e1bca9b33d7ee922a95ae3fdcdf4f37c",
    #     "encryption_method": "sha256"
    #   }
    # }
    def self.configuration
      connection.get('configuration/')
    end

    # Setup a webhook, the params is a payload that looks like this:
    #
    # {
    #   "data": {
    #     "regenerate_webhook_secret_token": true,
    #     "webhook_url": "string"
    #   }
    # }
    def self.update(config:)
      connection.put('configuration/', config)
    end

    # Get a list of the possible event types
    #
    # {
    #   "data": {
    #     "webhook_event_types": [
    #       "approval_status_changed",
    #       "customer_funded",
    #       "application_offered",
    #       "application_declined",
    #       "application_deferred",
    #       "cashflow_added",
    #       "mca_context_changed",
    #       "bank_account_setup",
    #       "application_status_changed",
    #       "application_attributed"
    #     ]
    #   }
    # }
    def self.event_types
      connection.get('webhook_event_types/')
    end

    def self.subscribe(event_types:)
      json = { data: { subscriptions: [] }}

      subscriptions = event_types.map do |event_type|
        { webhook_event_type: event_type }
      end

      json[:data][:subscriptions] = subscriptions

      connection.post('subscriptions/', json)
    end

    def self.unsubscribe(event_types:)
      json = {}
      connection.delete('subscriptions/', json)
    end

    # Returns the active subscriptions in a payload like this:
    # {
    #   "data": {
    #     "subscriptions": [
    #       { "webhook_event_type": "approval_status_changed", "url": null, "subscribed": true },
    #       { "webhook_event_type": "customer_funded", "url": null, "subscribed": true },
    #       { "webhook_event_type": "application_offered", "url": null, "subscribed": true },
    #       { "webhook_event_type": "application_declined", "url": null, "subscribed": true },
    #       { "webhook_event_type": "application_deferred", "url": null, "subscribed": true },
    #       { "webhook_event_type": "cashflow_added", "url": null, "subscribed": true },
    #       { "webhook_event_type": "mca_context_changed", "url": null, "subscribed": true },
    #       { "webhook_event_type": "bank_account_setup", "url": null, "subscribed": true },
    #       { "webhook_event_type": "application_status_changed", "url": null, "subscribed": true },
    #       { "webhook_event_type": "application_attributed", "url": null, "subscribed": true }
    #     ]
    #   }
    # }
    def self.subscriptions
      connection.get('subscriptions/')
    end
  end
end