class Admins::UnapproveCompetitionPrizeUserService < ApplicationService
  attr_reader :prize_user, :approver

  def initialize(prize_user, approver)
    @prize_user = prize_user
    @approver   = approver
  end

  def call
    raise Error, '状态有误' if prize_user.pending?

    ActiveRecord::Base.transaction do
      prize_user.unapprove
      prize_user.approver = approver
      prize_user.approved_at = nil
      prize_user.save!

      # 删除团队证书
      team_certificate_path = Util::FileManage.source_disk_filename(prize_user.competition_team, CompetitionTeam.certificate_suffix)
      File.delete(team_certificate_path) if File.exist?(team_certificate_path)

      # 删除个人证书
      user_certificate_path = Util::FileManage.source_disk_filename(prize_user)
      File.delete(user_certificate_path) if File.exist?(user_certificate_path)
    end
  end
end