# encoding: utf-8
# frozen_string_literal: true
#
# Redmine plugin for Document Management System "Features"
#
# Copyright © 2011-22 Karel Pičman <karel.picman@kontron.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

desc <<-END_DESC
Alert all users who are expected to do an approval in the current approval steps

Available options:
  * dry_run - No email, just print list of recipients to the console

Example:
  rake redmine:dmsf_alert_approvals RAILS_ENV="production"
  rake redmine:dmsf_alert_approvals dry_run=1 RAILS_ENV="production"
END_DESC
  
namespace :redmine do
  task :dmsf_alert_approvals => :environment do    
    DmsfAlertApprovals.alert
  end
end

class DmsfAlertApprovals    

  def self.alert
    dry_run = ENV['dry_run']
    revisions = DmsfFileRevision.visible.joins(:dmsf_file).joins('JOIN projects ON projects.id = dmsf_files.project_id').where(
        dmsf_file_revisions: { workflow: DmsfWorkflow::STATE_WAITING_FOR_APPROVAL },
        projects: { status: Project::STATUS_ACTIVE})
    revisions.each do |revision|
      next unless revision.dmsf_file.last_revision == revision
      workflow = DmsfWorkflow.find_by_id revision.dmsf_workflow_id
      next unless workflow
      assignments = workflow.next_assignments revision.id
      assignments.each do |assignment|
        next unless assignment.user.active?
        if dry_run
          puts "#{assignment.user.name} <#{assignment.user.mail}>"
        else
          DmsfMailer.deliver_workflow_notification(
            [assignment.user],
            workflow,
            revision,
            :text_email_subject_requires_approval,
            :text_email_finished_step,
            :text_email_to_proceed,
            nil,
            assignment.dmsf_workflow_step
            )
        end
      end      
    end
  end

end