class EdFi::Indiana::V2026::StudentParentAssociationsService <
  EdFi::Indiana::V2026::ApplicationService
  def call
    data = school_year_students_with_association
      .preload(student: [:primary_contacts, :state_number])
      .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(student, contact).to_json)
    end
  end

  def endpoint
    'ed-fi/studentparentassociations'
  end

  private
    def props(student, contact)
      {
        parentReference: {
          parentUniqueId: "#{school.id}#{contact.id}"
        },
        studentReference: {
          studentUniqueId: student.student.state_number.number
        },
      }
    end
end
