class Pdf::Nursing::EmergencyInfoService < Pdf::NewApplicationService
  def initialize(student)
    @student = student.decorate
    @school = student.school
  end

  private
    def header
      content = ActionController::Base.new.render_to_string(
        partial: 'pdf/nursing/emergency_info_header.html.haml',
        locals: {
          school: @school,
          student: @student
        }
      )
      { content: content, spacing: 25 }
    end

    def body
      ActionController::Base.new.render_to_string(
        partial: 'pdf/nursing/emergency_info.html.haml',
        locals: set_local_variables
      )
    end

    def set_local_variables
      {
        student: @student,
        family_one_contacts: family_one_contacts.map(&:decorate),
        family_two_contacts: family_two_contacts&.map(&:decorate),
        medical_props: medical_props.select { |k, _v| @student.send(k).present? }
      }
    end

    def family_one_contacts
      @student.contact_families
    end

    def family_two_contacts
      @student.family2&.contact_families
    end

    def medical_props
      {
        health_information: 'Health Information',
        permitted_medications: 'Permission to Administer',
        medical_comments: 'Medical Notes',
        medical_alerts: 'Medical Alerts',
        allergies: 'Allergies',
        medication: 'Medication',
        family_medical_comments: 'Family Comments'
      }
    end
end
