<%
  brand_name = global_config['BRAND_NAME'] || 'Chatwoot'
  recipient_name = @resource.name.presence || @resource.email
  account_user = @resource&.account_users&.first
  inviter = account_user&.inviter
  account_name = account_user&.account&.name
  is_saml_account = account_user&.account&.saml_enabled?
  invited_user = inviter.present? && @resource.unconfirmed_email.blank?

  eyebrow = 'Welcome'
  heading = 'Confirm your email to get started'
  intro_text =
    "Welcome to #{brand_name}. We just need to verify your email address before you can start using your account."
  supporting_text = 'This only takes a moment.'
  action_text = 'Confirm my account'
  action_url = frontend_url('auth/confirmation', confirmation_token: @token)
  info_title = nil
  info_text = nil
  detail_rows = []
  detail_rows << ['New email', @resource.unconfirmed_email] if @resource.unconfirmed_email.present?

  if @resource.unconfirmed_email.present?
    eyebrow = 'Email update'
    heading = 'Confirm your new email address'
    intro_text = "We received a request to update the email address on your #{brand_name} account."
    supporting_text = 'Confirm the new address below to finish the change.'
    action_text = 'Confirm email address'
  elsif @resource.confirmed?
    eyebrow = 'Account ready'

    if is_saml_account
      heading = 'Your access is ready'
      intro_text = "Your #{brand_name} access is already set up."
      supporting_text = "Use your organization's Single Sign-On (SSO) portal to access #{brand_name}."
      action_text = nil
      action_url = nil
      info_title = "Sign in with your organization's SSO"
      info_text =
        "You won't need a separate password for #{brand_name}. Start from your company identity provider portal."
      detail_rows = []
      detail_rows << ['Workspace', account_name] if account_name.present?
      detail_rows << ['Sign-in method', 'Single Sign-On (SSO)']
    else
      heading = 'Your account is ready'
      intro_text = "Your #{brand_name} account is already active."
      supporting_text = 'Use the button below to sign in and continue where you left off.'
      action_text = 'Open my account'
      action_url = frontend_url('auth/sign_in')
      detail_rows = []
    end
  elsif invited_user
    eyebrow = 'Workspace invitation'
    heading = account_name.present? ? "You're invited to join #{account_name}" : "You're invited to try #{brand_name}"

    if is_saml_account
      intro_text = if account_name.present?
        "#{inviter.name} invited you to access the #{account_name} workspace on #{brand_name}."
      else
        "#{inviter.name} invited you to access #{brand_name}."
      end
      supporting_text =
        "Your organization uses Single Sign-On (SSO), so you won't need to create a separate password."
      action_text = nil
      action_url = nil
      info_title = "Use your organization's SSO portal"
      info_text = "Continue from your company identity provider portal to access #{brand_name}."
      detail_rows = [['Invited by', inviter.name]]
      detail_rows << ['Workspace', account_name] if account_name.present?
      detail_rows << ['Sign-in method', 'Single Sign-On (SSO)']
    else
      intro_text = if account_name.present?
        "#{inviter.name} invited you to join the #{account_name} workspace on #{brand_name}."
      else
        "#{inviter.name} invited you to try #{brand_name}."
      end
      supporting_text = 'Create your account to start collaborating with your team.'
      action_text = 'Accept invitation'
      action_url = frontend_url(
        'auth/password/edit',
        reset_password_token: @resource.send(:set_reset_password_token)
      )
      detail_rows = [['Invited by', inviter.name]]
      detail_rows << ['Workspace', account_name] if account_name.present?
    end
  end
%>

<%= render partial: 'devise/mailer/confirmation_body', locals: {
  action_text: action_text,
  action_url: action_url,
  detail_rows: detail_rows,
  eyebrow: eyebrow,
  heading: heading,
  info_text: info_text,
  info_title: info_title,
  intro_text: intro_text,
  recipient_name: recipient_name,
  supporting_text: supporting_text
} %>
