require "rails_helper"

RSpec.describe Teachable::Api::V1::BonusesController do

  context "#index" do
    it "returns a list of bonuses based on upgrade src" do
      upgrade_src = SecureRandom.hex(6)
      generate_bonuses_for(upgrade_src)
      get :index, params: {upgrade_src: upgrade_src, api_key: ENV["TEACHABLE_CROSS_PLATFORM_API_KEY"]}

      expect(JSON.parse(response.body)).to eq(
        {
          "bonuses" => [
            {
              "Name" => "The Profitable Teacher",
              "Description" => "Teachable’s flagship program explaining our 7-step process to create \u0026 launch a course",
              "Value" => "348",
              "Course ID" => "48971",
              "Icon" => "https://cdn.filepicker.io/api/file/aMFkCABdRW6PR19u7oEJ",
              "bonus_type" => "basic",
              "bonus_type_interval" => "month"
            },
            {
              "Name" => "Course Creation Workshop Series",
              "Description" => "11 on-demand replays from the 2016 Teachable Summit, starring 28 speakers, like Pat Flynn \u0026 Mariah Coz",
              "Value" => "199",
              "Course ID" => "124323",
              "Icon" => "https://cdn.filepicker.io/api/file/aMFkCABdRW6PR19u7oEJ",
              "bonus_type" => "basic",
              "bonus_type_interval" => "month"
            }
          ]
        }
      )
    end

    it "returns 'Missing upgrade_src' message if upgrade_src missing" do
      get :index, params: {api_key: ENV["TEACHABLE_CROSS_PLATFORM_API_KEY"]}
      expect(JSON.parse(response.body)).to eq({"error" => "Missing upgrade_src param"})
    end

    it "returns proper error message if upgrade_src not found with bonuses" do
      get :index, params: {upgrade_src: "maga", api_key: ENV["TEACHABLE_CROSS_PLATFORM_API_KEY"]}
      expect(JSON.parse(response.body)).to eq({"error" => "Unable to find bonuses with that upgrade_src: maga"})
    end

    it "returns a list of bonuses when upgrade_src is from replay" do
      upgrade_src = SecureRandom.hex(6)
      generate_bonuses_for_webinar(upgrade_src)
      get :index, params: { upgrade_src: "#{upgrade_src}-replay", api_key: ENV["TEACHABLE_CROSS_PLATFORM_API_KEY"] }
      expect(JSON.parse(response.body)).to eq(
        {
          "bonuses" => [
            {
              "Name" => "The Profitable Teacher",
              "Description" => "Teachable’s flagship program explaining our 7-step process to create \u0026 launch a course",
              "Value" => "348",
              "Course ID" => "48971",
              "Icon" => "https://cdn.filepicker.io/api/file/aMFkCABdRW6PR19u7oEJ",
              "bonus_type" => "basic",
              "bonus_type_interval" => "month"
            }
          ]
        }
      )
    end
  end

end
