class Pdf::Nursing::StudentProfileLogService < Pdf::NewApplicationService
  include DateRangeHelper

  def initialize(student, params)
    @student = student
    @school = student.school
    @dates = params['dates']
  end

  private
    def header
      content = ActionController::Base.new.render_to_string(
        partial: 'pdf/headers/school_info.html.haml',
        locals: {
          school: @school,
          side_content_partial: 'pdf/nursing/student_profile_log_header',
          side_content: {
            student: @student.decorate,
            start: formatted_dates[:start],
            end: formatted_dates[:end]
          }
        }
      )
      { content: content, spacing: 25 }
    end

    def body
      ActionController::Base.new.render_to_string(
        partial: 'pdf/nursing/student_profile_log.html.haml',
        locals: { logs: logs }
      )
    end

    def configuration
      {}.tap do |config|
        config[:encoding] = 'UTF-8'
        config[:margin] = { top: 40, bottom: 20 }
        config[:orientation] = 'Landscape'
      end
    end

    def logs
      @student.nursing_logs
        .includes(:complaint, :author)
        .with_date_range(datetime_range(@dates))
        .decorate
    end

    def formatted_dates
      dates = @dates.split(',')
      { start: dates.first.to_date, end: dates.last.to_date }
    end
end
