class EdFi::Indiana::Sandbox::ParentsService < EdFi::Indiana::Sandbox::ApplicationService
  def call
    data = school_year_students_with_association
      .preload(student: :primary_contacts)
      .joins(student: :ed_fi_state_programs)
      .where(ed_fi_state_programs: { program: 0, school_year_id: school_year.id })

    data.each do |student|
      contact = student.student.primary_contacts.first
      next if contact.nil?

      rest_client_request(:post, url, contact, body: props(contact).to_json)
    end
  end

  def endpoint
    'ed-fi/parents'
  end

  private
    def props(contact)
      {
        parentUniqueId: "#{school.id}#{contact.id}",
        firstName: contact.first_name,
        lastSurname: contact.last_name
      }
    end
end
