class EdFi::Indiana::V2026::StaffContactAssociationService <
  EdFi::Indiana::V2026::ApplicationService
  def call
    employees.each { |e| props(e) }
  end

  def endpoint
    'ed-fi/staffeducationorganizationcontactassociations'
  end

  private
    def employees
      school.employees
        .preload(:state_id, recent_position: :position)
        .joins(:positions, :state_id)
        .position_within_date_range(semester_range)
    end

    def semester_range
      semester = school.school_config.trimesters? ? 2 : school_year.current_semester
      school_year.semester_date_range(semester)
    end

    def props(employee)
      recent_position = employee.recent_position
      return if recent_position.position.edfi_id.blank?

      data = {
        educationOrganizationReference: {
          educationOrganizationId: organization_id.number
        },
        staffReference: {
          staffUniqueId: employee.state_id.number
        },
        contactTitle: 'Unified Access',
        electronicMailAddress: employee.email
      }

      rest_client_request(:post, url, employee, body: data.to_json)
    end
end
