require "rails_helper"

RSpec.describe ContractsController, type: :controller do
  let(:current_user) { create(:user) }

  before do
    sign_in current_user
  end

  shared_examples "a contracts controller" do
    describe "#show" do
      context "for a native release" do
        it "responds with a PDF contract generated dynamically" do
          pdf_body = Tempfile.new
          allow_any_instance_of(Contract).to receive(:to_pdf).and_return(pdf_body)

          get :show, params: { format: :pdf, "#{native_releasable.model_name.singular}_id" => native_releasable }

          expect(response).to be_successful
          expect(content_type).to eq("application/pdf")
          expect(content_disposition).to include("inline")
        end
      end

      context "for a non-native release" do
        it "responds with the attached contract" do
          contract = double(:contract, service_url: "http://example.org/contract.pdf")
          allow_any_instance_of(non_native_releasable.class).to receive(:contract).and_return(contract)

          get :show, params: { format: :pdf, "#{non_native_releasable.model_name.singular}_id" => non_native_releasable }

          expect(response).to be_redirect
          expect(response).to redirect_to("http://example.org/contract.pdf")
        end
      end
    end
  end

  context "for appearance releases" do
    let(:native_releasable) { create(:appearance_release_with_contract_template, :native) }
    let(:non_native_releasable) { create(:appearance_release_with_contract_template, :non_native) }

    it_behaves_like "a contracts controller"
  end

  context "for location releases" do
    let(:native_releasable) { create(:location_release_with_contract_template, :native) }
    let(:non_native_releasable) { create(:location_release_with_contract_template, :non_native) }

    it_behaves_like "a contracts controller"
  end

  context "for material releases" do
    let(:native_releasable) { create(:material_release_with_contract_template, :native) }
    let(:non_native_releasable) { create(:material_release_with_contract_template, :non_native) }

    it_behaves_like "a contracts controller"
  end

  context "for talent releases" do
    let(:native_releasable) { create(:talent_release_with_contract_template, :native) }
    let(:non_native_releasable) { create(:talent_release_with_contract_template, :non_native) }

    it_behaves_like "a contracts controller"
  end
end
