require "test_helper"

class InvitationMailerTest < ActionMailer::TestCase
  test "invite sends email with correct details" do
    user = users(:pending_invitation)
    mail = InvitationMailer.invite(user)

    assert_equal "You've been invited to join Sanasto Wiki", mail.subject
    assert_equal [ user.email ], mail.to
    assert_match user.name, mail.body.encoded
    assert_match user.email, mail.body.encoded
    assert_match user.role.titleize, mail.body.encoded
  end

  test "invite includes invitation link" do
    user = users(:pending_invitation)
    mail = InvitationMailer.invite(user)

    assert_match "invitations/#{user.invitation_token}", mail.body.encoded
  end

  test "invite includes expiry date" do
    user = users(:pending_invitation)
    mail = InvitationMailer.invite(user)

    expires_at = user.invitation_sent_at + User::INVITATION_TOKEN_EXPIRY
    assert_match expires_at.strftime("%B"), mail.body.encoded
  end

  test "invite has both HTML and text parts" do
    user = users(:pending_invitation)
    mail = InvitationMailer.invite(user)

    assert_equal 2, mail.parts.size
    assert_equal "text/plain", mail.text_part.content_type.split(";").first
    assert_equal "text/html", mail.html_part.content_type.split(";").first
  end

  test "invite with approved_entry includes entry details" do
    user = users(:requester_user)
    user.update!(
      invitation_token: SecureRandom.urlsafe_base64(32),
      invitation_sent_at: Time.current
    )
    entry = entries(:requested_entry)
    mail = InvitationMailer.invite(user, approved_entry: entry)

    assert_equal "Your entry request has been approved - Join Sanasto Wiki", mail.subject
    assert_equal [ user.email ], mail.to

    # Check that entry details are included
    assert_match entry.fi, mail.body.encoded
    assert_match entry.en, mail.body.encoded
    assert_match entry.category.to_s.humanize, mail.body.encoded
    assert_match "approved", mail.body.encoded.downcase
  end

  test "invite with approved_entry shows correct message" do
    user = users(:requester_user)
    user.update!(
      invitation_token: SecureRandom.urlsafe_base64(32),
      invitation_sent_at: Time.current
    )
    entry = entries(:requested_entry)
    mail = InvitationMailer.invite(user, approved_entry: entry)

    # HTML part should contain the entry box
    assert_match "Your Approved Entry", mail.html_part.body.encoded
    assert_match "entry-box", mail.html_part.body.encoded

    # Text part should contain entry details
    assert_match "YOUR APPROVED ENTRY", mail.text_part.body.encoded
    assert_match "Category:", mail.text_part.body.encoded
  end

  test "invite without approved_entry uses standard message" do
    user = users(:pending_invitation)
    mail = InvitationMailer.invite(user)

    assert_equal "You've been invited to join Sanasto Wiki", mail.subject
    assert_match "you can contribute to this work", mail.body.encoded.downcase
    assert_no_match "approved", mail.body.encoded.downcase
  end
end
