require "rails_helper"

describe UserMailer do
  describe "password_reset" do
    let(:user) { create(:user) }
    let(:mail) { UserMailer.password_reset(user) }

    it "renders the headers" do
      expect(mail.subject).to eq("Password reset")
      expect(mail.to).to eq([user.email])
      expect(mail.from).to eq(["support@bigmedia.ai"])
    end

    it "renders the body" do
      expect(mail.body.encoded).to match(edit_password_reset_path(id: user.password_reset_token))
    end
  end

  describe "project_invitation" do
    let(:user) { create(:user) }
    let(:project) { create(:project) }
    let(:mail) { UserMailer.project_invitation(user, project) }

    it "renders the headers" do
      expect(mail.subject).to eq("You've been added to a project in the ME Suite")
      expect(mail.to).to eq([user.email])
      expect(mail.from).to eq(["support@bigmedia.ai"])
    end

    it "renders the body" do
      expect(mail.body.encoded).to match(user.primary_account.name)
      expect(mail.body.encoded).to match(project.name)
      expect(mail.body.encoded).not_to match(edit_password_reset_url(id: user.password_reset_token, host: AppHost.new.domain_with_port))
    end

    context "when user is new" do
      let(:mail) { UserMailer.project_invitation(user, project, user_is_new: true) }

      it "includes a link to set the user password" do
        expect(mail.body.encoded).to match(edit_password_reset_url(id: user.password_reset_token, host: AppHost.new.domain_with_port))
      end
    end
  end

  describe "welcome" do
    let(:user) { create(:user) }
    let(:account) { create(:account) } 
    let(:mail) { UserMailer.welcome(user, account) }

    it "renders the headers" do
      expect(mail.subject).to eq("Welcome to BiG")
      expect(mail.to).to eq([user.email])
      expect(mail.from).to eq(["support@bigmedia.ai"])
    end

    it "renders the body" do
      expect(mail.body.encoded).to match(user.primary_account.name)
      expect(mail.body.encoded).to match(edit_password_reset_url(id: user.password_reset_token, host: AppHost.new.domain_with_port))
    end
  end
end
