require "rails_helper"

RSpec.describe AccountsController, type: :controller do
  render_views

  describe "#new" do
    it "returns a successful response" do
      get :new

      expect(response).to be_successful
    end
  end

  describe "#create" do
    let(:params) do
      { 
        user: { 
          first_name: "John",
          last_name: "Doe",
          email: "test_user+1@test.com", 
          password: "password", 
          account_name: "Test Dev account", 
          interested_product_name: "DirectME" 
        }
      } 
    end
    
    context "when valid account and user parameters are passed" do
      it "creates account successfully" do
        expect {
          post :create, params: params
        }.to change(Account, :count).by(1)
      end

      it "creates user successfully" do
        expect {
          post :create, params: params
        }.to change(User, :count).by(1)
        
        expect(User.last.first_name).to eq("John")
        expect(User.last.last_name).to eq("Doe")
      end

      it "signs in user successfully" do
        post :create, params: params

        expect(response).to redirect_to(signed_in_root_path)
      end

      it "creates guest_sign_up event" do
        expect {
          post :create, params: params
        }.to have_enqueued_job(TrackAnalyticsJob).with(be_kind_of(User), be_kind_of(Account), :track_guest_sign_up, user_agent: "Rails Testing", user_ip: "0.0.0.0")
      end

      it "enqueues hubspot form submission job" do
        expect {
          post :create, params: params
        }.to have_enqueued_job(SubmitHubspotFormJob).with(
          "test_user+1@test.com", 
          "Test Dev account", 
          i_m_interested_in: "DirectME"
        )
      end
    end

    context "when user is not able to sign in immediately after signup" do
      before do
        allow(@controller).to receive(:sign_in).and_return(nil)
      end

      it "redirects to the sign in page" do
        post :create, params: params

        expect(response).to redirect_to(new_session_path)
      end
    end

    context "when account creation fails" do
      it "redirects to the new account page" do
        post :create, params: { user: { email: "test_user+1@test.com", password: "password", account_name: "" }}

        expect(response).to redirect_to(new_account_path)
      end
    end

    context "when user is invalid" do
      it "redirects to the new account page" do
        post :create, params: { user: { email: "", password: "password", account_name: "Test Dev account" }}

        expect(response).to redirect_to(new_account_path)
      end
    end
  end

  describe "#update" do
    let(:current_user) { create(:user, :account_manager) }

    before do
      sign_in(current_user)
    end

    context "when account successfully updated" do
      it "returns updated account" do
        patch :update, params: { id: current_user.primary_account, account: account_params }, format: :js

        expect(current_user.primary_account.reload.logo.attached?).to be(true)
        expect(current_user.primary_account.logo.filename).to eq "person_photo.png"
      end
    end
  end

  def account_params
    logo = Rack::Test::UploadedFile.new(file_fixture("person_photo.png"), "image/png")

    { logo: logo }
  end
end
