require "uri"
require "net/http"
require "openssl"

module PaymentServices
  class DataStruct < OpenStruct
    def as_json(*args)
      super.as_json["table"]
    end
  end

  class Paddle
    attr_accessor :auth

    def initialize
      @conn = Faraday.new request: {
        params_encoder: Faraday::FlatParamsEncoder
      }
      @auth = {
        vendor_id: Chaskiq::Config.get("PADDLE_VENDOR_ID"),
        vendor_auth_code: Chaskiq::Config.get("PADDLE_SECRET_TOKEN")
      }
      self
    end

    def get_plans
      res = get_data(
        url: "https://vendors.paddle.com/api/2.0/subscription/plans",
        params: auth
      )

      res.filter do |o|
        Plan.all.map{|o| o[:id]}.include?(o.id)
      end.map do |o|
        p = Plan.get_by_id(o.id)
        {
          id: p["id"],
          source: "paddle",
          features: p.dig("features"),
          name: p["name"],
          pricing: o.recurring_price.USD,
          billing_type: o.recurring_price.billing_type
        }
      end
    end

    def get_user_transactions(id)
      res = get_data(
        url: "https://vendors.paddle.com/api/2.0/user/#{id}/transactions",
        params: auth
      )
    end

    def get_subscription_transactions(app)
      id = app.payment_attribute("user_id")
      return [] if id.blank?

      res = get_data(
        url: "https://vendors.paddle.com/api/2.0/subscription/#{id}/transactions",
        params: auth
      )
    end

    def get_subscription(id)
      return [] if id.nil?

      res = get_data(
        url: "https://vendors.paddle.com/api/2.0/subscription/users",
        params: auth.merge({
                             subscription_id: id
                           })
      )
    end

    def update_subscription(id, plan_id:, passthrough: nil)
      res = get_data(
        url: "https://vendors.paddle.com/api/2.0/subscription/users/update",
        params: auth.merge({
                             subscription_id: id,
                             passthrough: passthrough,
                             plan_id: plan_id
                           })
      )
    end

    def get_data(url:, params: {})
      response = @conn.post(url, params) do |req|
        # req.body = params.to_json
      end
      JSON.parse(response.body, object_class: DataStruct).response
    end
  end
end

# frozen_string_literal: true
