class Pdf::ApplicationService < ApplicationService
  def initialize
    FileUtils.mkdir_p(@school.tmp_path)
    @tmp_path = @school.tmp_path.join(@filename)
  end

  def call
    pdf = WickedPdf.new.pdf_from_string(body, options)
    File.open(@tmp_path, 'wb') { |file| file << pdf }
    Rack::Test::UploadedFile.new(@tmp_path, 'application/pdf')
  end

  private
    def options
      {}.tap do |data|
        data[:footer] = footer
        data[:header] = header if defined?(header)
      end.merge(configuration)
    end

    def configuration
      {}.tap do |config|
        config[:margin] = { top: 40, bottom: 20 }
      end
    end

    def footer
      { center: 'Page [page] of [topage]' }
    end
end
