# frozen_string_literal: true

require 'rails_helper'

describe ContractTemplates::BlankContractsController do
  let(:account) { build(:account) }
  let(:current_user) { create(:user, :manager, primary_account: account) }
  let(:project) { create(:project, members: [current_user], account: account) }
  let(:contract_template) { create(:contract_template, project: project) }

  before do
    sign_in(current_user)
  end

  describe '#show' do
    it 'responds with success' do
      get :show, params: { contract_template_id: contract_template }

      expect(response).to be_successful
    end

    it "responds with a PDF" do
      pdf_body = Tempfile.new
      allow_any_instance_of(Contract).to receive(:to_pdf).and_return(pdf_body)

      get :show, params: { contract_template_id: contract_template }

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

  describe '#new' do
    it 'responds with success' do
      get :new, params: { contract_template_id: contract_template }

      expect(response).to be_successful
    end
  end

  describe '#create' do
    it "responds with redirect for invalid number of copies" do
      post :create, params: { contract_template_id: contract_template, number_of_copies: -2 }

      expect(response).to redirect_to [:new, contract_template, :blank_contracts]
    end

    it "responds with PDF" do
      pdf_body = Tempfile.new
      allow_any_instance_of(Contract).to receive(:to_pdf).and_return(pdf_body)

      get :show, params: { contract_template_id: contract_template, number_of_copies: 2 }

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