require "spec_helper"

describe TrialExpiresReminder do
  let(:user1) { FactoryGirl.create(:user) }
  let(:user2) { FactoryGirl.create(:user) }
  let(:user3) { FactoryGirl.create(:user) }

  before(:each) do
    ActionMailer::Base.delivery_method = :test
    ActionMailer::Base.perform_deliveries = true
    ActionMailer::Base.deliveries = []

    User.delete_all
    UserMailer.deliveries.clear
    Rails.application.config.trial_expires_reminders_effective_from_date = 2.weeks.ago.to_s
  end

  after(:each) do
    ActionMailer::Base.deliveries.clear
  end

  it "sends reminder emails to users whose trials are about to expire" do
    user1.subscription_trial_ends_at = 1.days.from_now
    user1.subscription_last_checked_at = 2.days.ago
    user1.save!

    user2.subscription_trial_ends_at = 1.days.ago
    user2.subscription_last_checked_at = 2.days.ago
    user2.save!

    TrialExpiresReminder.send_reminders
    
    expect(ActionMailer::Base.deliveries.count).to eq(1)
    expect(ActionMailer::Base.deliveries.map(&:to).flatten).to include(user2.email)
    # Check if the first reminder email was sent by verifying the subject
    expect(ActionMailer::Base.deliveries.last.subject).to include("Your free gold trial has expired, but you have great options to keep playing!")

    expect(user2.reload.trial_expires_reminder1_sent_at).not_to be_nil
  end

  it "does not send reminder emails to users who have already received them" do
    user1.subscription_trial_ends_at = 1.days.ago
    user1.subscription_last_checked_at = 2.days.ago
    user1.trial_expires_reminder1_sent_at = Time.now
    user1.save!

    TrialExpiresReminder.send_reminders

    expect(ActionMailer::Base.deliveries.count).to eq(0)
  end

  it "sends the second reminder email to users whose trials are about to expire" do
    user1.subscription_trial_ends_at = 4.days.ago
    user1.subscription_last_checked_at = 1.days.ago
    user1.trial_expires_reminder1_sent_at = Time.now
    user1.save!

    user2.subscription_trial_ends_at = 5.days.ago
    user2.subscription_last_checked_at = 1.days.ago
    user2.trial_expires_reminder1_sent_at = Time.now
    user2.save!

    TrialExpiresReminder.send_reminders
                                                                                                                                                                                                               
    expect(ActionMailer::Base.deliveries.count).to eq(1)
    expect(ActionMailer::Base.deliveries.map(&:to).flatten).to include(user2.email)
    # Check if the second reminder email was sent by verifying the subject
    expect(ActionMailer::Base.deliveries.last.subject).to include("Don’t forget to check your options to keep playing")

    expect(user2.reload.trial_expires_reminder2_sent_at).not_to be_nil
  end

  it "sends the third reminder email to users whose trials are about to expire" do
    user1.subscription_trial_ends_at = 10.days.ago
    user1.subscription_last_checked_at = 1.days.ago
    user1.trial_expires_reminder1_sent_at = 6.days.ago
    user1.trial_expires_reminder2_sent_at = 4.days.ago
    user1.save!

    TrialExpiresReminder.send_reminders

    expect(ActionMailer::Base.deliveries.count).to eq(1)
    expect(ActionMailer::Base.deliveries.map(&:to).flatten).to include(user1.email)
    # Check if the third reminder email was sent by verifying the subject
    expect(ActionMailer::Base.deliveries.last.subject).to include("One last reminder!")

    expect(user1.reload.trial_expires_reminder3_sent_at).not_to be_nil
  end

  it "sends first and second and third reminder emails to users whose trials are about to expire" do
    user1.subscription_trial_ends_at = 2.days.ago
    user1.subscription_last_checked_at = 1.days.ago
    user1.save!

    user2.subscription_trial_ends_at = 6.days.ago
    user2.subscription_last_checked_at = 1.days.ago
    user2.trial_expires_reminder1_sent_at = 4.days.ago
    user2.save!

    user3.subscription_trial_ends_at = 10.days.ago
    user3.subscription_last_checked_at = 2.days.ago
    user3.trial_expires_reminder1_sent_at = 6.days.ago
    user3.trial_expires_reminder2_sent_at = 4.days.ago
    user3.save!

    TrialExpiresReminder.send_reminders

    expect(user1.reload.trial_expires_reminder1_sent_at).not_to be_nil
    expect(user2.reload.trial_expires_reminder2_sent_at).not_to be_nil
    expect(user3.reload.trial_expires_reminder3_sent_at).not_to be_nil

    expect(ActionMailer::Base.deliveries.count).to eq(3)
    expect(ActionMailer::Base.deliveries.map(&:to).flatten).to include(user1.email, user2.email, user3.email)

  end
end