class Admission::Applicants::AgreementService < ApplicationService
  def initialize(applicant, application)
    @applicant = applicant
    @application = application
  end

  def props
    application_agreements.map do |application_agreement|
      {}.tap do |props|
        props[:id] = application_agreement.agreement.id
        props[:name] = application_agreement.agreement.name
        props[:body] = application_agreement.agreement.body
        props[:response] = responses[application_agreement.agreement_id]
        props[:required] = application_agreement.required
      end
    end
  end

  def available?
    application_agreements.exists?
  end

  private
    def application_agreements
      @application_agreements ||= @application.application_agreements.includes(:agreement).ordered
    end

    def responses
      @responses ||= @applicant
        .applicant_agreements
        .map { |a| [a.agreement.id, a.response] }
        .to_h
    end
end
